Pass argument to docker compose

DockerDocker Compose

Docker Problem Overview


In my docker compose file there is a dynamic field which I'd like to generate during the running. Actually it is a string template:

environment:
    - SERVER_URL:https://0.0.0.0:${PORT}

And I want to configure this PORT parameter dynamically

docker-compose run <service> PORT=443

In documentation there is ARGS parameters set I suppose I can use. But there is no information how can I use those inside compose file

Docker Solutions


Solution 1 - Docker

In docker-compose, arguments are available and usefull only in dockerfile. You can specify what you are doing in the level ahead like following:

#dockerfile
ARG PORT
ENV SERVER_URL "https://0.0.0.0:$PORT"

Your port can be set in your docker-compose.yml:

build:
  context: .
  args:
    - PORT=443

It is actually an environment variable in any case. You can pass it through your run command if that fits to you:

PORT=443 docker-compose run <service>
#or
docker-compose run <service> -e PORT=443

Solution 2 - Docker

You can use the flag when using docker-compose build

docker-compose build --build-arg PRODUCTION=VALUE

In Dockerfile you can get the argument PRODUCTION

# Dockerfile
ARG PRODUCTION
FROM node:latest

Solution 3 - Docker

This is possible with docker stack deploy

Example Compose File in your environment section:

- MY_VARIABLE_NAME=${MY_VARIABLE_VALUE}

Stack Deploy Command (I ran this from Gitbash in Windows):

MY_VARIABLE_VALUE=some-value docker stack deploy --compose-file compose_file_here stackname


Reference See this Github post here

Solution 4 - Docker

This is possible with docker-compose with ARGS inside Dockerfile.

Problem to Solve:

  • Pull changes from Git Respository, to Automate App Deploy

Dockerfile

RUN 
ARG CACHEBUST=1 # This will setup a arg called CACHEBUST
RUN  git clone

Run the below bash command to build and run. SETTING --build-arg CACHEBUST= random md5sum hash, makes Docker-Compose to rebuild the image, starting the line with ARGS and so on.

docker-compose -f dockerprd.yml build  --build-arg CACHEBUST=$(echo $RANDOM | md5sum | head -c 20; echo;) && docker-compose -f dockerprd.yml up -d

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondr11View Question on Stackoverflow
Solution 1 - Dockerdzof31View Answer on Stackoverflow
Solution 2 - DockerblueandhackView Answer on Stackoverflow
Solution 3 - DockerjoshmcodeView Answer on Stackoverflow
Solution 4 - DockerJosué PiroloView Answer on Stackoverflow