Add hostname env to docker .yml file - docker

I'm trying to use my computer name as part of an address inside a docker file. I have an .env file with where it is supposed to call hostname. If I do an echo to the variable I can get the computer name but I can pass it to "traefik.http.routers.whoami.rule=Host(${HOST_HOSTNAME}.dd.dd.com)"
whoami:
image: containous/whoami
container_name: whoami
restart: ${RESTART}
labels:
- "traefik.enable=true"
# default route over https
- "traefik.http.routers.whoami.rule=Host(`${HOST_HOSTNAME}.dd.dd.com`)"
- "traefik.http.routers.whoami.entrypoints=https"
- "traefik.http.routers.whoami.tls.certresolver=${PROVIDER}"
# HTTP to HTTPS
- "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
- "traefik.http.routers.whoami-redirs.rule=hostregexp(`{host:.+}`)"
- "traefik.http.routers.whoami-redirs.entrypoints=http"
- "traefik.http.routers.whoami-redirs.middlewares=redirect-to-https"
Its any other way that I can invoke the computer name and use it to complete the address on the 8 code line?
This is what I've tried inside the .env file.
HOST_HOSTNAME=hostname
HOST_HOSTNAME='hostname'
set host=%COMPUTERNAME%
HOST_HOSTNAME=host

The environment variables that you are trying to access should be setted in your local environment. Try to run first an export to the variables, you can do something similar to this:
docker-compose.yml
version: '3.1'
services:
whoami:
image: containous/whoami
labels:
- "label_test=${VAR}"
Export the variable VAR
export VAR=foo
If you run and inspect the container you should see the label with the value
docker inspect root_whoami_1_6f9004197d63 --format '{{ index .Config.Labels "label_test"}}'
foo
You can view more information in the compose environment variable documentation https://docs.docker.com/compose/environment-variables/#substitute-environment-variables-in-compose-files

Related

use ssh keyfile as environment variable in docker-compose [duplicate]

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:

Docker-compose - setting environment variables that are not literals

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:

Docker Compose File cant get .env Variables

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 ...

Docker Deploy stack extra hosts ignored

docker stack deploy isnt respecting the extra_hosts parameter in my compose file. when i do a simple docker-compose up the entry is created in the /etc/hosts however when i do docker deploy –compose-file docker-compose.yml myapp it ignores extra_hosts, any insights?
Below is the docker-compose.xml:
version: '3'
services:
web:
image: user-service
deploy:
labels:
- the label
build:
context: ./
environment:
DATABASE_URL: jdbc:postgresql://dbhost:5432/postgres
ports:
- 9002:9002
extra_hosts:
- "dbhost: ${DB_HOST}"
networks:
- wellness_swarm
env_file:
- .env
networks:
wellness_swarm:
external:
name: wellness_swarm
the docker-compose config also displays the compose file properly.
This may not be a direct answer to the question as it doesn't use env variables but what I found was that the extra_hosts block in the compose file was ignored in swarm mode if entered in the format above.
i.e. this works for me and puts entries in /etc/hosts in the container:
extra_hosts:
retisdev: 10.48.161.44
retistesting: 10.48.161.44
whereas when entered in the other format it gets ignored when deploying as a service
extra_hosts:
- "retisdev=10.48.161.44"
- "retistesting=10.48.161.44"
I think it's an ordering issue. The ${} variable you've got in the compose file runs during the YAML processing before the service definition is created. Then stack deploy processes the .env file for running in the container as envvars, but the YAML variable is needed first...
To fix that, you should use the docker-compose config command first, to process the YAML, and then use the output of that to send to the stack deploy.
docker-compose config will show you the output you're likely wanting.
Then do a pipe to get a one-liner.
docker-compose config | docker stack deploy -c - myapp
Note: Ideally you wouldn't use the extra_hosts, but rather put the envvar directly in the connection string. Your way seems like unnecessary complexity and isn't the usual way I see a connection string created.
e.g.
version: '3'
services:
web:
image: user-service
deploy:
labels:
- the label
build:
context: ./
environment:
DATABASE_URL: jdbc:postgresql://${DB_HOST}:5432/postgres
ports:
- 9002:9002
networks:
- wellness_swarm
env_file:
- .env
networks:
wellness_swarm:
external:
name: wellness_swarm
As i see https://github.com/moby/moby/issues/29133 seems like it is by design where in the compose command takes into consideration the environment variables mentioned in .env file however the deploy command ignores that :( why is that so, pretty lame reasons!

Set extra host in environment variables

I'm using docker compose to run my application. And for do that I need to set the hosts inside container (it's depends on the environment i'm running).
My approach was:
Create an environment file and set the variable:
#application.env
SERVER_IP=10.10.9.134
My docker compose file looks like:
version: '2'
services:
api:
container_name: myApplication
env_file:
- application.env
build: ./myApplication/
entrypoint: ./docker/api-startup.sh
ports:
- "8080:8080"
depends_on:
- redis
extra_hosts: &extra_hosts
myip: $SERVER_IP
But my problem is that the variable SERVER_IP is never replaced.
When I run docker-compose config I see:
services:
api:
build:
context: /...../myApplication
container_name: myApplication
depends_on:
- redis
entrypoint: ./docker/api-startup.sh
environment:
SERVER_IP: 10.10.9.134
extra_hosts:
myip: ''
ports:
- 8080:8080
I've tried to replace the variable reference using $SERVER_IP or ${SERVER_IP} but it didn't work.
I created a file .env, added single line HOST=test.example.com, then did this in docker-compose:
extra_hosts:
- myip:${HOST}
docker-compose config then shows
extra_hosts:
myip: test.example.com
To do this I followed the documentation from Docker-compose environment variables the section about .env file
UPDATE
According to the Docker documentation,
Note: If your service specifies a build option, variables defined in
environment files will not be automatically visible during the build.
Use the args sub-option of build to define build-time environment
variables.
It basically means if you place your variables in .env file, you can use them for substitution in docker-compose.yml, but if you use env_file option for the particular container, you can only see the variables inside the Docker container, not during the build. It is also logical, env_file replaces docker run --env-file=FILE ... and nothing else.
So, you can only place your values into .env. Alternatively, as William described, you can use host's environment variables.
EDIT
Try the following:
version: '2'
services:
api:
container_name: myApplication
env_file:
- application.env
build: ./myApplication/
entrypoint: ./docker/api-startup.sh
ports:
- "8080:8080"
depends_on:
- redis
extra_hosts:
- "myip:${SERVER_IP}"
Ensure curly bracers and that the environment variable exists on the host os.

Resources