Prepare a simple Docker Compose project
In this step, we will prepare a simple Docker Compose project. Since Docker Compose is not pre-installed in the LabEx environment, we will first install it. Then, we will create a simple web application and a Docker Compose file to define and run it.
First, let's install Docker Compose. We will download the Docker Compose binary and make it executable.
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Now, let's verify the installation by checking the Docker Compose version.
docker-compose --version
You should see the installed version of Docker Compose in the output.
Next, we will create a simple web application. We will use a basic Python Flask application. Create a directory for our project and navigate into it.
mkdir my-web-app
cd my-web-app
Inside the my-web-app
directory, create a file named app.py
with the following content:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Docker Compose!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
This is a simple Flask application that returns "Hello, Docker Compose!" when accessed.
Now, we need a Dockerfile
to build a Docker image for our Flask application. Create a file named Dockerfile
in the my-web-app
directory with the following content:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
This Dockerfile uses a slim Python 3.9 image, sets the working directory, copies and installs dependencies from requirements.txt
, copies the application code, exposes port 5000, and specifies the command to run the application.
We also need a requirements.txt
file for the Flask dependency. Create a file named requirements.txt
in the my-web-app
directory with the following content:
Flask==2.2.2
Finally, we will create a docker-compose.yml
file to define our service. Create a file named docker-compose.yml
in the my-web-app
directory with the following content:
version: "3.8"
services:
web:
build: .
ports:
- "5000:5000"
This docker-compose.yml
file defines a service named web
. It tells Docker Compose to build the image using the Dockerfile
in the current directory (.
) and map port 5000 on the host to port 5000 in the container.
Now, let's build and run the service using Docker Compose. Make sure you are in the ~/project/my-web-app
directory.
docker-compose up -d
This command builds the image (if not already built), creates a container for the web
service, and starts it in detached mode (-d
).
You can check if the container is running using the docker ps
command.
docker ps
You should see a container for the my-web-app-web-1
service running.
To verify that the application is working, you can access it using curl
.
curl http://localhost:5000
You should see the output "Hello, Docker Compose!".