I run docker-compose up in a parent directory and -f the docker-compose.yml in child folder. Does anyone know why this won't work? MY_VAR is evaluated to empty string
root-ui-e2e-ci:
environment:
MY_VAR: ./hello
env_file: ./.env
volumes:
- ${MY_VAR}:/app
I end up with this error
.: volume name is too short, names should be at least two alphanumeric
characters
The variable specified in environment & env_file not used for compose-file, it will directly pass to container.
For variable substitution in docker-compose.yaml, you could use next two solutions, and use docker-compose config to quick check the effect:
Solution 1:
Use the variable export in the same shell which run docker-compose:
docker-compose.yaml:
version: '3'
services:
root-ui-e2e-ci:
image: ubuntu
volumes:
- ${MY_VAR}:/app
Try Command:
$export MY_VAR=./hello
$docker-compose config
services:
root-ui-e2e-ci:
image: ubuntu
volumes:
- /home/shubuntu1/99/hello:/app:rw
version: '3.0'
Solution 2:
Use .env:
Set a .env file in the same folder of docker-compose.yaml:
.env:
MY_VAR=./hello
docker-compose.yaml:
version: '3'
services:
root-ui-e2e-ci:
image: ubuntu
volumes:
- ${MY_VAR}:/app
Try Command:
$unset MY_VAR
$docker-compose config
services:
root-ui-e2e-ci:
image: ubuntu
volumes:
- /home/shubuntu1/99/hello:/app:rw
version: '3.0'
depends on you comment that hello is a stringyou need to rewrite you docker-compose like this:
environment:
MY_VAR: hello
volumes:
- ../${MY_VAR}:/app
Related
As far as I know, to use the env variable in docker-compose we need to create a .env file
PORT=9000
And then use it like this in docker-compose.yml:
services:
go:
ports:
- ${PORT}:${PORT}
But what if I have a config file in .yml file like this:
http:
host: "0.0.0.0"
port: "1020"
jwt_secret: "secret"
How to access it on the docker-compose? This doesn't work:
version: "3.8"
services:
go:
env_file: config.yml
build:
context: .
dockerfile: Dockerfile
command: /fetch-app
ports:
- ${http.port}:${http.port}
environment:
NODE_ENV: development
It is generates error:
invalid interpolation format for services.go.ports.[].
You may need to escape any $ with another $.
${http.port}:${http.port}
I have a simple docker compose file to create a Mysql database for my app. But I cannot interpolate the environment variable MYSQL_PORT to set a custom port. Running docker compose up with the configuration below results in a random port being assigned to mysql.
The path to the env file does work, since I have env variables configuring the database.
docker-compose.yml
version: '3'
services:
mysql:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- mysql_data:/var/lib/mysql
env_file:
- ../../.env
ports:
- ${MYSQL_PORT}:3306
volumes:
mysql_data:
.env
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=final_project_database
MYSQL_USER=db_user
MYSQL_PASSWORD=some_db_user_password
Use --env-file option with docker-compose up command. env_file declared in your MySQL service applies only for container env
Move your .env file to the same directory as the docker-compose file and change the env_file to point to it. That way both docker-compose and the container will use the same environment file.
Right now it's only the container that's using it.
version: '3'
services:
mysql:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
volumes:
- mysql_data:/var/lib/mysql
env_file:
- ./.env
ports:
- ${MYSQL_PORT}:3306
volumes:
mysql_data:
I get the below error when I run docker-compose up, any pointers why I am getting this error
service "mysqldb-docker" refers to undefined volume mysqldb: invalid compose project
Also, is there a way to pass the $ENV value in CLI to docker-compose up , currently I have a ENV variable that specified dev, uat or prod that I use to specify the db name. Are there better alternatives to do this other than create a .env file explicitly for this
version: '3.8'
services:
mysqldb-docker:
image: '8.0.27'
restart: 'unless-stopped'
ports:
- "3309:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_PASSWORD=root
- MYSQL_DATABASE=reco-tracker-$ENV
volumes:
- mysqldb:/var/lib/mysql
reco-tracker-docker:
image: 'reco-tracker-docker:v1'
ports:
- "8083:8083"
environment:
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root
- SPRING_DATASOURCE_URL="jdbc:mysql://mysqldb-docker:3309/reco-tracker-$ENV"
depends_on: [mysqldb-docker]
You must define volumes at the top level like this:
version: '3.8'
services:
mysqldb-docker:
# ...
volumes:
- mysqldb:/var/lib/mysql
volumes:
mysqldb:
You can pass environment variables from your shell straight through to a service’s containers with the ‘environment’ key by not giving them a value
https://docs.docker.com/compose/environment-variables/#pass-environment-variables-to-containers
web:
environment:
- ENV
but from my tests you cant write $ENV in the compose file and expect it to read your env
for this you need to call docker-compose that way :
docker-compose run -e ENV web python console.py
see this : https://docs.docker.com/compose/environment-variables/#set-environment-variables-with-docker-compose-run
I'm trying to pass some host's environment variables content as args to the docker-compose file, through a .env file. But the variable is interpreted as a string.
Follows the content of my files:
.env:
USER=$USER
UID=$UID
GID=$GID
docker-compose.yml:
version: "2"
services:
opencv_python:
build:
args:
- username=${USER}
- uid=${UID}
- gid=${GID}
context: .
dockerfile: opencv_base.Dockerfile
container_name: ocv-data-augmentation
image: ocv-data-augmentation
environment:
DISPLAY: $DISPLAY
QT_X11_NO_MITSHM: 1
volumes:
- "../project:/home/&{USER}/data_augmentation/" # Host : Container
- "/tmp/.X11-unix:/tmp/.X11-unix"
tty: true
And this is the output of the command docker-compose config:
services:
opencv_python:
build:
args:
gid: $$GID
uid: $$UID
username: fsalvagnini
context: /home/fsalvagnini/Documents/containers/data_augmentation/dockerfiles
dockerfile: opencv_base.Dockerfile
container_name: ocv-data-augmentation
environment:
DISPLAY: :1
QT_X11_NO_MITSHM: 1
image: ocv-data-augmentation
tty: true
volumes:
- /home/fsalvagnini/Documents/containers/data_augmentation/project:/home/&{USER}/data_augmentation:rw
- /tmp/.X11-unix:/tmp/.X11-unix:rw
version: '2.0'
If you need to use the .env file and assuming that all the env variables are defined, you just need to follow one step:
.env file
source .env
The above statement will source all the variables defined in .env and hence the env variables will be accessible to docker-compose.
Just added thing, you should also look at ${VARIABLE:-default} just in case you need to pass a default value.
More documentation here
According to the docker-compose manual:
When you set the same environment variable in multiple files, here’s
the priority used by Compose to choose which value to use:
Compose file
Shell environment variables
Environment file
Dockerfile
Variable is not defined
So if shell environment variables are not set, then env file will be used.
For your case, if you need to use shell env vars, you don't need to create .env file. To solve your issue, you need to export the variables before invoking docker-compose.
export GID
export UID
export DISPLAY
docker-compose config
output:
services:
opencv_python:
build:
args:
gid: '20'
uid: '501'
username: enix
context: /Users/enix/source/devops/stackoverflow
dockerfile: opencv_base.Dockerfile
container_name: ocv-data-augmentation
environment:
DISPLAY: :1
QT_X11_NO_MITSHM: 1
image: ocv-data-augmentation
tty: true
volumes:
- /Users/enix/source/devops/project:/home/&{USER}/data_augmentation:rw
- /tmp/.X11-unix:/tmp/.X11-unix:rw
version: '2.0'
I have two container images and doing compose and running the docker using "docker-compose up -d".
This works fine. I want to run the same container image in another port say 8081.
Can we pass port mapping as a command line parameter docker-compose up -port novnc :8081:8080?
How to pass port mapping dynamically to the docker-compose up command?
version: '2'
services:
ide:
image: myApp
image: myImage:latest
environment:
- DISPLAY=novnc:0.0
depends_on:
- novnc
networks:
- x11
novnc:
image: myImageTwo:latest
environment:
- DISPLAY_WIDTH=1600
- DISPLAY_HEIGHT=968
ports:
- "8080:8080"
networks:
- x11
networks:
x11:
use a ${VAR} in your docker-compose.yml
e.g.
version: '2'
services:
apache:
image: httpd:2.4
volumes:
- .:/usr/local/apache2/htdocs/
ports:
- ${APP_PORT}:80
then use environment variable:
$ export APP_PORT=8080
$ docker-compose up
or inline version:
$ APP_PORT=8080 docker-compose up
You can do it using the .env file.
For example, you'll have something like this :
$ cat .env
TAG=v1.5
$ cat docker-compose.yml
version: '3'
services:
web:
image: "webapp:${TAG}"
In the example, you can see that the tag value is in a variable that is set in the env file.
You can find out more in the official doc
Change your ports section to:
ports:
- "${MY_PORT}:8080"
and then just use MY_PORT=8081 docker-compose up -d
you can also use a port range
ports:
- "8080-8081:8080"
so you don't have to pass as parameter