compose icon indicating copy to clipboard operation
compose copied to clipboard

Using shell command in docker-compose.yml

Open zkanda opened this issue 10 years ago • 70 comments

Hello, is there a way to use shell commands in docker-compose.yml file? Here is my use case:

 version: '2'
 services:
   ci:
     image: jenkins
     volumes:
       - ./data:/var/jenkins_home
       - /var/run/docker.sock:/var/run/docker.sock
       - $(command -v docker):/usr/bin/docker
     groupadd:
       - $(stat -c %g /var/run/docker.sock)
     ports:
       - "8080:8080"
       - "50000:50000"

Currently it's giving me this error:

ERROR: Invalid interpolation format for "volumes" option in service "ci": "${command -v docker}:/usr/bin/docker"

zkanda avatar Oct 27 '16 09:10 zkanda

Hi @zkanda ,

Sorry, this is not something we support. Usually, this is done by setting environment variables and using variable substitution inside the Compose file instead.

shin- avatar Oct 28 '16 18:10 shin-

@shin- thanks, I can workaround with environment variables. Also .env seems very useful as well. Relevant docs: https://docs.docker.com/compose/environment-variables/#/the-env-file

zkanda avatar Dec 08 '16 03:12 zkanda

@zkanda did you ever get this to work, I tried in my .env file

DOCKER_BIN=`which docker`

and then in the docker-compose.yml

jenkinsmaster:
  build: jenkins-master
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - ${DOCKER_BIN}:/usr/bin/docker
  ports:
    - "50000:50000"

But I keep getting

Cannot create container for service jenkinsmaster: create `which docker`: "`which docker`" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.

So it looks like commands are interpreted as strings in the .env file?

dtothefp avatar Feb 27 '17 23:02 dtothefp

Commands are not expanded by Compose, in .env or elsewhere.

shin- avatar Feb 28 '17 02:02 shin-

This would be very useful to have ....

marzlarz avatar Jun 07 '18 16:06 marzlarz

+1

CpuID avatar Jul 16 '18 23:07 CpuID

Here's a use case. When setting up Kafka, it needs the IP of the host machine. I have the following script to get that for me:

DOCKER_HOST_IP=$(ifconfig | grep 'inet .*br' | sed -E 's/.*inet (.*) netmask.*/\1/')

I reference that IP in the env. I can't just run docker-compose up anymore because I need to run that beforehand. And I need to inform the other devs on the team of this.

jeffijoe avatar Aug 27 '18 09:08 jeffijoe

+1

bfwg avatar Oct 13 '18 20:10 bfwg

Would be useful to get the current user, as in:

services:
  foo:
    image: bar:latest
    user: ${CURRENT_USER-$(id -u):$(id -g)}

There must be other use cases too...

By the way, I don't get the fact that ${BAZ-default} works in Compose if we can't execute the remainder of the Bash functionalities... (as far as I know, the - is one of Bash's variable manipulation functionalities, as substitution with ${VAR/search/replace}, for example)

davi5e avatar Oct 17 '18 20:10 davi5e

Yes @davi5e ... What I was trying for:

version: "3.7"

services:
  orchestrator:
    build: .
    ports:
      - "2000:2000"
    # Use type host until we can test link
    network_mode: "host"
    environment:
      - NODE_ENV=development
      - LOCAL_USER_ID=${id -u}

binarymist avatar Oct 18 '18 06:10 binarymist

@davi5e another use case is when you want to create a docker volume and link it to your git repository directory. This absolute bath will wary for each developer on the team.

Baathus avatar Nov 06 '18 09:11 Baathus

+1 Plz reopen the issue

sido420 avatar Nov 07 '18 02:11 sido420

+1 my use case it to publish a range of ports that is determined as a function of a "base" port

sarkortrantor avatar Dec 04 '18 11:12 sarkortrantor

This would be very useful to have for setting UID/GID variables as this is pretty much a requirement for using Docker in a development environment (without needed to use scripts to set this into an .env file).

moacode avatar Dec 10 '18 01:12 moacode

+1

Could use this to label/tag the images generated by docker compose with the git tag and commit hash:

In .env:

GIT_VERSION=$(git describe --always --dirty --abbrev)
OUTER_PORT=6970

In docer-complse.yml:

version: '3'
services:
  nginx:
    restart: always
    build:
        context: ./nginx
        labels:
          org.label-schema.schema-version: "1.0"
          org.label-schema.version: "${GIT_VERSION}"
          org.lavel.schema.url: "https://mydocu-server.company.com/vcs/${GIT_VERSION}"
    ports:
      - ${OUTER_PORT}:8080

con-f-use avatar Dec 24 '18 22:12 con-f-use

For those doing this in development, if your shell sets the UID variable, you can pass that in when building the local development image. Since the local development image is not shared, you don't have to worry about uid conflicts with your co-workers.

ackerleytng avatar Jan 15 '19 11:01 ackerleytng

+1

laflaneuse avatar Mar 29 '19 13:03 laflaneuse

@con-f-use Doing that will just send the actual command and not the result from my testing, the only hack I've been successful so far is writing a script wrapping docker compose which sets up the environment variable.

Hultner avatar May 29 '19 09:05 Hultner

+1

julionsc avatar Jun 24 '19 16:06 julionsc

TL;DR if you want to export those variables from .env file:

# set the path of .env file here
ENV_FILE="${ENV_FILE:-local.env}"
while IFS= read -r line; do
  export "$line"
done < <( grep --color=never -E -v -e '^#' -e '^[[:space:]]*$' "${ENV_FILE}" )

How to use:

  • Type it on your command line and press enter to modify your current shell environment

OR

  • Save it as bash script and source it

Caveats:

  • Saving this as a script and running it won't magically export those variables in your current shell environment. You'll have to source it or do some fancy eval.
  • (Maybe a good thing?) It will overwrite existing variables with same name in $ENV_FILE

Explanation: here

wadkar avatar Jun 28 '19 09:06 wadkar