Creating a Simple Docker Compose Project
Now that Docker Compose is properly installed and working, let us create a simple project to test our setup. We will create a basic web application with a web server and a Redis database.
Create a Project Directory
First, let us create a dedicated directory for our project:
mkdir ~/project/docker-compose-test
cd ~/project/docker-compose-test
Create a Docker Compose File
Now, let us create a docker-compose.yml
file using the nano
text editor:
nano docker-compose.yml
Copy and paste the following content into the file:
version: "3"
services:
web:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
redis:
image: redis:alpine
This Docker Compose file defines two services:
web
: An Nginx web server that will serve content from a local directory
redis
: A Redis database for caching or storing session data
Press Ctrl+O
followed by Enter
to save the file, then press Ctrl+X
to exit nano.
Create HTML Content
Let us create a simple HTML page that our web service will serve:
mkdir -p html
nano html/index.html
Copy and paste the following content into the file:
<!DOCTYPE html>
<html>
<head>
<title>Docker Compose Test</title>
</head>
<body>
<h1>Hello from Docker Compose!</h1>
<p>If you can see this, your Docker Compose setup is working correctly.</p>
</body>
</html>
Press Ctrl+O
followed by Enter
to save the file, then press Ctrl+X
to exit nano.
Run the Docker Compose Project
Now, let us start our Docker Compose project:
docker-compose up -d
The -d
flag runs the containers in the background (detached mode). You should see output similar to:
Creating network "docker-compose-test_default" with the default driver
Pulling web (nginx:alpine)...
Pulling redis (redis:alpine)...
[...]
Creating docker-compose-test_web_1 ... done
Creating docker-compose-test_redis_1 ... done
Verify the Services Are Running
Let us check that our services are running correctly:
docker-compose ps
You should see output similar to:
Name Command State Ports
------------------------------------------------------------------------------------
docker-compose-test_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
docker-compose-test_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8080->80/tcp
Access the Web Application
Now you can access the web application by opening a web browser in the LabEx interface and navigating to http://localhost:8080
. You should see the "Hello from Docker Compose!" message.
Alternatively, you can use curl
to check the web server from the command line:
curl http://localhost:8080
You should see the HTML content we created earlier.
Stop the Docker Compose Project
When you are finished testing, you can stop the Docker Compose project:
docker-compose down
You should see output similar to:
Stopping docker-compose-test_web_1 ... done
Stopping docker-compose-test_redis_1 ... done
Removing docker-compose-test_web_1 ... done
Removing docker-compose-test_redis_1 ... done
Removing network docker-compose-test_default
Congratulations! You have successfully installed Docker Compose, resolved the "command not found" error, and tested your setup with a simple multi-container application.