-1


I want to dockerize a NET Core Application.
However i do not know how I can provide the port as a command-line argument to further use it in the Dockerfile like:
ENTRYPOINT ["appname","[port_argument_from_commandline]"]".

Dockerfile

FROM microsoft/dotnet:latest AS base
WORKDIR /app
COPY ./bin/Release/netcoreapp2.1/publish /app
ENTRYPOINT [ "dotnet","DockerContainerDaemon.dll" ]
EXPOSE 20000   //how can i set this as a command-line argument?

Further clarification: I want to provide my image with a configurable argument in our case lets say port.Then i want when i run an instance to set this argument with a value:

docker build myapp
//configured to accept a command line argument named port
docker run port=[instance port] myapp

Bercovici Adrian
  • 7,820
  • 11
  • 63
  • 124
  • Well if i lets say expose a `port` e.g `25000` inside the `Dockerfile`.How can i then run multiple instances of this image since all will be exposing the same port? – Bercovici Adrian Oct 01 '18 at 13:08
  • 2
    IME, there are a couple of details like file paths and port numbers _inside the container_ that make no difference. Pick something and document it. If the operator wants a different port published, they can `docker run -p 8080:25000` to pick their own external port. – David Maze Oct 01 '18 at 13:35

2 Answers2

1

Note that the EXPOSE instruction does not actually expose the port, but works as a documentation between the dockerfile writer and the user. Source here

To expose a port, you'll need to pass the following option to your command line

-p hostPort:containerPort

So in your example it could look like :

-p 8080:20000

The whole command could look like

docker run -ti -p 8080:20000 image_name

docker run -ti -p 8081:20000 image_name

docker run -ti -p 8082:20000 image_name

This would work properly and let you have 3 instances connected at the same time.

Community
  • 1
  • 1
Artandor
  • 444
  • 6
  • 20
  • I know that when calling `docker run` you have to `host port: container port` but i did not know how you can use the `port` parameter inside the `Dockerfile` if you for example want to run `ENTRYPOINT ["appname" ,"[port_argument]]"` – Bercovici Adrian Oct 01 '18 at 12:41
  • I try to be careful in SO answers about distinguishing “exposing” ports (“flag it so that `docker run -P` will allocate a port for it and the archaic link feature works”) from “publishing” them (“make this port actually accessible from outside Docker space”) – David Maze Oct 01 '18 at 13:33
  • I'm pretty sure that docker-compose uses EXPOSE for something. Yeah, EXPOSE lets multiple containers defined in one docker-compose talk to each other, see https://stackoverflow.com/a/40801773/3406189 So EXPOSE isn't just documentation but for a single container it can't do anything. – grofte Feb 10 '22 at 15:09
1

You can use build-time variables when running docker build:

 docker build --build-arg PORT=XXXX .

Inside your Dockerfile, you can use this setting with ARG:

FROM microsoft/dotnet:latest AS base
ARG PORT # Supports a default, e.g. ARG PORT=5000
WORKDIR /app
COPY ./bin/Release/netcoreapp2.1/publish /app
ENTRYPOINT [ "dotnet","DockerContainerDaemon.dll" ]
EXPOSE $PORT # Use $PORT here
Kirk Larkin
  • 73,173
  • 13
  • 183
  • 186
  • 1
    Exposing a variable port makes no sense since this line is only for documentation purpose. the only thing that change the exposed port is the -p parameter. If you want to create multiple instance you just need to run multiple container and expose a different host and container port every time. – Artandor Oct 01 '18 at 13:26
  • @Artandor Are you saying that the OP wouldn't want to document the port if it was defined at build-time? It's clear that the OP didn't mean build-time in the original question now, but let's say *that was the case*, because e.g. the port wasn't known until the app itself was built - Are you saying `EXPOSE` would just be omitted entirely? – Kirk Larkin Oct 01 '18 at 13:36
  • @KirkLarkin The expose instruction is for documentation purpose, you could delete it and it would change nothing. Check sources i posted in my answer. Therefore, setting a variable that will make the EXPOSE dynamic is a non sense to me since the documentation won't be relevant and the impact of the line is null. – Artandor Oct 01 '18 at 14:45
  • @Artandor I think you’ve missed the point I was trying to make. That could just be the way I’m articulating it. Anyway, if you don’t like this answer, you can always downvote it. – Kirk Larkin Oct 01 '18 at 14:54