Prepare a simple Docker Compose project
In this step, we will prepare a simple Docker Compose project. 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.
Since Docker Compose is not pre-installed in the LabEx environment, we need to install it first. We will download the Docker Compose binary and make it executable.
First, let's download the Docker Compose binary. We will use curl
to download the latest stable release.
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
This command downloads the Docker Compose binary for your system architecture and saves it to /usr/local/bin/docker-compose
.
Next, we need to make the downloaded binary executable.
sudo chmod +x /usr/local/bin/docker-compose
This command adds execute permissions to the Docker Compose binary.
Now, let's verify the installation by checking the Docker Compose version.
docker-compose --version
You should see the installed Docker Compose version in the output, confirming that the installation was successful.
Now that Docker Compose is installed, 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.
First, create a directory named my-compose-app
in your home directory.
mkdir ~/project/my-compose-app
Navigate into the newly created directory.
cd ~/project/my-compose-app
Now, we will create a docker-compose.yml
file. This file will define the services for our application. We will use nano
to create and edit the file.
nano docker-compose.yml
Inside the nano
editor, paste the following content:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "80:80"
app:
image: ubuntu:latest
command: tail -f /dev/null
Let's break down this docker-compose.yml
file:
version: '3.8'
specifies the Docker Compose file format version.
services:
defines the services that make up your application.
web:
defines a service named web
.
image: nginx:latest
specifies that this service will use the nginx:latest
Docker image. If the image is not available locally, Docker Compose will pull it from Docker Hub.
ports:
maps port 80 on the host machine to port 80 on the web
container.
app:
defines a service named app
.
image: ubuntu:latest
specifies that this service will use the ubuntu:latest
Docker image.
command: tail -f /dev/null
specifies the command to run when the app
container starts. This command keeps the container running indefinitely without exiting.
Save the file by pressing Ctrl + X
, then Y
, and then Enter
.
Before we can start the services, we need to make sure the required Docker images are available. Although Docker Compose will pull images if they are not present, it's good practice to explicitly pull them beforehand, especially in environments with limited internet access or for faster startup.
Let's pull the nginx:latest
image.
docker pull nginx:latest
This command downloads the nginx:latest
image from Docker Hub.
Next, let's pull the ubuntu:latest
image.
docker pull ubuntu:latest
This command downloads the ubuntu:latest
image from Docker Hub.
Now you have successfully prepared a simple Docker Compose project with two services, web
and app
, and pulled the necessary Docker images.