5

I'm new to Docker and I'm trying to figure out if is possible to run an application multiple times using different argument.

I developed my application using .Net, and so far I've executed all the instances using Ubuntu service like:

[Unit]
Description=swp %I

[Service]
Environment=ENV=PRODUCTION
Type=simple
User=foo
ExecStart=/usr/bin/dotnet /opt/swp/app.dll %i
WorkingDirectory=/opt/app
Restart=always
RestartSec=5

Where %I is the argument which I pass when I start the service like:

sudo systemctl start app@argument1.service
sudo systemctl start app@argument2.service
sudo systemctl start app@argument3.service

So I have three instances of the application which run with different logic, based on the argument provided.

How can I replicate the same using Docker?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
sfarzoso
  • 1,356
  • 2
  • 24
  • 65
  • Take a look at the [`docker run` documentation](https://docs.docker.com/engine/reference/run/). Most of the options in your service file map to some `docker run` options. – jkr Sep 23 '20 at 20:04

2 Answers2

5

This is completely possible to do using docker. The first step you will have to take is to modify your application from using command-line arguments to accepting environment variables. After this docker provides an easy method of using environment variables.

Docker Run

For example you can set environment variables in a service’s containers with the ‘environment’ key, just like with docker run -e VARIABLE=VALUE

docker run -e DEBUG=1 web python console.py

ENV_FILE

Or you could make use of an env file. The example below shows an env_file setting the NODE_ENV environment variable to 'test'

$ cat ./Docker/api/api.env
NODE_ENV=test

Docker Compose

Just like with docker run -e, you can set environment variables on a one-off container with docker-compose run -e

docker-compose run -e DEBUG=1 web python console.py

You can also check out this question for more examples.

If it is not possible or too inconvenient to modify your program to accept environment variables you can easily and an ARG in your dockerfile to modify how your CMD is run on startup.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
DCCoder
  • 1,587
  • 4
  • 16
  • 29
5

The way to do it on Docker is to use a CMD and ENTRYPOINT couple.

The table in the Docker documentation is maybe the more convenient way to understand it and is way better than a long description.

But in short, when you are using the "parameters" form of CMD you can act on the ENTRYPOINT of your container and so the executable that it is going to run.

Here is a really simplified form of what you could do with your existing application, just removing the echo in the ENTRYPOINT of this dummy example should lead you to your desired behaviour already:

FROM ubuntu

ENTRYPOINT ["echo", "/usr/bin/dotnet", "/opt/swp/app.dll"]
## This would be the default service, 
## if we run the container without any parameter
CMD ["app@argument1.service"] 
  1. Running it without argument:
    docker run 31f3db535473 
    /usr/bin/dotnet /opt/swp/app.dll app@argument1.service
    
  2. Running it with the second service as an argument:
    docker run 31f3db535473 app@argument2.service
    /usr/bin/dotnet /opt/swp/app.dll app@argument2.service
    
  3. Running it with the third service as an argument:
    docker run 31f3db535473 app@argument3.service
    /usr/bin/dotnet /opt/swp/app.dll app@argument3.service
    
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83