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 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 output similar to Docker Compose version v2.20.2
.
Next, we will 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 service using the ubuntu
image.
mkdir ~/project/my-compose-app
cd ~/project/my-compose-app
nano docker-compose.yml
In the nano
editor, paste the following content into the docker-compose.yml
file:
version: "3.8"
services:
my-service:
image: ubuntu:latest
command: tail -f /dev/null
Press Ctrl + X
, then Y
, and Enter
to save and exit the editor.
Let's break down the docker-compose.yml
file:
version: '3.8'
specifies the Docker Compose file format version.
services:
defines the services that make up your application.
my-service:
is the name of our service.
image: ubuntu:latest
specifies the Docker image to use for this service. We are using the latest version of the Ubuntu image.
command: tail -f /dev/null
is the command that will be executed when the container starts. This command keeps the container running indefinitely without consuming significant resources, which is useful for testing and debugging.
Before starting the service, we need to pull the ubuntu:latest
image.
docker pull ubuntu:latest
Now, we can start the service 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
You should see output indicating that the service is being created and started.
Finally, let's check the status of the running service using the docker-compose ps
command.
docker-compose ps
You should see output showing that the my-service
container is running.