Deploy a sample stack
In this step, you will learn how to deploy a sample stack using 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.
Since Docker Compose is not pre-installed in the LabEx environment, you need to install it first. We will install Docker Compose version 1.29.2, which is compatible with the installed Docker version.
First, download the Docker Compose binary:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
This command downloads the Docker Compose binary from the official GitHub repository and saves it to /usr/local/bin/docker-compose
. The $(uname -s)
and $(uname -m)
parts automatically detect your operating system and architecture.
Next, apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
This command makes the downloaded file executable.
Now, verify the installation by checking the Docker Compose version:
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 simple Docker Compose file to define our stack. We will create a file named docker-compose.yml
in the ~/project
directory.
Use the nano
editor to create and edit the file:
nano ~/project/docker-compose.yml
Paste the following content into the docker-compose.yml
file:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "80:80"
app:
image: alpine:latest
command: echo "Hello from Alpine"
This docker-compose.yml
file defines two services:
web
: Uses the nginx:latest
image and maps port 80 on the host to port 80 in the container.
app
: Uses the alpine:latest
image and runs a simple echo
command.
Save the file by pressing Ctrl + S
and exit the editor by pressing Ctrl + X
.
Before deploying the stack, let's pull the necessary images. While Docker Compose can pull images automatically during deployment, explicitly pulling them beforehand can sometimes be helpful.
Pull the nginx:latest
image:
docker pull nginx:latest
Pull the alpine:latest
image:
docker pull alpine:latest
Now, deploy the stack using the docker-compose up
command. The -d
flag runs the containers in detached mode (in the background).
Navigate to the ~/project
directory where you created the docker-compose.yml
file:
cd ~/project
Then, run the command:
docker-compose up -d
This command reads the docker-compose.yml
file and creates and starts the defined services. You should see output indicating the creation and starting of the web
and app
services.
To verify that the services are running, you can list the running containers:
docker ps
You should see two containers running, one for the web
service (based on nginx
) and one for the app
service (based on alpine
).