pikapoo
pikapoo

Reputation: 77

Scale docker container with ENV settings

If I have a simple counting program: given a starting number, the program will output 10 numbers from the starting number with increment of 1.

I want to have 0 to 49 as output (order is not a hard requirement). Can I use docker to do something like scale to 5 containers, but setting some ENV as the starting point of each container? Assuming I’m fine with multiple containers will not guarantee the order.

For example: container 1 will start with 0, container 2 will start with 10...container 5 will start at 40. The output may be [0, 9], [40, 49], [30, 39], [10, 19], [20, 29].

Upvotes: 0

Views: 252

Answers (1)

gbryce
gbryce

Reputation: 56

You can certainly achieve this using the -e argument with the docker run command. The sample below shows how to launch containers with an environment variable 0-49 and uses the alpine image for demonstration purposes:

for i in {0..49}
do
    docker run -it -e "IDX_ENV=$i" alpine:latest env
done

Note the output will just print the environment for each invocation:

...
IDX_ENV=0
HOME=/root
...

Each container will, of course, exit after printing the environment. If you need to run something more "real" such as a long running process, I suggest that you run the containers in detached mode with the -d option and set an entrypoint that makes use of the environment passed in.

Upvotes: 2

Related Questions