My Docker containers will be set up through a bash script.
In my docker-compose.yml file (which is called through docker-compose up inside the bash file) I have a few arguments:
version: "2"
services:
nginx:
build: ./Dockerfiles/wordpress-nginx
expose:
- "80"
depends_on:
- fpm
links:
- fpm
container_name: wordpress-nginx
args:
- site_title=Example
- admin_email=test#test.co
- site_url=www.testcase001.com
- admin_user=admin
- admin_pass=qwerty
- ssl_domain=www.testcase001.com
- dbname=testcase
- dbuser=testcase
- dbpass=testcase
- dbhost=testcase
fpm:
build: ./Dockerfiles/php-fpm
expose:
- "9000"
container_name: fpm
Now my question is this:
How can I set the arguments from the command line?
For example:
I want to set the site_title argument: docker-compose up --something-to-set-the-site_title-argument
You can use environment variables in your docker-compose.yaml file, e.g.:
args:
- site_title: $SITE_TITLE
In your Bash script, you can then set the title environment variable like this:
#!/bin/bash
SITE_TITLE="My new title"
docker-compose up -d
The $SITE_TITLE environment variable should then be used in your compose configuration.
Related
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 have a docker-compose file which looks like:
version: '3.8'
services:
scheduler:
image: apache/airflow
command: scheduler
restart: on-failure
env_file:
- .env
volumes:
- ./dags:/opt/airflow/dags
- ./logs:/opt/airflow/logs
webserver:
image: apache/airflow
entrypoint: ./scripts/entrypoint.sh
restart: on-failure
depends_on:
- scheduler
env_file:
- .env
volumes:
- ./dags:/opt/airflow/dags
- ./logs:/opt/airflow/logs
- ./scripts:/opt/airflow/scripts
ports:
- "8080:8080"
It works as expected on running docker-compose up.
But if I remove the env_file option from yml and pass it in CLI - docker-compose --env-file .env up, then it doesn't pick the value of environment variables from the file.
This happens because the --env-file in CLI and env_file in service section are not exactly the same thing.
The former is actually a specification of the file that compose will use to substitute variables inside docker-compose.yml (the default value is .env).
The latter is specifying the file from which the variables will be loaded and passed to the container of the service.
And yes, it's kinda confusing.
Reference:
Read this and this.
I have setup Jenkins within a Docker container and I am trying to access that my private Bitbucket repo with that server. I need to copy my SSH key into that container so that Bitbucket recognizes it and I can have my Jenkins server access the repo then.
I have in my docker-compose.yml file the following:
services:
jenkins:
build: .
volumes:
- jenkins-data:/var/jenkins_home
environment:
- SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa)
ports:
- "8080:8080"
- "50000:50000"
volumes:
jenkins-data:
However, echo $SSH_PRIVATE_KEY gives /.ssh/id_rsa literally instead of the value stored inside. I have heard the problem with doing this inside the Dockerfile instead would be that it still can be viewed in one of the layers of the image that will be pushed.
My question is how can I set the value of SSH_PRIVATE_KEY to the value of the contents of my file?
I believe this could be a duplicate of How to set environment variable into docker container using docker-compose however that solution does not appear to change anything for me.
You could create an Environment variable in your shell from which you are running your compose :
export SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa)
and then use it in your compose like :
services:
jenkins:
build: .
volumes:
- jenkins-data:/var/jenkins_home
environment:
- SSH_PRIVATE_KEY
ports:
- "8080:8080"
- "50000:50000"
It should pick up the value for your environment variable for container from shell environment as specified in the docs :
The value of the variable in the container is taken from the value for the same variable in the shell in which Compose is run.
Possible solution:
environment:
- SSH_PRIVATE_KEY
and call the docker-compose like this:
SSH_PRIVATE_KEY=$(cat ~/.ssh/id_rsa) docker-compose build
Unfortunately, it's currently not possible to use multiline variables in .env.
Another possibility would be:
services:
jenkins:
build: .
volumes:
- jenkins-data:/var/jenkins_home
- "/home/user/.ssh/id_rsa:/home/user/.ssh/id_rsa:ro"
ports:
- "8080:8080"
- "50000:50000"
volumes:
jenkins-data:
I am using docker-compose to run a traefik container. The Domain of this Container should be set by an environment file but everytime i start this service it says:
WARNING: The DOMAIN variable is not set. Defaulting to a blank string
My compose-file setup:
version: '3.5'
networks:
frontend:
name: frontend
backend:
name: backend
services:
Traefik:
image: traefik:latest
command: --api --docker --acme.email="test#test.de"
restart: always
container_name: Traefik
networks:
- backend
- frontend
env_file: ./env.env
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik/traefik.toml:/traefik.toml
- ./traefik/acme.json:/acme.json
ports:
- "80:80"
- "443:443"
labels:
- "traefik.docker.network=frontend"
- "traefik.enable=true"
- "traefik.frontend.rule=Host:traefik.${DOMAIN}"
- "traefik.port=8080"
- "traefik.protocol=http"
My env.env file setup:
DOMAIN=fiture.de
Thanks for your Help!
env_file: ./env.env
The file env.env isn't loaded to parse the compose file, it is loaded to add environment variables within the container being run. At the point docker processes the above instruction, the yaml file has already been loaded and variables have been expanded.
If you are using docker-compose to deploy containers on a single node, you can rename the file .env and docker-compose will load variables from that file before parsing the compose file.
If you are deploying with docker stack deploy, then you need to import the environment variables into your shell yourself. An example of doing that in bash looks like:
set -a && . ./env.env && set +a && docker stack deploy ...
I want to use excel file and some folders with my container .
I am using volumes but , dont know what the problem is with my compose .
seleniumhub:
image: selenium/hub
ports:
- "4444:4444"
firefoxnode:
image: selenium/node-firefox-debug
ports:
- "5901:5900"
links:
- "seleniumhub:hub"
shm_size: '2gb'
environment:
- "NODE_MAX_SESSION=2"
- "NODE_MAX_INSTANCES=2"
chromenode2:
image: selenium/node-chrome-debug
ports:
- "5902:5900"
links:
- "seleniumhub:hub"
shm_size: '2gb'
environment:
- "NODE_MAX_SESSION=2"
- "NODE_MAX_INSTANCES=2"
test:
image: raveena1/dilsel
ports:
- 4579
links:
- "seleniumhub:hub"
container_name: mywebcontainer
**volumes:
- /$$(pwd)/Newfolder/Config/framework-config.properties:/var/lib/docker/**
I want to use the above property file in my container , how can i achieve this ?
I don't think docker-compose can interpret bash command inside a compose file. However, what you can do is use environment variable. In your case, you might want to use $PWD.
[...]
volumes:
- $PWD/Newfolder/Config/framework-config.properties:/var/lib/docker/
[...]
This will interpret the environment variable $PWD (which resolves to your current working directy) and mount this to /var/lib/docker.
Below is an example of using environment variable in docker-compose :
docker-compose.yml:
test:
image: debian:stretch-slim
ports:
- 4579
container_name: mywebcontainer
volumes:
- $PWD/:/current_directory_of_host
entrypoint: "ls -l /current_directory_of_host"
Start this container with docker-compose up. You should see a list of file that is in your current working directory.
You can also use custom environment variable : CUSTOM_ENV=$(pwd) docker-compose up. This will forward CUSTOM_ENV to docker-compose which can be used in your docker-compose.yml.