'Pass argument to docker compose

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



Solution 1:[1]

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 2:[2]

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 3:[3]

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 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 blueandhack
Solution 4