Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing dev/test/prod environments with Docker

There seems to be sparse conflicting information around on this subject. Im new to Docker and need some help. I have several docker containers to run an application, some require different config files for local development as they do for production. I don't seem to be able to find a neat way to automate this with Docker.

My containers that include custom config are Nginx, Freeradius and my code/data container is Laravel therefore requires a .env.php file (L4.2 at the moment).

I have tried Dockers environment variables in docker compose:

docker-compose.yml:

freeradius:
    env_file: ./env/freeradius.env

./env/freeradius.env

DB_HOST=123.56.12.123
DB_DATABASE=my_database
DB_USER=me
DB_PASS=itsasecret

Except I can't pick those variables up in /etc/freeradius/mods-enabled/sql where they need to be.

How can I get Docker to run as a 'local' container with local config, or as a 'production' container with production config without having to actually build different containers, and without having to attach to each container to manually config them. I need it automated as this is to eventually be used on quite a large production environment which will have a large cluster of servers with many instances.

Happy to learn Ansible if this is how people achieve this.

like image 967
DavidT Avatar asked Feb 21 '26 20:02

DavidT


1 Answers

If you can't use environment variables to configure the application (which is my understanding of the problem), then the other option is to use volumes to provide the config files.

You can use either "data volume containers" (which are containers with the sole purpose of sharing files and directories) with volumes_from, or you can use a named volume.

Data Volume container

If the go with the "data volume container" route, you would create a container with all the environment configuration files. Every service that needs a file uses volumes_from: - config. In dev you'd have something like:

configs:
    build: dev-configs/

freeradius:
    volumes_from:
        - configs

The dev-configs directory will need a Dockerfile to build the image, which will have a bunch of VOLUME directives for all the config paths.

For production (and other environments) you can create an override file which replaces the configs service with a different container:

docker-compose.prod.yml:

configs:
    build: prod-configs/

You'll probably have other settings you want to change between dev and prod, which can go into this file as well. Then you run compose with the override file:

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

You can learn more about this here: http://docs.docker.com/compose/extends/#multiple-compose-files

Named Volume

If you go with the "named volume" route, it's a bit easier to configure. On dev you create a volume with docker volume create thename and put some files into it. In your config you use it directly:

freeradius:
    volumes:
        - thename:/etc/freeradius/mods-enabled/sql

In production you'll either need to create that named volume on every host, or use a volume driver plugin that supports multihost (I believe flocker is one example of this).

Runtime configs using Dockerize

Finally, another option that doesn't involve volumes is to use https://github.com/jwilder/dockerize which lets you generate the configs at runtime from environment variables.

like image 92
dnephin Avatar answered Feb 24 '26 18:02

dnephin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!