Prepare a simple docker-compose.yml file
In this step, we will prepare 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
Now, let's 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
.
Next, we will create a directory for our project and navigate into it.
mkdir ~/project/my-docker-app
cd ~/project/my-docker-app
Now, we will create a docker-compose.yml
file using the nano
editor. This file will define a simple web service using the nginx
image.
nano docker-compose.yml
In 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 Compose file format version.
services:
defines the services for our 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.
Save the file by pressing Ctrl + X
, then Y
, and then Enter
.
Before starting the service, we need to pull the nginx:latest
image.
docker pull nginx:latest
This command downloads the nginx:latest
image from Docker Hub.