All I wanted was to launch a nice little Strapi inside a Docker container.
I type docker-compose up
... and BAM โ Strapi v3 ๐ฎ.
โButโฆ I asked for Strapi 4?!โ
Spoiler: the official Docker image strapi/strapi
โ it's version 3 (and it's old ๐
).
Don't panic. Let me show you how to properly install Strapi v4 in Docker, without losing your sanity.
Letโs go ๐
๐ฏ Why doesnโt it work out of the box?
The Docker Hub image strapi/strapi
was never updated for Strapi v4.
โ It always gives you Strapi v3.6.8 (RIP ๐ชฆ).
So if you want Strapi v4 + Docker โ you gotta cheat a bit.
No worries, we love cheating in dev ๐.
๐๏ธ The correct method โ use Node.js as base + mount your Strapi project.
Instead of asking for a magic โstrapi v4โ image,
โ create your Strapi v4 project locally
โ and run it inside a Node.js container.
Easy.
โ 1๏ธโฃ Create your Strapi v4 project locally
npx create-strapi@latest app
(or yarn create strapi@latest app
)
Boom, youโve got a fresh ./app
folder with Strapi.
โ
2๏ธโฃ Write this magical docker-compose.yml
version: '3'
services:
strapi:
image: node:18-alpine
container_name: techlink-strapi
working_dir: /srv/app
command: sh -c "yarn install && yarn build && yarn start"
environment:
DATABASE_CLIENT: postgres
DATABASE_NAME: strapi
DATABASE_HOST: postgres
DATABASE_PORT: 5432
DATABASE_USERNAME: strapi
DATABASE_PASSWORD: strapi123
NODE_ENV: production
ports:
- '1337:1337'
volumes:
- ./app:/srv/app
depends_on:
- postgres
postgres:
image: postgres:15
container_name: techlink-postgres
restart: always
environment:
POSTGRES_USER: strapi
POSTGRES_PASSWORD: strapi123
POSTGRES_DB: strapi
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
โ ๏ธ Important โ the ./app
folder must contain your pre-created Strapi v4 project (see step 1).
โ 3๏ธโฃ Launch Docker, captain
docker-compose up -d
๐ Done!
Your Strapi is available at:
๐ http://localhost:1337/admin
(and this time, itโs Strapi v4, not v3 ๐)
๐ต๏ธ Why does it work?
- We use node:18-alpine as the engine
- We mount your local Strapi project inside the container
- The container installs, builds, starts everything
No ghost official image needed.
YOU are the official image now ๐
๐ฅ Bonus option โ a Dockerfile if you want a real Docker build
Want a โclosedโ image? Add a Dockerfile
:
FROM node:18-alpine
WORKDIR /srv/app
COPY . .
RUN yarn install
RUN yarn build
EXPOSE 1337
CMD ["yarn", "start"]
Then adapt your docker-compose.yml
:
strapi:
build: .
ports:
- '1337:1337'
depends_on:
- postgres
environment:
DATABASE_CLIENT: postgres
DATABASE_NAME: strapi
DATABASE_HOST: postgres
DATABASE_PORT: 5432
DATABASE_USERNAME: strapi
DATABASE_PASSWORD: strapi123
โ Docker will build it all in one go.
๐ TL;DR
โ
strapi/strapi
= Strapi v3 (DONโT use it)
โ
Create your Strapi v4 project locally
โ
Use node:18-alpine
+ mounted volume
โ
Or build a custom Dockerfile
There you go.
Now you can proudly say:
๐ โYes, I have Strapi v4 running in Docker. Not v3. No cheating.โ
๐ฌ Need help with permissions, relations, API? Ping me ๐
Or share this article with your buddy struggling on Stack Overflow ๐ซ
TechLink powered. Peace โ๏ธ