This content refers to:
Docker version 28.2.2, build e6534b4
Docker Compose version v2.36.2
Under Ubuntu 24.04.2 LTS.
- Problem: Environment file is not used for credentials
- Problem: Resource still in use
- How-to: Optimizing container size
- How-to: Pin specific image versions
- Useful Commands
The database container cannot be started properly because:
[Warning] Access denied for user 'root'@'localhost' (using password: NO)
This is a well-documented and frustrating issue with MariaDB (and MySQL) Docker images: if the data directory (/var/lib/mysql or your bind mount) is not empty, the MariaDB container will ignore the environment variables for root password and database/user creation. This means you can have a correct .env file, but if the data directory already has content (even partial or from a failed init), the entrypoint script will skip initialization and ignore your env vars.
sudo rm -rf /mariadb-data/*
If you are not using a volume or bind mount, MariaDB's data is stored inside the container. To fully reset:
docker compose down
docker compose down --rmi all
If you ever attach a named volume or bind mount for persistence, add --volumes to remove those as well:
docker compose down --volumes
Sometimes, containers can get stuck in a "removal in progress" or "dead" state, which prevents image deletion even though they don't show up in "docker ps -a".
See the cleanup script:
../scripts/docker-helper/delete-orphanes.sh
Otherwise, the following cmds might help for specific cases
List dangling (unused) volumes:
docker volume ls -f dangling=true
Remove all unused volumes:
docker volume prune
Remove all stopped containers:
docker container prune
Remove all unused networks:
docker network prune
Remove dangling image:
docker image prune
Remove all unused images:
docker image prune -a
WARNING this will delete all images
and perform full system cleanup (last resort!):
docker system prune -a
Use multistage Dockerfiles. See: https://docs.docker.com/build/building/multi-stage/
Jar file should be run with an JRE instead of JDK.
Here are some options:
| Image Type | Typical Size | Notes |
|---|---|---|
| openjdk:21-jdk-slim | ~300MB+ | Full JDK, not minimal |
| eclipse-temurin:21-jre | ~100-200MB | JRE only, smaller than JDK |
| eclipse-temurin:21-jre-alpine | ~50-100MB | Smallest official JRE, if available |
| Custom JRE (JLink) | 50MB or less | Smallest possible, only needed modules |
| BellSoft Alpaquita, Corretto Alpine | ~40-90MB | Vendor-optimized, very small |
See slim spring boot container:
https://github.com/mgrecuccio/slim-spring-boot
Use the script to get the digest hash under:
/scripts/docker-helper/image-digest.sh
Pining works in compose.yaml and Dockerfile.
In the Dockerfile:
FROM openjdk@sha256:7072053847a8a05d7f3a14ebc778a90b38c50ce7e8f199382128a53385160688 as builder
instead of just writing:
FROM openjdk:21-jdk-slim
Same thing in the compose.yaml:
image: mariadb@sha256:1d18f91deb21136d1881705720071d1b474a9904ecca827058bf1c0fc64d3118
instead of:
image:mariadb:11.4.7
Access container
docker exec -it <container_id> /bin/bash
Stopping all containers
docker stop $(docker ps -a -q)
Removing all containers
docker rm $(docker ps -a -q)
Starts all services from compose
docker compose up -d
Show Config
docker compose config
Delete Orphans
docker compose down --remove-orphans
Build with no cache to execute all steps fresh
docker compose build --no-cache
Prune old networks that cause issues
docker compose down -v
docker network prune