Testing Docker Compose
Now that we have successfully installed Docker Compose, let's create a simple project to test that it works correctly.
Create a Project Directory
First, let's create a directory for our test project:
mkdir -p ~/project/docker-compose-test
cd ~/project/docker-compose-test
Create a Docker Compose Configuration File
Now, let's create a simple docker-compose.yml
file using the nano
editor:
nano docker-compose.yml
Add the following content to the file:
version: "3"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
This configuration defines a simple web server using the Nginx image. It maps port 8080 on your host to port 80 in the container and mounts a local directory to serve HTML content.
Save the file by pressing Ctrl+O
, then Enter
, and exit nano with Ctrl+X
.
Create HTML Content
Let's create a directory for our HTML content and a simple HTML file:
mkdir -p html
nano html/index.html
Add the following content to the HTML 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>
Save the file and exit nano.
Start the Docker Compose Application
Now, let's start our Docker Compose application:
docker compose up -d
You should see output similar to:
[+] Running 2/2
⠿ Network docker-compose-test_default Created
⠿ Container docker-compose-test-web-1 Started
This indicates that Docker Compose has created a network and started the Nginx container.
Verify the Application is Running
Let's check that our container is running:
docker compose ps
You should see output similar to:
NAME COMMAND SERVICE STATUS PORTS
docker-compose-test-web-1 "/docker-entrypoint.…" web running 0.0.0.0:8080->80/tcp
Now, let's send a request to the web server to verify it's serving our content:
curl http://localhost:8080
You should see the HTML content we created:
<!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>
Great! You've successfully created and run a Docker Compose application.
Stop the Docker Compose Application
To stop and remove the containers, networks, and volumes created by Docker Compose, run:
docker compose down
You should see output similar to:
[+] Running 2/2
⠿ Container docker-compose-test-web-1 Removed
⠿ Network docker-compose-test_default Removed
This confirms that Docker Compose has cleaned up the resources it created.