5

my intention is to able to pass some arguments to a docker container when calling the docker-compose for that container.

Something like

docker-compose up -d myContainer myArg1=hallo myArg2=world

My Docker file looks something like:

FROM mcr.microsoft.com/dotnet/core/runtime:2.2 AS final
WORKDIR /app
COPY Myproj/bin/Debug/netcoreapp2.1 .
ENTRYPOINT ["dotnet", "myproj.dll", myArg1, myArg2]

And the docker-compose.yml file looks like this:

version: '3.4'

services:

   myContainer:
      image: ${DOCKER_REGISTRY-}myContainerImage
      build:
         context: ../..
         dockerfile: MyProj/Dockerfile

First of all I wonder if this is something doable and secondly how to do it

1

2 Answers 2

13

There are two "halves" to the command line Docker ultimately runs, the entrypoint and the command. There are corresponding Docker Compose entrypoint: and command: directives to set them. Their interaction is fairly straightforward: they're just concatenated together to produce a single command line.

Given what you show, in your Dockerfile you could write

ENTRYPOINT ["dotnet", "myproj.dll"]

and then in your docker-compose.yml

command: myArg1=hallo myArg2=world

and that would ultimately launch that process with those arguments.

If you want to be able to specify this from the command line when you spin up the Compose stack, Compose supports ${VARIABLE} references everywhere so you could write something like

command: myArg1=${MY_ARG1:-hallo} world

and then launch this with a command like

MY_ARG1=greetings docker-compose up

A very typical pattern in Docker is to use ENTRYPOINT for a wrapper script that does some first-time setup, and then runs the command as the main container process with exec "$@". The entrypoint gets the command-line arguments as parameters and can do whatever it likes with them, including reformatting them or ignoring them entirely, and you could also use this to implement @Kevin's answer of configuring this primarily with environment variables, but translating that back to var=value type options.

Sign up to request clarification or add additional context in comments.

4 Comments

what should I use in the command line ? If I write "MY_ARG1=greetings docker-compose up" it won't work. The command line should being with docker-compose.
Do you need to be able to specify the arguments every time you run docker-compose up, or is including it in the YAML file enough?
specify them everytime
docker-compose up doesn't take parameters in the syntax you show in the question, so the only way to pass them in like this is using environment variables. If you need to explicitly specify the entire command line, Docker Compose is a little bit of a mismatch for your needs and a plain docker run might work better (or even just run the tool outside of Docker).
0

I think you need to use environnement variables, you can launch your command with it.

Something like this: (there are several ways to do this)

In compose: https://docs.docker.com/compose/environment-variables/

environment:
- myVar

In dockerfile: https://docs.docker.com/engine/reference/builder/#env

ENTRYPOINT ["dotnet", "myproj.dll", "$myVar"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.