Exposing a port on a live Docker container

reberhardt

I'm trying to create a Docker container that acts like a full-on virtual machine. I know I can use the EXPOSE instruction inside a Dockerfile to expose a port, and I can use the -p flag with docker run to assign ports, but once a container is actually running, is there a command to open/map additional ports live?

For example, let's say I have a Docker container that is running sshd. Someone else using the container ssh's in and installs httpd. Is there a way to expose port 80 on the container and map it to port 8080 on the host, so that people can visit the web server running in the container, without restarting it?

SvenDowideit

You cannot do this via Docker, but you can access the container's un-exposed port from the host machine.

If you have a container with something running on its port 8000, you can run

wget http://container_ip:8000

To get the container's IP address, run the 2 commands:

docker ps
docker inspect container_name | grep IPAddress

Internally, Docker shells out to call iptables when you run an image, so maybe some variation on this will work.

To expose the container's port 8000 on your localhost's port 8001:

iptables -t nat -A  DOCKER -p tcp --dport 8001 -j DNAT --to-destination 172.17.0.19:8000

One way you can work this out is to setup another container with the port mapping you want, and compare the output of the iptables-save command (though, I had to remove some of the other options that force traffic to go via the docker proxy).

NOTE: this is subverting docker, so should be done with the awareness that it may well create blue smoke.

OR

Another alternative is to look at the (new? post 0.6.6?) -P option - which will use random host ports, and then wire those up.

OR

With 0.6.5, you could use the LINKs feature to bring up a new container that talks to the existing one, with some additional relaying to that container's -p flags? (I have not used LINKs yet.)

OR

With docker 0.11? you can use docker run --net host .. to attach your container directly to the host's network interfaces (i.e., net is not namespaced) and thus all ports you open in the container are exposed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exposing a Docker container port

From Dev

Docker container for Postgres 9.1 not exposing port 5432 to host

From Dev

docker-machine, create and run a nginx container is not exposing port 80

From Dev

Exposing Docker Container Ports

From Dev

docker not exposing the port with network host

From Dev

prevent Docker from exposing port on host

From Dev

Exposing a WSGI app from a Docker container

From Dev

Exposing a WSGI app from a Docker container

From Dev

Docker: Connect to container (without exposing ports)

From Dev

Exposing Redis DB docker container to a NodeJS docker container

From Dev

How to host two Docker containers exposing port 80 on the same server

From Dev

AWS Docker deployment not exposing port to outside world with Dockerrun file

From Dev

How to host two Docker containers exposing port 80 on the same server

From Dev

What is the difference between exposing docker port and creating bridge(or overlay)?

From Dev

Docker - Exposing container directories to host directories without obscuring original contents

From Java

Forward host port to docker container

From Java

Change container port in docker compose

From Dev

Port based routing in docker container

From Dev

How to port forward in Docker container?

From Dev

Port data out of docker container

From Dev

Bind docker container port to path

From Dev

Docker - connecting to an open port in a container

From Dev

docker container port accessed from another container

From Dev

docker container port format does not looks right(like <port>-<port>)

From Java

How to access host port from docker container

From Dev

Remove port binding from an existing docker container

From Dev

docker elasticsearch container not forwarding port (macOs)

From Dev

docker within-container port forwarding

From Dev

How to expose port from host to container in Docker?

Related Related

  1. 1

    Exposing a Docker container port

  2. 2

    Docker container for Postgres 9.1 not exposing port 5432 to host

  3. 3

    docker-machine, create and run a nginx container is not exposing port 80

  4. 4

    Exposing Docker Container Ports

  5. 5

    docker not exposing the port with network host

  6. 6

    prevent Docker from exposing port on host

  7. 7

    Exposing a WSGI app from a Docker container

  8. 8

    Exposing a WSGI app from a Docker container

  9. 9

    Docker: Connect to container (without exposing ports)

  10. 10

    Exposing Redis DB docker container to a NodeJS docker container

  11. 11

    How to host two Docker containers exposing port 80 on the same server

  12. 12

    AWS Docker deployment not exposing port to outside world with Dockerrun file

  13. 13

    How to host two Docker containers exposing port 80 on the same server

  14. 14

    What is the difference between exposing docker port and creating bridge(or overlay)?

  15. 15

    Docker - Exposing container directories to host directories without obscuring original contents

  16. 16

    Forward host port to docker container

  17. 17

    Change container port in docker compose

  18. 18

    Port based routing in docker container

  19. 19

    How to port forward in Docker container?

  20. 20

    Port data out of docker container

  21. 21

    Bind docker container port to path

  22. 22

    Docker - connecting to an open port in a container

  23. 23

    docker container port accessed from another container

  24. 24

    docker container port format does not looks right(like <port>-<port>)

  25. 25

    How to access host port from docker container

  26. 26

    Remove port binding from an existing docker container

  27. 27

    docker elasticsearch container not forwarding port (macOs)

  28. 28

    docker within-container port forwarding

  29. 29

    How to expose port from host to container in Docker?

HotTag

Archive