0


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
  • 8,794
  • 17
  • 73
  • 152
  • 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

4

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
  • 84,915
  • 16
  • 214
  • 203
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
  • 472
  • 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