Prepare a simple docker-compose.yml file
In this step, we will prepare a simple docker-compose.yml
file. Before we start, let's install Docker Compose. Since the LabEx VM environment does not have Docker Compose pre-installed, we need to install it manually. We will download the Docker Compose binary and make it executable.
First, download the Docker Compose binary using curl
. We will download version 1.29.2
, which is compatible with the installed Docker version.
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
This command downloads the Docker Compose binary from the official GitHub releases page and saves it to /usr/local/bin/docker-compose
. The $(uname -s)
and $(uname -m)
parts automatically detect your operating system and architecture, ensuring you download the correct binary.
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
file, allowing you to run it as a command.
Now, let's verify that Docker Compose is installed correctly by checking its version.
docker-compose --version
You should see output similar to docker-compose version 1.29.2, build 5becea4c
. This confirms that Docker Compose is installed and ready to use.
Now, let's create a simple docker-compose.yml
file in your ~/project
directory. This file will define a single service using the nginx
image.
nano ~/project/docker-compose.yml
This command opens the nano
text editor to create and edit the docker-compose.yml
file. Paste the following content into the editor:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "80:80"
Let's break down this docker-compose.yml
file:
version: '3.8'
specifies the Docker Compose file format version.
services:
defines the services (containers) that you want to run.
web:
is the name of our service. You can choose any name you like.
image: nginx:latest
specifies the Docker image to use for this service. In this case, we are using the latest version of the official Nginx image.
ports:
maps ports between the host machine and the container. "80:80"
maps port 80 on the host to port 80 in the container. This means you can access the Nginx web server running inside the container by visiting http://localhost
in your web browser (or the VM's IP address).
Save the file by pressing Ctrl + O
, then press Enter
, and exit the editor by pressing Ctrl + X
.
You have now successfully created a simple docker-compose.yml
file that defines a web service using the Nginx image. In the next steps, we will use this file to create and manage containers.