Dynamically assigns different value for the environment variable upon a Docker service scale - docker

For some situation, we might need to scale up the service and assign the different value for the environment variable, for example, NODE_ID (to be used internally).
Usually, I create a script to run my service with a dynamic parameter as preferred scale.
$ docker run -e NODE_ID=node_01 ...
$ docker run -e NODE_ID=node_02 ...
...
$ docker run -e NODE_ID=node_20 ...
Question
Is there any way to achieve this with the docker swarm mode, e.g.
$ docker service create ... ?

I believe the only value you can get it the container id by using $(hostname). That gives you a unique value for each container. There is no way to provide a custom one.

Related

Pass NEPTUNE_API_TOKEN environment variable via docker run command

Using the docker run command, I'm trying to pass my NEPTUNE_API_TOKEN to my container.
My understanding is that I should use the -e flag as follows: -e ENV_VAR='env_var_value' and that might work.
I wish, however, to use the value existing in the already-running session, as follows:
docker run -e NEPTUNE_API_TOKEN=$(NEPTUNE_API_TOKEN) <my_image>
However, after doing so, NEPTUNE_API_TOKEN is set to empty when checking the value inside the container.
My question is whether I'm doing something wrong or if this is not possible and I must provide an explicit Neptune API token as a string.
$(NEPTUNE_API_TOKEN) is the syntax for running a command and grabbing the output. Use $NEPTUNE_API_TOKEN.
You can set up and pass NEPTUNE_API_TOKEN as a:
Docker run command options environment variable
Example: docker run -e NEPTUNE_API_TOKEN="<YOUR_API_TOKEN>" <image-name>
Dockerfile environment variable
Docker secret
Neptune will work with any of the methods described above.
For your case, I believe using method 2 and 3 will work best as you will set the API token only once and all containers can reuse it. Additionally, they are more secure methods.
You can read this guide on how to use Neptune with Docker I created last year.
Docs: https://docs.neptune.ai/how-to-guides/automation-pipelines/how-to-use-neptune-with-docker

How to use export command to set environment variable with docker exec?

I have a running docker container using an ancestor my_base_image. Now when the container is running, can I set an environment variable using export command with docker exec? if yes, how?
I tried using the following, but doesn't work
docker exec -i -t $(docker ps -q --filter ancestor=`my_base_image`) bash -c "export my_env_var=hey"
Basically I want to set my_env_var=hey as env variable inside docker container. I know this can be done in may ways using .env_file or env key docker-compose & ENV in Dockerfile. But I just want to know if it is possible using docker exec command
This is impossible. A process can never change the environment of any other process beyond itself, except that it can specify the initial environment of processes it starts itself. In this case, your docker exec shell isn’t launching the main container process, so it can’t change that process’s environment variables.
This is one of a number of changes that you will need to stop, delete, and recreate the container for. You should treat this as extremely routine container maintenance and plan to delete the container eventually. That means, for example, keeping any data that needs to be persisted outside the container, ideally in an external database but possibly in a mounted volume.

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.

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

How to dynamically set environment variables of linked containers?

I have two containers webinterface and db, while webinterface is started using the --link option (for db) which generates the environment variables
DB_PORT_1111_TCP=tcp://172.17.0.5:5432
DB_PORT_1111_TCP_PROTO=tcp
DB_PORT_1111_TCP_PORT=1111
DB_PORT_1111_TCP_ADDR=172.17.0.5
...
Now my webinterface container uses a Dockerfile where some static environment variables are defined to define the connection:
ENV DB_HOST localhost
ENV DB_PORT 2222
Knowing that there is also an -e option for docker run, the problem is that I want to use those variables in the Dockerfile (used in some scripts) but overwrite them with the values generated with the --link option, i.e. something like:
docker run -d -e DB_HOST=$DB_PORT_1111_TCP_ADDR
This would use the host's defined environment variable which doesn't work here.
Is there a way to handle this?
This is a variable expansion issue so to resolve try the following:
docker run -d -e DB_HOST="$DB_PORT"_1111_TCP_ADDR
With a Unix process that is already running, its environment variables can only be changed from inside the process, not from the outside, so their are somewhat non-dynamic by nature.
If you find Docker links limiting, you are not the only person out there. One simple solution to this would be using WeaveDNS. With WeaveDNS you can simply use default ports (as with Weave overlay network there is no need to expose/publish/remap any internal ports) and resolve each component by via DNS (i.e. your app would just need to look for db.weave.local, and doesn't need to be aware of clunky environment variable scheme that Docker links present). To get a better idea of how WeaveDNS works, checkout one of the official getting started guides. WeaveDNS effectively gives you service discovery without having to modify the application you have.

Resources