Create a simple docker-compose.yml file
In this step, we will create a basic docker-compose.yml
file. This file is the core of Docker Compose, defining the services, networks, and volumes for your application. Before we start, we need to install Docker Compose as it is not pre-installed in this environment.
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
Now, let's verify the installation by checking the version.
docker-compose --version
You should see the version information printed to the console, confirming that Docker Compose is installed correctly.
Next, navigate to the ~/project
directory, which is your working directory for this lab.
cd ~/project
Now, we will create a file named docker-compose.yml
in this directory. This file will define a simple service using the nginx
image. We will use the nano
editor 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"
Let's break down this docker-compose.yml
file:
version: '3.8'
specifies the Docker Compose file format version.
services:
defines the different services that make up your application.
web:
is the name of our service. You can name your services anything 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. Since this image might not be present locally, Docker Compose will automatically pull it from Docker Hub when you start the service.
ports:
maps ports between the host machine and the container. "80:80"
maps port 80 on the host to port 80 on the container. This means you will be able to access the Nginx web server running inside the container by visiting http://localhost
(or the VM's IP address) on your host machine.
After pasting the content, save the file by pressing Ctrl + O
, then press Enter
to confirm the filename, and finally press Ctrl + X
to exit the nano
editor.
You have now successfully created your first docker-compose.yml
file. In the next step, we will use this file to start the Nginx service.