Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there way in docker compose to create the cross service constant?

I have compose file like this:

service1:
   //some stuff
   environment:
  - "PROP=some_common_value"
service2:
  //some stuff
  environment:
  - "PROP=some_common_value"
service2:
  //some stuff
  environment:
  - "PROP=some_common_value"

I want to set properties with the same values.

As you can I now I copy past that value. It looks awful.
Is there way in docker compose to create the cross service constant?

like image 385
gstackoverflow Avatar asked Oct 12 '18 14:10

gstackoverflow


People also ask

Does docker compose create services?

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

What are the three main steps of docker compose?

Basically, using Compose in Docker is a three-step process: At very first, define app's environment with a Dockerfile hence we can reproduce it anywhere. Afterwards, define the services that make up the app in docker-compose. yml hence it can be run together in an isolated environment.

Do you need Dockerfiles with docker compose?

Docker compose uses the Dockerfile if you add the build command to your project's docker-compose. yml. Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.

How to run multiple containers as a service in Docker Compose?

Docker Compose runs multiple containers as a single service. So, for example, if the application requires MySQL and NUnit, we can start both the containers at once as a service using the YAML file.

What is docker-compose?

The Compose file provides a way to document and configure all of the application’s service dependencies (databases, queues, caches, web service APIs, etc). Using the Compose command line tool you can create and start one or more containers for each dependency with a single command (docker-compose up).

How does docker compose create a network?

By default, Docker Compose automatically creates a network specifically for the application stack (which is why we didn’t define one in the compose file). Let’s look at the logs using the docker-compose logs -f command. You’ll see the logs from each of the services interleaved into a single stream.

What is a compose file in Docker?

Docker - Compose. Docker Compose is used to run multiple containers as a single service. For example, suppose you had an application which required NGNIX and MySQL, you could create one file which would start both the containers as a service without the need to start each one separately.


1 Answers

There are a couple options. If you deploy with docker-compose and not docker stack deploy, then you can use a .env file to set variables. Or with either docker-compose or swarm mode, you can set an environment variable in your shell (often a shell script used to deploy the app). Inside of the yaml, you would use the environment variable like:

service1:
  environment:
  - PROP
service2:
  environment:
  - PROP
service2:
  environment:
  - PROP

or explicitly use the variable like:

service1:
  environment:
  - PROP=${PROP}
service2:
  environment:
  - PROP=${PROP}
service3:
  environment:
  - PROP=${PROP}

If you have a .env file and want to do a docker stack deploy, you can script the processing of that with either of:

# option 1, leaves vars set in shell
set -a && . .env && set +a && docker stack deploy -c compose.yml stack_name

# option 2, only sets vars for single command
env $(cat .env | xargs) docker stack deploy -c compose.yml stack_name

In each of the above, the .env is just the variables you could otherwise set in your shell:

PROP=some_common_value

The next option is to use Yaml syntax of anchors and aliases. This copies from one section of a Yaml file to another (to support merging in service3, I switched to key/value instead of the array syntax):

service1:
  environment: &common_env
    PROP: "some_common_value"
service2:
  environment: *common_env
service3:
  environment:
    <<: *common_env
    PROP3: "unique value"

To allow each service to have unique values, Docker added extension fields in recent versions of the compose syntax. This lets you define an anchor separate from any of the service definitions and merge it into each service, each with their own unique values, e.g.:

version: '3.4'
x-common-env: &common_env
  PROP: "some_common_value"
services:
  service1:
    environment:
      <<: *common_env
      PROP1: "unique value 1"
  service2:
    environment:
      <<: *common_env
      PROP2: "unique value 2"
  service3:
    environment:
      <<: *common_env
      PROP3: "unique value 3"
like image 200
BMitch Avatar answered Oct 02 '22 07:10

BMitch