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. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.
First, let's install Docker Compose. We will download the latest stable release 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
After the installation, verify the installation by checking the version.
docker-compose --version
You should see the version number printed in the output, confirming that Docker Compose is installed correctly.
Now, let's create a simple Docker Compose project. We will create a directory for our project and then create a docker-compose.yml
file inside it. This file will define a simple web service using the nginx
image.
Navigate to the project directory.
cd ~/project
Create a new directory for our project, for example, my-web-app
.
mkdir my-web-app
cd my-web-app
Now, create the docker-compose.yml
file using the nano
editor.
nano docker-compose.yml
Add the following content to the docker-compose.yml
file:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "80:80"
This docker-compose.yml
file defines one service named web
. This service uses the nginx:latest
Docker image and maps port 80 on the host to port 80 in the container.
Save the file by pressing Ctrl + X
, then Y
, and Enter
.
Now, we can start the services defined in the docker-compose.yml
file using the docker-compose up
command. The -d
flag runs the containers in detached mode, meaning they will run in the background.
docker-compose up -d
This command will pull the nginx:latest
image (if not already present) and start a container for the web
service.
You can check the status of the running containers using the docker ps
command.
docker ps
You should see a container named my-web-app_web_1
(or similar, depending on the directory name) running and forwarding port 80.
To verify that the web server is running, you can use curl
to access it.
curl http://localhost
You should see the default Nginx welcome page HTML in the output. This confirms that our simple Docker Compose project is set up and running correctly.