Here is my docker-compose.yml file which is using environment
variable from .env file.I am also providing my .env file below this.
docker-compose.yml
version: "3.5"
services:
database_master:
image: mysql:8.0.30
stdin_open: true
tty: true
container_name: "database_master"
restart: unless-stopped
ports:
- 3307:3306
volumes:
- mysqldata_master:/var/lib/mysql
- ./master.cnf:/etc/my.cnf
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
networks:
- mynetwork
database_slave:
image: mysql:8.0.30
stdin_open: true
tty: true
container_name: "database_slave"
restart: unless-stopped
depends_on:
- database_master
ports:
- 3308:3306
volumes:
- mysqldata_slave:/var/lib/mysql
- ./slave.cnf:/etc/my.cnf
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
networks:
- mynetwork
networks:
mynetwork:
volumes:
mysqldata_master:
mysqldata_slave:
create .env file as
MYSQL_ROOT_PASSWORD='S3cret'
MYSQL_USER='my_db_user'
MYSQL_DATABASE='my_db'
MYSQL_PASSWORD='S3cret'
docker-compose.yml we are using bash syntax to get value of variable in .env file in environment section as
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
No comments:
Post a Comment