A
A
Alexey Nikolaev2019-09-03 11:24:05
Docker
Alexey Nikolaev, 2019-09-03 11:24:05

What is the easiest way to build docker containers for different env?

Good day.
You need to build the project differently for different env (prod, dev). The project is built via docker-compose (there are several containers).
I read on the Internet, someone suggests simply naming the dockerfile differently and specifying the one you need explicitly via -f, someone suggests creating a shell script to encapsulate the installation logic and call it inside the dockerfile, but no one has written how to do it so that:
- there was a .env file
- when running docker-compose up, this file was read, ENV was taken from there
- depending on ENV, docker-compose.{env}.yaml and dockerfile.{env} were selected for each of the containers (if the assembly is from a directory, and not from image)
At the same time, I would like to avoid bicycles with shell scripts and the like. Docker, on the other hand, should provide something out of the box - it seems to be a standard Wishlist, not so much, but I didn’t find how to do it, and apparently, there will be a fierce crutch.
Is it possible to achieve the above, and if so, how? If not, what method of building a project for different environments would you recommend?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Shakhunov, 2019-09-04
@Heian

- depending on ENV, docker-compose.{env}.yaml and dockerfile.{env} were selected for each of the containers (if the build is from a directory, not from an image)

It doesn't work like that. ENV is set immediately for a specific service in docker-compose and for a specific Dockerfile build.
I would do normal CI in gitlab, for example, and builds from branches for stage and production. Here, docker-compose itself is not quite designed for builds and generates crutches.
It is more convenient to have one Dockerfile, and pass special parameters via --build-arg
docker build --build-arg ENV=stage .

V
Vitaly Karasik, 2019-09-03
@vitaly_il1

If you prefer to use separate files for each env, then as far as I understand it is very simple:
1) ENV=xxxx
2) COMPOSE_FILE=docker-compose-$ENV.yml (or $ENV/docker-compose.yml)
3) DOCKERFILE= Dockerfile-$ENV.yml
and run build

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question