Reference env variable from host at runtime in "env-file" to be passed to docker image - docker

Is there a syntax to reference an environment variable from the host in a Docker env-file.
Specifically I'd like to do something like DOCKER_HOST=${HOSTNAME} where HOSTNAME would come the environment of the machine hosting the docker image.
The above doesn't get any attempt at replacement whatsoever and gets passed into the Docker image literally as ${HOSTNAME}.

This is generally not done at the image level, but at runtime, on docker run:
See "How to get the hostname of the docker host from inside a docker container on that host without env vars"
docker run .. -e HOST_HOSTNAME=$(hostname) ..
That does use an environment variable.
You can do so without environment variables, using -h
docker run -h=$(hostname)
But that does not work when your docker run is part of a docker compose. See issue 3840.

Related

How to set environment variable in docker desktop in windows?

I used command "docker pull mysql:5.7.28" which showed image and container correctly in docker desktop but when trying to run the container it showed exited and error was MYSQL_ROOT_PASSWORD required.
So I need to edit MYSQL_ROOT_PASSWORD in yaml file to resolve this issue.
Now the problem is simple I have not used docker-compose file to setup the container and unable to find option in docker desktop to set up this variable.
You can set the environment variable when you run the container with docker run - see, e.g. "Start a mysql server instance" on https://hub.docker.com/_/mysql.
An alternative would be to create a docker-compose.yml and set the environment variable there (the reference for what you can put in Compose files is here).
There might be a way to set environment variables in Docker Desktop, but I don't use it, so I don't know. The documentation should tell you, though.

How do I pass in configuration settings to a docker image for local development?

I'm working on a dotnet core docker container (not aspnet), I'd like to specify configuration options for it through appsettings.json. These values will eventually be filled in through environment variables in kubernetes.
However, for local development, how do we easily pass in these settings without storing them in the container?
You can map local volumes to docker -v local_path:container_path.
If you gonna use kubernetes you can use ConfigMap as well.
You can pass env variables while running the container with -e flag of the command docker run.
With this method, you’ll have to pass each variable in the command line. For example, docker run -e VAR1=value1 -e VAR2=value2
If this gets cumbersome, you can write these values to an env file and use this file like so, docker run --env-file=filename
For reference, you can check out the official docs.

Running testcontainers inside a Docker container for Windows

As said in documentation, if I want to run testcontainers inside a docker I have to consider the following points:
The docker socket must be available via a volume mount
The 'local' source code directory must be volume mounted at the same path inside the container that Testcontainers runs in, so that Testcontainers is able to set up the correct volume mounts for the containers it spawns.
How to comply with 2nd point, mainly with the -v $PWD:$PWD condition if I use Docker for Windows?
On windows, instead socket, docker uses named pipes.
docker run -v \\.\pipe\docker_engine:\\.\pipe\docker_engine
But you need Windows v1709 and special version of Docker for Windows, since this feature is experimental.
More info:
https://blog.docker.com/2017/09/docker-windows-server-1709/
As for the $PWD, on windows cmd you can use %CD% variable which does this same job. Powershell also has a $pwd, same as in linux. But unfortunatelly, they does not work with docker-compose, as they're not true environment variables.
I think easiest would be to execute a short script to create .env file on windows where PWD= will be set to the current dir:
echo PWD=%cd% > .env
and you can use $PWD in docker-compose same as on linux.

Can one determine the hostname of the Docker host from a Container?

This is kind of a duplicate question to this one. . But ironically, in spite of that question's title, all the answers say: Use an ENV variable.
My use case: I don't know if docker is running via docker-compose or swarm. However, it will not be docker run! I am trying to kick off an upgrade script that resides on the host. Thus, from within a container I need the docker host name. Is there any programmatic way to get this WITHOUT environment variables?
Here is the feature request that has been implemented in 17.10 https://github.com/moby/moby/issues/30966
This is how you do it:
$ hostname testing123
$ docker service create \
--name foo \
--hostname "{{.Service.Name}}-{{.Task.Slot}}-{{.Node.Hostname}}" \
nginx:alpine
$ docker inspect foo.1.k51r3eclvcelsxy6jthtavkwa --format
'{{.Config.Hostname}}'
foo-1-testing123
This should also work in docker-compose
services:
nginx:
image: nginx
hostname: '{{.Node.Hostname}}'
No. The goal of docker is to isolate the host environment as much as possible. The only way you could get the hostname is if you were to pass it into the container as a variable (or bind-mount a file containing the hostname, etc).
I don't know if docker is running via docker-compose or swarm. However, it will not be docker run! I am trying to kick off an upgrade script that resides on the host.
Why do you care how the container was started? How do you plan to run a script on the host? The answers to these questions might help us provide a better answer to your original question.
If you're using Docker for Mac, you can use the following hostname:
docker.for.mac.localhost

Docker retain ENV variables

I am running a docker container using --env VAR="foo" to set a few variables. When I run commands on this running container from the same shell / environment that I started the container, everything is fine.
The problem is that now I want to run commands against this container from cron. When cron runs the same command, the ENV variables in the container no longer exist.
How can I persist these ENV variables in the container regardless of where it is accessed from?
Edit:
To clarify, I docker run from a standard shell. In the cron job, I use docker exec and that is when the ENV vars disappear.
I have also noticed that some host machines I can't do any exec's on docker containers from a cron job.
I presume you use docker run inside your cron task.
If that's the case that's normal. You are starting a new container from the same image.
If you want to use the same container (with all your env variables set), you can use docker exec.
https://docs.docker.com/reference/commandline/exec/

Resources