Dotnet Core docker cmd args - docker

Trying to access the command line args from a dotnet core console application in docker.
This is basically just the default template with default docker compose / dockerfile template.
Tried a few different approaches.
Add args to ENTRYPOINT in dockerfile
Added args to CMD in dockerfile
Added args under build in the docker-compose file
Cant get it to pass it on, how is this usually handled?
Test repo: https://github.com/lasrol/DotnetCoreDockerArgs

CMD is meant as an alternative to ENTRYPOINT, or a way to supply arguments to an entrypoint.
Rather than doing:
ENTRYPOINT ["dotnet", "TestDocker.dll", $arg1, $arg2]
CMD ["arg1", "arg2"]
Which will repeat the arguments,
Try:
ENTRYPOINT ["dotnet", "TestDocker.dll", "arg1", "arg2"]
or if you want to use both, simply use CMD for all the arguments only.
ENTRYPOINT ["dotnet", "TestDocker.dll"]
CMD ["arg1", "arg2"]
https://docs.docker.com/engine/reference/builder/#cmd

Related

How to concat arguments to an existing Dockerfile CMD?

Suppose we have a Dockerfile with some CMD and we produced and image from it. Now suppose that we write a docker-compose and one of the services is built from that image.
What I want to do is running the same command but concatenating my own new parameters.
As an example, suppose the original CMD is
java -jar app.jar --argumentA=valA
I want the command to be
java -jar app.jar --argumentA=valA --argumentB=valB
Is it possible?
I'm not entirely sure if this is what you would want to accomplish, but...
Dockerfile exposes both ENTRYPOINT and CMD for being able to execute commands. These also can be used in conjunction, but in this case the ENTRYPOINT will be the command what we want to execute and the CMD will represent some default arguments for the ENTRYPOINT (docs).
For example:
FROM openjdk:11
COPY . /target/app.jar
ENTRYPOINT ["java", "-jar", "app.jar", "--argumentA=valA"]
CMD ["--argumentB=valB"]
The --argumentB=valB will be appended to the java -jar app.jar --argumentA=valA, if we run the image like this:
docker build -t app .
docker run app # # the command executed will be java -jar app.jar --argumentA=valA --argumentB=valB
But the CMD part will be overridden if we provide other arguments when we run the docker image:
docker build -t app .
docker run app --argumentA=valC # the command executed will be java -jar app.jar --argumentA=valA --argumentB=valC
Also, we can commit the CMD and have the ENTRYPOINT only, if we don't require some defaults to be appended to the ENTRYPOINT.

How to pass arguments to a docker container without specifying a CMD

Currently if I run a container I must speicfy a new CMD in order to pass args.
I.e. the format is docker run image [CMD] [ARGS]
Is there any way to pass args to the CMD at the end of the Dockerfile without specifying a new CMD when running the container.
The reason this probably works for you is that usually, the default ENTRYPOINT is /bin/bash -c, and then when you add CMD with executable it adds to the ENTRYPOINT to run the CMD contents as you'd expect.
CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
Dockerfile docs
This means your main option is to replace the old CMD with an ENTRYPOINT in the new Dockerfile and append the runtime arguments with a new CMD directive, either with permanent default values, ones received from environment variables, or a combination of the two
Hopes this helps
You can write your CMD statement like this at end of the Dockerfile.
CMD ["executable","param1","param2"]
source: https://docs.docker.com/engine/reference/builder/

Dockerfile ENTRYPOINT with parameters

I need to run the following on ENTRYPOINT[..]:
dotnet reportgenerator -reports:coverage.cobertura.xml -targetdir:Reports -reportTypes:htmlInline
How do I do this?
Try execform of ENTRYPOINT:
ENTRYPOINT ["dotnet","reportgenerator","-reports:coverage.cobertura.xml","-targetdir:Reports","-reportTypes:htmlInline"]
Apart from execform there is the shell form, which looks like this:
ENTRYPOINT command param1 param2
In the ENTRYPOINT array, you should include stable default(they are not considered changeable) commands with their arguments. Then if you wish to set additional defaults that are more likely to be changed use CMD.
Example taken from the official's documentation link:
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
Did you try?
ENTRYPOINT ["dotnet", "reportgenerator", "-reports:coverage.cobertura.xml", "-targetdir:Reports", "-reportTypes:htmlInline"]

How to use CMD or ENTRYPOINT with based image that uses ENTRYPOINT

I am using base image ibmcom/mq which uses ENTRYPOINT to execute its process:
ENTRYPOINT ["mq.sh"]
If in my Dockerfile I use CMD the parent image works fine, but my CMD doesn't seem to be executed. If in my Dockerfile I use ENTRYPOINT my command is running but then the parent ENTRYPOINT doesn't seem to be running.
What am i missing here?
OK. I now understand that if I use CMD it acts as a parameter to the ENTRYPOINT and if I use ENTRYPOINT it overrides it. I thought this is so only within the same Dockerfile.

Docker CMD when base image has a CMD?

I have a Dockerfile which starts with:
FROM puppet/puppetserver
When I look at the source container it is built from another:
FROM puppet/puppetserver-standalone:5.0.0
The second contains a CMD command:
ENTRYPOINT ["dumb-init", "/docker-entrypoint.sh"]
CMD ["foreground" ]
In my own container I end with:
COPY start.sh /
CMD /start.sh
The CMD run but with unexpected results:
puppetserver: '/bin/sh' is not a puppetserver command. See 'puppetserver --help'.
I know that I have bash availible because I'm using RUN commands.sh before CMD in the same Dockerfile.
How do CMD commands stack when inheriting from base images?
Is my CMD not run as a normal bash command and instead run in conjunction with the CMD of the base image?
You need to reset the ENTRYPOINT from the parent image
COPY start.sh /
ENTRYPOINT []
CMD /start.sh
See https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact
CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.
and https://docs.docker.com/engine/reference/builder/#cmd
There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

Resources