Create a simple docker-compose.yml file
In this step, we will create a simple docker-compose.yml
file. 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.
Before we create the docker-compose.yml
file, we need to install Docker Compose. Since it's not pre-installed in the LabEx environment, we will install it using pip
.
First, let's update the package list and install pip
if it's not already installed.
sudo apt update
sudo apt install -y python3-pip
Now, we can install Docker Compose using pip
.
pip install docker-compose
After the installation is complete, you can verify the installation by checking the version of Docker Compose.
docker-compose --version
You should see output similar to docker-compose version 1.29.2, build 5becea4c
.
Now that Docker Compose is installed, let's create a directory for our project and navigate into it. We will create the docker-compose.yml
file inside this directory.
mkdir ~/project/my-compose-app
cd ~/project/my-compose-app
Next, we will create the docker-compose.yml
file using the nano
editor.
nano docker-compose.yml
Inside the nano
editor, paste the following content. This docker-compose.yml
file defines a single service named web
that uses the nginx
image and maps port 80 of the container to port 8080 on the host machine.
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "8080:80"
Let's break down this file:
version: '3.8'
specifies the Compose file format version.
services:
defines the different services that make up your application.
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. We will pull this image later when we start the service.
ports:
maps ports between the host and the container.
- "8080:80"
maps port 80 inside the container (where Nginx runs by default) to port 8080 on your host machine. This means you can access the Nginx web server by visiting http://localhost:8080
in your web browser (or using curl
from the terminal).
Save the file by pressing Ctrl + X
, then Y
, and then Enter
.
You can verify the content of the file using the cat
command.
cat docker-compose.yml
You should see the YAML content you just pasted.