Prepare a simple Compose file with multiple services
In this step, we will prepare a simple Docker Compose file that defines multiple services. Docker Compose is a tool that allows you to define and run multi-container Docker applications. While Docker is pre-installed on this VM, Docker Compose is not. We will install it first.
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, we will create a directory for our project and navigate into it.
mkdir ~/project/my-compose-app
cd ~/project/my-compose-app
Now, we will create a docker-compose.yml
file in this directory. This file will define our services. We will use the nano
editor to create and edit the file.
nano docker-compose.yml
Inside the nano
editor, paste the following content. This file defines two services: web
and redis
. The web
service uses the nginx
image and maps port 80 of the container to port 8080 on the host. The redis
service uses the redis
image.
version: "3.8"
services:
web:
image: nginx
ports:
- "8080:80"
redis:
image: redis
After pasting the content, save the file by pressing Ctrl + X
, then Y
, and finally Enter
.
Before we can start the services, we need to pull the images defined in the docker-compose.yml
file. We can do this using the docker pull
command.
docker pull nginx
docker pull redis
These commands will download the nginx
and redis
images from Docker Hub.