Creating docker compose files for development and production 【Docker】
1 min readMar 25, 2023
When I use docker compose yml file, environment variables can be changed depending on the environment such as development and production. In that case, you can write file like below.
# docker-compose.development.yml
services:
db:
extends:
file: docker-compose.yml
service: db
env_file: .env.development
web:
extends:
file: docker-compose.yml
service: web
env_file: .env.development
mail:
extends:
file: docker-compose.yml
service: mail
volumes:
mysql:
name: exa_volume
networks:
example_default:
external: true
and production is…
# docker-compose.production.yml
services:
db:
extends:
file: docker-compose.yml
service: db
env_file: .env.production
web:
extends:
file: docker-compose.yml
service: web
env_file: .env.production
volumes:
mysql:
name: example_volume
networks:
example_default:
external: true
Of course two files above have common yml file.
# this file can be used for running golang.
version: '3'
services:
db:
build:
context: .
dockerfile: ./docker/db/Dockerfile
image: mysql
container_name: example_DB
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
volumes:
- ./mysql/:/docker-entrypoint-initdb.d
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- example_default
web:
build:
context: .
dockerfile: ./docker/web/Dockerfile
tty: true
command: go run .
volumes:
- .:/go/src/projects/example
ports:
- "3000:3000"
networks:
- example_default
mail:
image: mailhog/mailhog
container_name: example_mail
ports:
- 8025:8025
volumes:
mysql:
name: example_volume
networks:
example_default:
external: true
The command for running docker is below.
docker-compose -f docker-compose.production.yml up