When to use this stack
This stack runs a Strapi 5 headless content management system with PostgreSQL 16 as its database. Strapi gives you a web admin panel for content types, media and roles, and a REST or GraphQL API that a website, mobile app or dashboard can read. Keeping the CMS and its database in one compose file makes the setup repeatable: the Strapi build, the database credentials and the secret keys all come from the same project, and a fresh server can be ready after one command.
Choose it when editors need a visual admin area and developers want to define content types in code, with a database that can handle growing data. It suits marketing sites that consume content through an API, multi-language catalogues and internal publishing tools. Do not choose it when a fixed set of pages or a simple blog is enough, because a static site generator or a hosted CMS needs less maintenance. Strapi also requires care with secrets: the admin panel and the API both depend on the salts and keys in the environment, and losing them invalidates sessions and tokens.
The Dockerfile builds the admin panel with npm run build and starts Strapi with npm run start. Your ./app folder must contain a Strapi project with package.json, package-lock.json and a pg dependency, because the image does not add the PostgreSQL driver for you. Uploaded media is stored in public/uploads inside the project, so this stack mounts a named volume there to keep files across rebuilds.
docker-compose.yml
services:
app:
build: ./app
restart: unless-stopped
depends_on:
db:
condition: service_healthy
ports:
- "1337:1337"
environment:
NODE_ENV: production
HOST: 0.0.0.0
PORT: 1337
DATABASE_CLIENT: postgres
DATABASE_HOST: db
DATABASE_PORT: 5432
DATABASE_NAME: ${POSTGRES_DB}
DATABASE_USERNAME: ${POSTGRES_USER}
DATABASE_PASSWORD: ${POSTGRES_PASSWORD}
DATABASE_SSL: "false"
APP_KEYS: ${APP_KEYS}
API_TOKEN_SALT: ${API_TOKEN_SALT}
ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}
TRANSFER_TOKEN_SALT: ${TRANSFER_TOKEN_SALT}
JWT_SECRET: ${JWT_SECRET}
volumes:
- strapi_uploads:/opt/app/public/uploads
db:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
interval: 10s
timeout: 5s
retries: 5
volumes:
postgres_data:
strapi_uploads:# PostgreSQL database, user and password
POSTGRES_DB=strapi
POSTGRES_USER=strapi
POSTGRES_PASSWORD=CHANGE_ME_postgres_password
# Two or more comma-separated random keys, for example from: openssl rand -base64 16
APP_KEYS=CHANGE_ME_key_one,CHANGE_ME_key_two
# Random salts and secrets, one value each
API_TOKEN_SALT=CHANGE_ME_api_token_salt
ADMIN_JWT_SECRET=CHANGE_ME_admin_jwt_secret
TRANSFER_TOKEN_SALT=CHANGE_ME_transfer_token_salt
JWT_SECRET=CHANGE_ME_users_jwt_secretDockerfile (app/Dockerfile)
Place this file in the ./app folder, which is the Strapi project root. Add a .dockerignore with node_modules, .env, .tmp and build.
FROM node:22-alpine
WORKDIR /opt/app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
ENV NODE_ENV=production
EXPOSE 1337
CMD ["npm", "run", "start"].env
# PostgreSQL database, user and password
POSTGRES_DB=strapi
POSTGRES_USER=strapi
POSTGRES_PASSWORD=CHANGE_ME_postgres_password
# Two or more comma-separated random keys, for example from: openssl rand -base64 16
APP_KEYS=CHANGE_ME_key_one,CHANGE_ME_key_two
# Random salts and secrets, one value each
API_TOKEN_SALT=CHANGE_ME_api_token_salt
ADMIN_JWT_SECRET=CHANGE_ME_admin_jwt_secret
TRANSFER_TOKEN_SALT=CHANGE_ME_transfer_token_salt
JWT_SECRET=CHANGE_ME_users_jwt_secretStart and verify
docker compose config --quiet
# Build the Strapi image (this includes the admin panel build) and start
docker compose up -d --build
# db must be healthy before app starts
docker compose ps
# Check the log for "Strapi started" and any database errors
docker compose logs --tail=80 app
# The admin page and the API root should answer
curl -I http://localhost:1337/admin
curl -I http://localhost:1337/apiOpen http://localhost:1337/admin on the first run to create the first administrator.
Production checklist
- Put Strapi behind HTTPS with a reverse proxy, and set the public URL in the proxy headers so generated media links are correct.
- Generate every key and salt with a random generator, keep them out of Git, and rotate them only with a plan, because rotation signs everyone out.
- Keep DATABASE_SSL false only on the internal network. If the database is remote, enable SSL and set the certificate options.
- Restrict API permissions per role in the admin panel. Public access should be read-only and limited to published content.
- Back up postgres_data with pg_dump and archive strapi_uploads separately, because media files are not in the database.
- Pin node:22-alpine and postgres:16-alpine, and test Strapi upgrades on a copy first.
- Set a rate limit on /admin and /api at the proxy to slow brute-force attempts.
- Use an email provider for password resets and configure it in config/plugins.
Common problems
- Symptom: The build fails with “Cannot find module ‘pg’”. Cause: The PostgreSQL driver is not in package.json. Fix: Run npm install pg in ./app, commit package-lock.json and run docker compose up -d –build.
- Symptom: Strapi exits with “Missing APP_KEYS” or a similar message. Cause: One of the secret variables is empty or missing from the environment block. Fix: Check that every variable in .env is also listed in the app environment section, then recreate the container.
- Symptom: The admin panel says the session is invalid after a restart. Cause: The JWT secrets changed between runs. Fix: Keep the same values in .env and do not regenerate them on each deploy.
- Symptom: Uploaded images return 404 after a rebuild. Cause: The uploads folder was not mounted as a volume. Fix: Confirm that strapi_uploads is mounted at /opt/app/public/uploads, and copy the old files into the new volume if you had them elsewhere.
- Symptom: Port 1337 answers with a connection refused error. Cause: Strapi listens on 127.0.0.1 inside the container. Fix: Set HOST to 0.0.0.0 as in the compose file.
Variations
- Development mode with live reload: Run npm run develop on the host with the database published only on 127.0.0.1, or use a compose.dev.yml that mounts ./app. Keep the production image separate.
- MySQL instead of PostgreSQL: Set DATABASE_CLIENT to mysql, use the mysql:8.4 image, and keep the same DATABASE_* names. Install the mysql2 package in the project.
- Object storage for media: Install the AWS S3 upload provider, store media in a bucket, and remove the strapi_uploads volume. This is better when several app containers must share uploads.
Questions
Is there an official Strapi Docker image?
There is no official image maintained by Strapi that this file uses. The compose file builds your own Strapi project from ./app with a Node 22 Alpine base image, which is the approach Strapi documents.
Why does Strapi fail with a missing pg module?
Strapi uses the pg package for PostgreSQL but it is not always installed by default in every project. Run npm install pg in ./app, commit the lock file and rebuild the image.
How do I generate APP_KEYS and the salts?
Run openssl rand -base64 16 once per value. APP_KEYS takes two or more comma-separated keys, and each salt or secret needs its own random value.
Adapt this stack
Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.
Open in builderRelated stacks
Laravel + PostgreSQL + Redis
Run a Laravel app on Apache with PostgreSQL 16 and Redis 7 in Docker Compose, with a Dockerfile, queue and cache settings, volumes and a production checklist.
Next.js + PostgreSQL + Redis
Containerize a Next.js app with PostgreSQL 16 and Redis 7 in Docker Compose, using a minimal Dockerfile, healthchecks, named volumes and a production checklist.
Nuxt + PostgreSQL
Run a Nuxt 3 app built with Nitro and PostgreSQL 16 in Docker Compose, with a Node 22 Dockerfile, a database healthcheck, persistent data and a checklist.
Spring Boot + MariaDB
Run a Spring Boot 3 API on Java 21 with MariaDB 11.4 in Docker Compose, with a JRE image, datasource variables, a database healthcheck and a checklist.
Browse more Web & Frameworks stacks or the full library.