Docker + your development and production environments

in docker •  6 years ago  (edited)

As a developer I’m used to have at least 2 environments of all projects  -  development and production. Since I’ve been using Docker this routine seemed to be more comfortable until …

What if I want to run my project simultaneously in both environments? Boom, el problem! Why? Let’s dive into that.

When I started using Docker and I get too far too use docker-compose I’ve created my first docker-compose.yml file. Then I used to happily run this:

docker-compose up

and my project was running. Amazing.

That was cool for dev environment. Once I’ve needed to create production set-up I created new file docker-compose.prod.yml and when I wanted to run production set-up I’ve used to run this:

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

and Docker took default file + my production file, merged them into one configuration and project was up in production set-up.

But what if I want to have these set-ups running side by side? When you run these two commands in two terminals

docker-compose up

and

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

Docker will end up with errors like conflict names, containers etc. I was like

Basically all you need to know is you have to create 2 separate docker files (so NOT one as base and one as override). In these files you need to name your services uniquely and then run docker-compose with -p param with project name for each environment. So step by step:

  1. Two docker files — one named like docker-compose.dev.ymland second docker-compose.prod.yml.
  2. Name your services uniquely across your whole computer. When I use Redis I name my redis image like redis-PROJECT_NAME-dev so i.e.
redis:
    image: redis:3.2
    container_name: redis-my-app-dev

Even your app has to have unique name:

app:
    container_name: my-app-dev
  1. Run docker-compose with given project name like:
docker-compose -f docker-compose.prod.yml -p my-app-prod up

More on Github
Related Stack overflow question.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://medium.com/@n1_/docker-and-your-development-a-production-environments-857067a9bfc0