forward exact environment variables from host to container - docker

I am finding my self repeating like this:
services:
noice-service:
environment:
- EUREKA_SERVICE_URL=${EUREKA_SERVICE_URL}
- ZIPKIN_BASE_URL=${ZIPKIN_BASE_URL}
- CONFIG_SERVER_URL=${CONFIG_SERVER_URL}
I have defined these env vars in .env file and some in another scripts and I just want to pass their exact value in container. Is there any way quicker way of achieving this without any custom shell script as entrypoint ?

You can pass the variables directly:
# .env
DOTENV=foo
# docker-compose.yml
version: "3.7"
services:
busybox:
image: busybox
command: ["env"]
environment:
- DOTENV
- ANOTHER
And run ANOTHER=bar docker-compose up.

Related

Docker Compose .env File Not Read

I have a .env file I'm trying to use in a Docker Compose file to pass to the container to be used in the entrypoint script defined in the Dockerfile.
I have the following effective Dockerfile (spread out over two files, one override). The .env.dev file is in the same directory as the docker compose files. The environment variable value is not getting passed to the container. When I add "=${RUN_MIGRATIONS_ON_START}", the variable value is blank. If I leave that off, then the variables aren't even set in the container.
Docker compose files:
Main docker compose file:
version: '3.4'
services:
web:
build
context: .
Override docker compose file:
version: '3.4'
services:
web:
environment:
- RUN_MIGRATIONS_ON_START=${RUN_MIGRATIONS_ON_START}
- WS_SCHEME=${WS_SCHEME}
env_file:
- .env.dev
Solution
docker-compose.override.yml
version: '3.4'
services:
web:
env_file:
- .env.dev
.env.dev
RUN_MIGRATIONS_ON_START=FOO
WS_SCHEME=BAR
Why
environment:
- X=${Y} # Y is a variable from the local shell environment, not from .env
Described in detail in documentation
Your configuration options can contain environment variables. Compose
uses the variable values from the shell environment in which
docker-compose is run.

Docker: Why does my project have a .env file?

I'm working on a group project involving Docker that has a .env file, which looks like this:
DATABASE_URL=xxx
DJANGO_SETTINGS_MODULE=xxx
SECRET_KEY=xxx
Couldn't this just be declared inside the Dockerfile? If so, what is the advantage of making a .env file?
Not sure if I'm going in the right direction with this, but this Docker Docs page says (emphasis my own):
Your configuration options can contain environment variables. Compose
uses the variable values from the shell environment in which
docker-compose is run. For example, suppose the shell contains
POSTGRES_VERSION=9.3 and you supply this configuration:
db:
`image: "postgres:${POSTGRES_VERSION}"`
When you run docker-compose up with this configuration, Compose looks for the POSTGRES_VERSION environment variable in the shell and substitutes its value in. For this example, Compose resolves the image to postgres:9.3 before running the configuration.
If an environment variable is not set, Compose substitutes with an empty string. In the example above, if POSTGRES_VERSION is not set, the value for the image option is postgres:.
You can set default values for environment variables using a .env file, which Compose automatically looks for. Values set in the shell environment override those set in the .env file.
If we're using a .env file, then wouldn't I see some ${...} syntax in our docker-compose.yml file? I don't see anything like that, though.
Here's our docker-compose.yml file:
version: '3'
services:
server:
build:
context: ./server
dockerfile: Dockerfile
env_file: .env.dev
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- ./server:/app
ports:
- "8500:8000"
depends_on:
- db
stdin_open: true
tty: true
db:
image: postgres
client:
build:
context: ./client
dockerfile: Dockerfile
command: bash -c "npm install; npm run start"
volumes:
- ./client:/app
- /app/node_modules
ports:
- "3000:3000"
depends_on:
- server
Idea there is probably to have a place to keep secrets separated from docker-compose.yml, which you then can keep in VCS and/or share.

How to pass variables from a .env file to Docker?

I have a .env file with a large list of variables in the root of my node project. In the root is also my docker-compose.yml file:
version: '3'
services:
api:
build: .
env_file:
- .env
ports:
- 5000:5000
I'm trying to pass in these variables to Docker (from here), but when I run docker-compose up, I'm running into an error in my node app because it doesn't have access to these variables. What am I doing wrong? Thanks.
Change the file name to something different like node.env. The .env file is used by the docker-compose itself to define the content of the docker-compose file itself.
Example:
$ cat .env
TAG=v1.5
$ cat docker-compose.yml
version: '3'
services:
web:
image: "webapp:${TAG}"
The content of this file is not used inside the docker instance itself.

docker-compose is not setting container_name from environment variable

I am working on a docker-compose file, in which I need to specify container_name from an environment variable.
My docker-compose.yml file looks like this:
version: '3.0'
services:
jenkins:
environment:
- INSTANCE_NAME=team_1
image: my_image
container_name: container_$INSTANCE_NAME
ports:
- "80:80"
expose:
- "80"
So, I think, when I run docker-compose up it should create container as name, container_team_1, but instead of that it runs as contaner_
I also tried this thing using .env file, but still, I can not use environment variable in container_name,
although, if I run docker-compose config I can see all variables set like follow,
container_name: container_
environment:
COMPANY_NAME: team_1
but, Actually it is not attaching in container-name.
You cannot use environment variables defined in docker-compose.yml to variable substitution.
Docker Compose uses .env by default so it should work when you define in .env file
INSTANCE_NAME=team_1
And then run docker-compose up
As I can see in variable substitution section of the docker-compose documentation, you will need to set your $INSTANCE_NAME in the shell that is running the docker-compose up, because:
Compose uses the variable values from the shell environment in which docker-compose is run.
First of all, do something like:
export INSTANCE_NAME=my_instance`
and then:
docker-compose up
Best regards.

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