4

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

user1472131
  • 411
  • 1
  • 5
  • 15
  • Does this answer your question? [Pass argument to docker compose](https://stackoverflow.com/questions/43544328/pass-argument-to-docker-compose) – KarateKid Dec 13 '19 at 08:35

2 Answers2

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.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • 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. – user1472131 Dec 13 '19 at 13:23
  • 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? – David Maze Dec 13 '19 at 13:27
  • specify them everytime – user1472131 Dec 13 '19 at 13:34
  • 1
    [`docker-compose up`](https://docs.docker.com/compose/reference/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). – David Maze Dec 13 '19 at 13:53
1

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"]
Kevin
  • 78
  • 1
  • 9