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

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.

Related

Openshift pass Dockerfile or Docker Image Environment variables while creating app

I am using Openshift ARC Platform to deploy my docker images.
We need to create three projects with a slight change in the environment variables in each of them.
# Dockerfile
ARG X
ENV PROJECT_NAME=$X
ENV SOMETHING abc-${PROJECT_NAME}
ENV DATA_DIR /data/${PROJECT_NAME}
...
Currently, we supply the value of X using --build-arg option during docker build. This requires us to create 3 separate images (with different tags) and then deploy them using oc new-app -n <namespace> --iamge-stream=<image registry location> syntax.
I wonder if there is some strategy widely used so that building 3 different images with only a few changes in ENV variables be avoided and we could just use a single image and provide these variables while creating the app using oc new-app.
No need to add environment variables to Dockerfile, if you need to pass variables to your application running in a pod. Thus you don't need to create several images due to environment variables.
https://docs.openshift.com/container-platform/3.7/dev_guide/application_lifecycle/new_app.html#specifying-environment-variables
In Openshift, you can pass environment variables to a container of an app as below.
oc new-app -e PROJECT_NAME=projectx -e SOMETHING=abc-projectx -e DATA_DIR=/data/projectx

how to Set environment variable in hyperledger fabric chaincode container

As my chaincode will get execute on each chaincode container, I want to set environment variable inside every chaincode container so that i can use this environment variable in my chaincode.
I don't have access to create a chaincode container. It will get created automatically at the time of chaincode instantiation (one docker container per peer). So that i don't have any control to set the environment variable inside chaincode containers.
I also think to update and commit the chaincode containers, but if there are more endorsing peers then this could take unnecessary delay. So according to my understanding, the best way is to set the environment variable at the time of container creation.
Please let me know how to solve above problem?
You do not want to set an environment variable. If there is some type of "configuration" setting you need to pass into chaincode then you should pass it as a parameter to the Init function and then save the value using PutState and retrieve it using GetState as required.
If you want to set environment variables before running a container, use the --env argument for the docker run command:
$ docker run --help
...
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
Use the -e, --env, and --env-file flags to set simple (non-array) environment variables in the container you’re running, or overwrite variables that are defined in the Dockerfile of the image you’re running. More info is here.
docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash
If you want to set environment variables after running a container, Docker does not allow this currently. See these issues:
https://github.com/moby/moby/issues/8838
https://github.com/moby/moby/issues/7561
Right now Docker can't change the configuration of the container once it's created, and generally this is OK because it's trivial to create a new container

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

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.

Docker environmental variables from a file

I am trying to use one Dockerfile for both my production and development. The only difference between the production and development are the environment variables I set. Therefore I would like someway import the environment variables from a file. Before using Docker I would simply do the following
. ./setvars
./main.py
However if change ./main.py with the Docker equivalent
. ./setvars
docker run .... ./main.py
then the variables will be on the host and not accessible from the Docker instance. Of course a quick and dirty hack would be to make a file with
#!/bin/bash
. ./setvars
./main.py
and run that in the instance. That would however be really annoying, since I got lots of scripts I would like to run (with the same environment variables), and would then have to create a extra script for everyone of those.
Are there any other solution to get my environment variables inside docker without using a different Dockerfile and the method I described above?
Your best options is to use either the -e flag, or the --env-file of the docker run command.
The -e flag allows you to specify key/value pairs of env variable,
for example:
docker run -e ENVIRONMENT=PROD
You can use several time the -e flag to define multiple env
variables. For example, the docker registry itself is configurable
with -e flags, see:
https://docs.docker.com/registry/deploying/#running-a-domain-registry
The --env-file allow you to specify a file. But each line of the file
must be of type VAR=VAL
Full documentation:
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables-e-env-env-file

Is it possible to customize environment variable by linking two docker containers?

I've created a docker image for my database server and one for the web application. Using the documentation I'm able to link between both container using the environment variables as follow:
value="jdbc:postgresql://${DB_PORT_5432_TCP_ADDR}:${DB_PORT_5432_TCP_PORT}/db_name"
It works fine now but it would be better that the environment variables are more general and without containing a static port number. Something like:
value="jdbc:postgresql://${DB_URL}:${DB_PORT}/db_name"
Is there anyway to link between the environment variables? for example by using the ENV command in the dockerfile ENV DB_URL=$DB_PORT_5432_TCP_ADDR or by using the argument --env by running the image docker run ... -e DB_URL=$DB_PORT_5432_TCP_ADDR docker_image ?
Without building this kind of functionality into your docker startup shell scripts or other orchestration mechanism, this is not possible at the moment to create environment variables like you are describing here. You do mention a couple of workarounds. However, the problem at least with using the -e DB_URL=... in your docker run command is that your $DB_PORT_5432_TCP_ADDR environment variable is not known at runtime, and so you will not be able to set this value when you run it. Typically, this is what your orchestration layer is used for, service discovery and passing this kind of data among your containers. There is at least one workaround mentioned here on SO that involves constructing a special shell script that you put in your CMD or ENTRYPOINT directives that passes the environment variable to the container.

Resources