n1 avatar

Docker + your development and production environments

n1-cz

Published: 10 Dec 2018 › Updated: 10 Dec 2018Docker + your development and production environments

Docker + your development and production environments

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.

Leave Docker + your development and production environments to:

Written by

Python dev, tomorrow tech guy.

Read more #docker posts


Best Posts From n1

We have not curated any of n1-cz's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From n1