Vishnukk
Vishnukk

Reputation: 564

Run docker image without port forwarding

I am running a custom image (based on the docker image Locust) locally using the below command

docker run -p 5557:5557 my-stress-test:0.1

My dockerfile looks as below

FROM locustio/locust:latest

COPY ./ /mnt/locust

CMD ["-P", "5557", "-f", "/mnt/locust/locustfile.py"]

Now, I deploy this image on to my cloud service which runs this image generating the command

docker run -p 5557 my-stress-test:0.1

This is the command I cannot change. However, I am not able to run the image without port forwarding, like -p 5557:5557. How can I change my dockerfile or anything to run the image without port forwarding.

Upvotes: 0

Views: 1669

Answers (2)

Mojtaba Ahadi
Mojtaba Ahadi

Reputation: 349

In dockers you should know how its network works. There is 3 type of port configuration:

docker run my-stress-test:0.1
docker run -p 5557:5557 my-stress-test:0.1
docker run -p 127.0.0.1:5557:5557 my-stress-test:0.1

In the first type, only apps in the same network as this app can connect to that port. In the second type, all apps inside and outside of the container network can connect to that port. In the third type only apps inside the container network, and other apps inside the host can connect to the app, and apps outside of the host cannot connect to the app.

I think the third type is what you are looking for.

If you have multiple network in your host, and you want other apps from other hosts to access the app, you can bind that network ip to the port. for example

docker run -p 192.168.0.10:5557:5557 my-stress-test:0.1

Upvotes: 1

Solowalker
Solowalker

Reputation: 2856

With Docker, you can't publish ports in the dockerfile or at any other time other than at run by design.

How to publish ports in docker files

This question will be best posed to your cloud provider as to why you're unable to change the command that runs a container of your image.

Upvotes: 0

Related Questions