1

I have been trying to understand this process of having an docker-compse.yml file that I can re-use for many production with diffrent hosts, sql password and user and so on.

I've been trying out on this file fx.


services:
 db_node_domain:
   image: mysql:5.7
   volumes:
     - db_data:/var/lib/mysql
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: PASSWORD
     MYSQL_DATABASE: wordpress
     MYSQL_USER: wordpress
     MYSQL_PASSWORD: PASSWORD
   container_name: wordpress_db

 wordpress:
   depends_on:
     - db_node_domain
   image: wordpress:latest
   expose:
     - 80:80
   restart: always
   environment:
     VIRTUAL_HOST: sub.domain.example
     WORDPRESS_DB_HOST: db_node_domain:3306
     WORDPRESS_DB_USER: wordpress
     WORDPRESS_DB_PASSWORD: $TEST
   container_name: wordpress
volumes:
   db_data:

networks:
 default:
   external:
     name: nginx-proxy  

And then I've tried to use varibales in gitlab to change the WORDPRESS_DB_PASSWORD in many ways. This is just one example of trying.

I also tried to use sed -i in .gitlab-ci.yml to change password but I wanted to know if someone could help me out on how to do this.

Thx

  • I think this might be what you're looking for: https://stackoverflow.com/questions/29377853/how-to-use-environment-variables-in-docker-compose – Flowkap Feb 14 '20 at 13:25

1 Answers1

0

You just need to replace hardcoded creds with environment variables.

Then each stages will have creds/settings.

In Gitlab go your project, then Settings > "CI / CD" > Variables.

Then populate each ENV variable needed, hey will be present on each run

services:
 db_node_domain:
   image: mysql:5.7
   volumes:
     - db_data:/var/lib/mysql
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
     MYSQL_DATABASE: ${MYSQL_DATABASE}
     MYSQL_USER: ${MYSQL_USER}
     MYSQL_PASSWORD: ${MYSQL_PASSWORD}
   container_name: wordpress_db

 wordpress:
   depends_on:
     - db_node_domain
   image: wordpress:latest
   expose:
     - 80:80
   restart: always
   environment:
     VIRTUAL_HOST: ${VIRTUAL_HOST}
     WORDPRESS_DB_HOST: ${WORDPRESS_DB_HOST}
     WORDPRESS_DB_USER: ${WORDPRESS_DB_USER}
     WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
   container_name: wordpress
volumes:
   db_data:

networks:
 default:
   external:
     name: nginx-proxy
J-Jacques M
  • 978
  • 6
  • 16