Stacks
A stack is a set of containers that belong together, described by one Compose file and managed as one thing: deployed together, listed together, removed together. This chapter deploys the guide's practice stack — an invented pair of services called storybook — and takes it away again at the end of the guide's arc (transcript t09), so you see the whole life of a stack in honest conditions.
Check: after deploying, the stack
list shows storybook, status 1
(active), and the container list shows
its two containers running; the web
service answers a real request.
Transcript
portainer-lab/transcripts/t04-stack-deploy.txt
has all three.
The practice stack
The lab's file is kept at
portainer-lab/stack.yaml:
name: storybook
services:
story-web:
image: nginx:alpine
ports:
- "8080:80"
story-cache:
image: redis:alpine
volumes:
- story-data:/data
volumes:
story-data:
A web server with its port published,
a cache with its data in a named
volume, nothing real at stake —
which is what practice stacks are for.
The file the lab actually deployed
(stack-lab.yaml) differs by one
idea: the sandbox cannot create
network namespaces, so its services
share the host network instead of
publishing a port. On any ordinary
engine, use the file above exactly as
written; transcript t90 explains the
sandbox, and chapter 9 returns to what
the difference means.
Both files were checked with a real Compose validator before use (transcript t00) — a habit worth stealing, since a stack that fails to parse fails in Portainer too, just further from the cause.
Three doors into the same room
Portainer offers three ways to create a stack, and they are the same stack at the end:
- The web editor. Stacks → Add stack, a name, and paste the Compose file into the editor. The friendly door, and the one to learn on.
- Upload and Git. The same form takes an uploaded file, or the address of a Git repository to pull the file from — the door for stacks whose files live in version control (described, not run, in this guide's lab).
- The API. What the lab used:
POST /api/stacks/create/standalone/string, carrying the name and the whole file as text (transcript t04). Scripted deploys and this guide's evidence live here.
Whichever door you use, Portainer
files the project under its data
volume (/data/compose/<id>) and
remembers the stack as its own —
editing it later happens in
Portainer, not by editing your
original file behind its back.
What deploying does
Portainer hands the file to its
Compose machinery, which pulls the
images, creates the stack's own
network (chapter 9), the named
volumes, and the containers —
conventionally named
<stack>-<service>-1, so the lab's
list shows storybook-story-web-1 and
storybook-story-cache-1. The stack
record that comes back names its type
(2: a standalone Compose stack, not
Swarm), its environment, and its
status. The lab then did the only
check that finally matters: it asked
the web service for its page, and got
the web server's welcome — the stack
was not just recorded, it was serving.
Variables, and stacks that change
The stack form has a table for
environment variables, values the
Compose file can reference as
${NAME} — the right home for the
port that differs per machine, and the
wrong home for secrets you would not
write on the office wall; variables
are visible to anyone who can view
the stack. Editing a stack's file in
Portainer and redeploying updates the
set in place; containers whose
definition changed are recreated,
which is a small, planned outage of
exactly those services.
Removing a stack
Stacks → the stack → Remove (the API:
DELETE /api/stacks/<id>, transcript
t09) deletes the stack's containers
and its network — and leaves its
named volumes standing, data and
all. That asymmetry is deliberate and
correct: removing an application
should not quietly shred its data.
Chapter 9 shows the volume that
survived the lab's stack removal, and
how to delete a volume when you
actually mean it.