Create a simple docker-compose.yml file
In this step, we will create a simple docker-compose.yml
file. Before we start, we need to install Docker Compose. 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
After the installation, you can verify the installation by checking the version of Docker Compose.
docker-compose --version
You should see output similar to Docker Compose version v2.20.2
.
Now, let's create a directory for our project and navigate into it.
mkdir my-docker-app
cd my-docker-app
Inside the my-docker-app
directory, we will create a file named docker-compose.yml
. This file will define the services for our application. We will use the nano
editor to create and edit this file.
nano docker-compose.yml
In the nano
editor, paste the following content. This docker-compose.yml
file defines a single service named web
that uses the nginx:latest
image.
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "80:80"
Let's break down this file:
version: '3.8'
specifies the Docker Compose file format version.
services:
defines the services that make up your application.
web:
is the name of our service.
image: nginx:latest
specifies the Docker image to use for this service. In this case, we are using the latest version of the Nginx image.
ports:
maps ports between the host and the container. "80:80"
maps port 80 on the host to port 80 on the container.
After pasting the content, save the file by pressing Ctrl + X
, then Y
, and finally Enter
.
Before starting the service, we need to make sure the nginx:latest
image is available locally. If it's not, Docker Compose will automatically pull it when you start the service. However, you can also manually pull the image using the docker pull
command.
docker pull nginx:latest
This command downloads the nginx:latest
image from Docker Hub.