Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command line arguments to a dotnet dll in a Docker image at run time?

Tags:

.net

docker

Working on my first Docker image. It is a dotnet program that uses CMD to launch (only one CMD allowed in Docker). I would like to pass the program an argument (an API key) at runtime. After some googling, not finding a clear answer. Entrypoint doesn't seem helpful. Maybe ENV, but it seems ENV is only for Docker. My Dockerfile:

FROM microsoft/dotnet
WORKDIR /app
COPY . /app
CMD [ "dotnet",  "/app/netcore/Somename.dll"]

Thanks

like image 212
Gerry Avatar asked Jan 02 '23 07:01

Gerry


1 Answers

Docker joins ENTRYPOINT and CMD into single command line, if both use JSON notation, like in your example.

This is JSON notation: CMD [ "dotnet", "/app/netcore/Somename.dll"]

This is shell notation: CMD dotnet /app/netcore/Somename.dll

Another thing you need to know - what is written in docker run ... <image_name> ... after - considered as CMD.

So, to conclude.

  1. The constant (immutable) part of command line, like dotnet foo.dll you can put in ENTRYPOINT.

  2. Variable part, like additional arguments, you supply with docker run and optionally put defaults to CMD in Dockerfile

Example:

Dockerfile

...
ENTRYPOINT ["dotnet", "/app/netcore/Somename.dll"]
CMD ["--help"]

Command line 1:

docker run ... <your image name> --environment=Staging --port=8080 

Will result in dotnet /app/netcore/Somename.dll --environment=Staging --port=8080

Command line 2:

docker run ... <your image name>

Will result in dotnet /app/netcore/Somename.dll --help. --help comes from default value, defined in Dockerfile.

like image 195
grapes Avatar answered Jan 13 '23 14:01

grapes