Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign as static port to a container?

Tags:

docker

port

I want to assign a container a port, so that it gets the same port after every restart of the container.

Example: I have a container, which has an Apache in it. The Apache runs on port 80 inside the container. Now, after starting the container, docker assigns a host port to the container port, for example: 49154 -> 80. But the host port changes after restart, depending on the number of running containers. I tried to specify the port in the config.json file of the container, but it gets overwritten.

Is it possible to specify the host port manually?

Thanks in advance and best regards, Chris

like image 781
Chris R. Avatar asked Jun 06 '13 09:06

Chris R.


People also ask

How do I map a container port to a host port?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.


2 Answers

Per the docker.io documentation: https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/

$ sudo docker run -p 80:80 <image> <cmd>

Default port redirects can be built into a container with the EXPOSE build command.

like image 59
Mentor Avatar answered Nov 03 '22 17:11

Mentor


When you start docker, you can use the '-p' parameter.

docker run -p 80 yourimage apache2 will do what you currently have.

Now, you can specify ':' to make this port static:

docker run -p :80 -p :443 yourimage apache2

If you are using a Dockerfile with the EXPOSE instruction, it is the same thing :)

like image 31
creack Avatar answered Nov 03 '22 19:11

creack