Docker Compose Examples

Organize Docker compose.yaml examples here.

Postgres

Postgres DockerHub

Run Postgres with Docker Compose

compose.yaml

services:
postgres1:
image: 'postgres:17-alpine'
container_name: 'postgres1'
ports:
- '${POSTGRES_PORT}:5432'
environment:
- TZ=Asia/Shanghai
# Note: Password can only be set on first startup. You can delete the volume to set a new password.
- 'POSTGRES_PASSWORD=${POSTGRES_PASSWORD}'
volumes:
- 'postgres_data:/var/lib/postgresql/data'
restart: unless-stopped

volumes:
postgres_data:
driver: local

Verify

mkdir my-app && cd my-app
vim compose.yaml
# paste the compose.yaml. Save and exit.

# set temporary environment variables
export POSTGRES_PORT=15432 && export POSTGRES_PASSWORD=STRONG_PASSWORD
# start docker compose
docker compose up -d
docker compose logs -f postgres1
# verify
docker compose exec -it postgres1 psql -V
docker compose exec -it postgres1 psql -U postgres

Run Postgres with docker run command

docker run -d \
--name postgres1 \
-p 5432:5432 \
-e POSTGRES_PASSWORD=STRONG_PASSWORD \
-e TZ=Asia/Shanghai \
-v postgres_data:/var/lib/postgresql/data \
--restart always \
postgres

MySQL

MySQL server DockerHub

MySQL server docker environment variables

Run MySQL with Docker Compose

compose.yaml

services:
mysql1:
image: 'mysql/mysql-server:8.0'
container_name: 'mysql1'
ports:
- '${MYSQL_PORT}:3306'
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_HOST=%
- 'MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}'
volumes:
- 'mysql_data:/var/lib/mysql'
restart: on-failure

volumes:
mysql_data:
driver: local

Verify

mkdir my-app && cd my-app
vim compose.yaml
# paste the compose.yaml. Save and exit.

# set temporary environment variables
export MYSQL_PORT=13306 && export MYSQL_PASSWORD=STRONG_PASSWORD
# start docker compose
docker compose up -d
docker compose logs -f mysql1
# verify
docker compose exec -it mysql1 mysql -V
docker compose exec -it mysql1 mysql -uroot -p

Run MySQL with docker run command

docker run --name=mysql1 -d -p 3306:3306 \
-e TZ=Asia/Shanghai -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD=STRONG_PASSWORD \
--restart on-failure \
-v mysql_data:/var/lib/mysql \
mysql/mysql-server:8.0

Redis

Redis DockerHub

Run Redis with Docker Compose

compose.yaml

services: 
redis1:
image: 'redis:7-alpine'
container_name: 'redis1'
ports:
- '${REDIS_PORT}:6379'
volumes:
- 'redis_data:/data'
command: 'redis-server --save 20 1 --loglevel warning --requirepass ${REDIS_PASSWORD}'
restart: unless-stopped

volumes:
redis_data:
driver: local

Verify

mkdir my-app && cd my-app
vim compose.yaml
# paste the compose.yaml. Save and exit.

# set temporary environment variables
export REDIS_PORT=16379 && export REDIS_PASSWORD=STRONG_PASSWORD
# start docker compose
docker compose up -d
docker compose logs -f redis1
# verify
docker compose exec -it redis1 redis-server -v
docker compose exec -it redis1 redis-cli

Run Redis with docker run command

docker run --name redis1 -d -p 16379:6379 \
-v redis_data:/data \
redis \
redis-server \
--save 60 1 \
--loglevel warning \
-requirepass STRONG_PASSWORD

Nginx

Nginx DockerHub

Run Nginx with Docker Compose

services:
nginx1:
image: 'nginx:stable-alpine'
container_name: 'nginx1'
ports:
- '${NGINX_PORT}:${NGINX_PORT}'
volumes:
# The nginx.conf file mapping is optional. You can just put your domain config to /etc/nginx/conf.d/mydomain.conf
- ./nginx.conf:/etc/nginx/nginx.conf:ro
#- ./conf.d/:/etc/nginx/conf.d/:ro
#- ./dist:/usr/share/nginx/html:ro
restart: unless-stopped

Verify

mkdir my-app && cd my-app
vim compose.yaml
# paste the compose.yaml. Save and exit.

# get nginx.conf
docker run --rm --entrypoint=cat nginx:stable-alpine /etc/nginx/nginx.conf > ./nginx.conf
# set temporary environment variables
export NGINX_PORT=80
# start docker compose
docker compose up -d
docker compose logs -f nginx1
# verify
docker compose exec -it nginx1 nginx -v
docker compose exec -it nginx1 nginx -t
curl http://localhost:80