Prepare a simple docker-compose.yml file
In this step, we will prepare a simple docker-compose.yml
file. Before we start, let's understand what Docker Compose is. 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.
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 that Docker Compose is installed, let's create a simple docker-compose.yml
file in the ~/project
directory. This file will define a single service that uses the ubuntu
image and simply runs the sleep infinity
command to keep the container running.
We will use the nano
editor to create and edit the file.
nano ~/project/docker-compose.yml
In the nano
editor, paste the following content:
version: "3.8"
services:
ubuntu_service:
image: ubuntu
command: sleep infinity
Let's break down this docker-compose.yml
file:
version: '3.8'
specifies the Compose file format version.
services:
defines the services for your application.
ubuntu_service:
is the name of our service. You can choose any name you like.
image: ubuntu
specifies the Docker image to use for this service. In this case, we are using the official ubuntu
image. Since the image might not be present locally, Docker Compose will automatically pull it if needed.
command: sleep infinity
specifies the command to run when the container starts. sleep infinity
is a simple command that keeps the container running indefinitely.
After pasting the content, save the file by pressing Ctrl + X
, then Y
to confirm, and Enter
to save to the default filename docker-compose.yml
.
To ensure the ubuntu
image is available, let's manually pull it using the docker pull
command. This is a good practice to ensure the image is ready before starting the service with Docker Compose.
docker pull ubuntu
You should see output indicating that the ubuntu
image is being pulled.