List all services in a stack
In this step, you will learn how to list all services running within a Docker stack. Before we can list services, we need to have a stack running. Since Docker Compose is not pre-installed, we will first install it and then deploy a simple stack.
First, let's install Docker Compose. We will download the 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 that Docker Compose is installed, let's create a simple docker-compose.yml
file to define our stack. We will create a file named docker-compose.yml
in the ~/project
directory.
nano ~/project/docker-compose.yml
Add the following content to the file:
version: "3.8"
services:
web:
image: nginx:latest
ports:
- "80:80"
app:
image: alpine:latest
command: sleep 3600
This file defines a stack with two services: web
using the nginx
image and app
using the alpine
image. The web
service maps port 80 on the host to port 80 in the container. The app
service simply runs the sleep 3600
command to keep the container running.
Save the file and exit the editor (Ctrl+X, Y, Enter in nano).
Now, let's deploy the stack using docker stack deploy
. We will name our stack mystack
.
docker stack deploy -c ~/project/docker-compose.yml mystack
This command reads the docker-compose.yml
file and creates the services defined within it as a Docker stack named mystack
. You should see output indicating that the services are being created.
Once the stack is deployed, you can list all services in the stack using the docker stack services
command followed by the stack name.
docker stack services mystack
This command will display a table showing information about the services in the mystack
stack, including their ID, name, mode, replicas, image, and ports.
You should see output similar to this:
ID NAME MODE REPLICAS IMAGE PORTS
xxxxxxxxxxxx mystack_app replicated 1/1 alpine:latest
xxxxxxxxxxxx mystack_web replicated 1/1 nginx:latest *:80->80/tcp
The output lists the mystack_app
and mystack_web
services, confirming that they are running as part of the mystack
stack.