Differentiating Environments in Docker Compose - And A Hack For Jenkins Plugin
Docker Compose orchestrates the launch of multiple containers for an application on a single Docker host. A YAML file (usually named docker-compose.yml) describes how each container is going to be deployed (volumes, ports, environment variables) and how they are related to one another (Apache, PHP, and MySQL, for example).
One less known feature of Docker Compose is that you can combine multiple YAM files and Compose will merge them. You can, for example, define different database parameters (host, username, password, database/schema) for different environments (production and development) by writing a base file and then more specific files for each.
Lets take this base file:
docker-compose.yml
version: '3'
services:
web:
image: php:7.3.0-apache
container_name: site-web
depends_on:
- db
volumes:
- ./site/:/var/www/html/
ports:
- "8100:80"
mysql:
image: mysql:8.0
container_name: site-db
restart: always
volumes:
- mysql:/var/lib/mysql
Now we can extend this file for each environment:
docker-compose.prod.yml
version: '3'
services:
mysql:
environment:
MYSQL_ROOT_PASSWORD: root_passord_prod
MYSQL_DATABASE: database_prod
MYSQL_USER: user_prod
MYSQL_PASSWORD: user_password_prod
MYSQL_ROOT_PASSWORD: root_passord_prod
MYSQL_DATABASE: database_prod
MYSQL_USER: user_prod
MYSQL_PASSWORD: user_password_prod
docker-compose.dev.yml
version: '3'
services:
mysql:
environment:
MYSQL_ROOT_PASSWORD: root_passord_dev
MYSQL_DATABASE: database_dev
MYSQL_USER: user_dev
MYSQL_PASSWORD: user_password_dev
MYSQL_ROOT_PASSWORD: root_passord_dev
MYSQL_DATABASE: database_dev
MYSQL_USER: user_dev
MYSQL_PASSWORD: user_password_dev
You can now start containers for each environment:
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
or
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
Well, but then Jenkins is used to deploy your containers. Jenkins has a handy plugin to deploy containers described by Docker Compose provided by a Git repository. To make the above setup work, it's needed a little hack and Jenkins will pass the correct parameters to Docker Compose.
Docker Compose File
docker-compose.yml -f /jenkins/workspace/project/Development/docker-compose.dev.yml
Notice the -f parameter with the full path to the override Docker Compose file. You must find the full path of Jenkins agent in the Docker Host.
Docker Compose and Jenkins are great together.
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete