Using shell command in docker-compose.yml
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"
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- 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 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?
Commands are not expanded by Compose, in .env or elsewhere.
This would be very useful to have ....
+1
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.
+1
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)
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}
@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.
+1 Plz reopen the issue
+1 my use case it to publish a range of ports that is determined as a function of a "base" port
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).
+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
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.
+1
@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.
+1
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