Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

how to create port forwarding inside a dockerfile

Writer Matthew Martinez

I am installing application on docker container which needs to port 8080

To be forward from the host to the container,i have started the container with:

docker run -p 8080:8080 "image id" 

I am able to get to the application inside the docker container via browser using the below address :

docker_parent_host:8080

i need to see the container ports like this:

docker port 7d04524erte
3306/tcp -> 0.0.0.0:3306
80/tcp -> 0.0.0.0:80
8080/tcp -> 0.0.0.0:8080

i have tried to use expose in the dockerfile but is not giving me the same results,its giving me :

80/tcp

how can i get the definition of port forwarding in the dockerfile same as mentioning -p 8080 ?

1 Answer

How can I get the definition of port forwarding in the Dockerfile same as mentioning -p 8080?

You can't...

With the Dockerfile you can only expose the port to the host (i.e: using EXPOSE) - you cannot automatically forward it as well.

The -P (or --publish-all) option will forward all EXPOSE'd ports for you, but this is still not necessarily ideal, as it will use random ports on this host interfaces (use docker port ${CONTAINER} to check).


You can however automate it using Docker Compose.

For example, you might have the following in docker-compose.yml:

version: '2'
services: myservice: restart: "no" build: . ports: - 8080:8080/tcp

Then run docker-compose up -d myservice.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy