Ubuntu example:
Warning: you may have to uninstall OS defaults before:
sudo apt-get remove docker docker-engine docker.io containerd runc curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
A docker example with a multi-stage approach to minimize image size and build time (taking advantage of buildx cache).
.
├── apps
│ └── nextjs-app
├── packages
│ ├── core-lib
│ ├── db-main-prisma
│ └── ui-lib
├── static
│ ├── assets
│ └── locales
├── .dockerignore
├── docker-compose.nextjs-app.yml (specific for nextjs-app)
├── docker-compose.yml (general services like postgresql...)
└── Dockerfile (multistage build for nextjs-app)
- docker-engine >= 20.10.0
- docker-compose >= 1.29.0
- docker buildkit enabled.
- optional: lazydocker, a beautiful tui.
- optional: dive to debug layer sizes.
Note: Be sure to create a .dockerignore containing at least those entries.
Yarn script | Description |
---|---|
yarn docker:nextjs-app:develop |
Run apps/nextjs-app in development mode |
yarn docker:nextjs-app:install |
Install dependencies in cache mount |
yarn docker:nextjs-app:build |
Create a production build |
yarn docker:nextjs-app:serve |
Serve production build on localhost:3000, |
yarn docker:prune-cache |
Run this regularly if using in local !!! |
Build and serve commands requires to have a
./apps/nextjs-app/.env.local
present.
yarn docker:nextjs-app:develop
# Or alternatively
DOCKER_BUILDKIT=1 docker-compose -f ./docker-compose.yml -f ./docker-compose.nextjs-app.yml up develop main-db
Want to open a shell to debug ?
DOCKER_BUILDKIT=1 docker-compose -f ./docker-compose.nextjs-app.yml run --rm develop sh
See the latest ./docker-compose.nextjs-app.yml and ./Dockerfile.
PS: The goal of multistage is mainly to reduce the size of the resulting image, it also allows to skip deps stage (ie: install deps) when no changes are detected in your deps (lock file).
This stage will install the monorepo and make all node_modules folders available in later stages.
Some commands
To build it independently
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --progress=tty deps
# docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty deps
To force a rebuild
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --no-cache --force-rm --progress=tty deps
Want to open a shell into it ?
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml run --rm deps sh
This stage will automatically run the deps stage and copy all installed node_modules folder. Then build the thing and remove devDependencies.
PS: You'll have to send some build-args (env variables) in order to have a real build.
Some commands
To build it independentlyDOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --progress=tty builder
# docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty builder
To force a rebuild
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --no-cache --force-rm --progress=tty builder
Want to open a shell into it ?
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml run --rm builder sh
Launch a production build and listen by default to http://localhost:3000.
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml --env-file .env.secret up runner
PS: you'll have to provide your own .env with required runtime variables.
Some commands
To build it independentlyDOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --progress=tty runner
# docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty runner
To force a rebuild
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --no-cache --force-rm --progress=tty runner
Want to open a shell into it ?
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml run --rm runner sh
Option | Command |
---|---|
Prune buildx | docker buildx prune |
Prune cachemount caches | docker builder prune --filter type=exec.cachemount |
Remove all containers | docker container rm -f $(docker container ls -qa) |
Clean all images | docker image rm -f $(docker image ls -q) |
Remove all volumes | docker volume rm $(docker volume ls -q) |
Like to remove all docker layers, overlays... Warning you'll lose all your data.
systemctl docker stop
rm -rd /var/lib/docker
systemctl docker start