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?
Read Ricardo's response first. This worked for me.
However, there exists a scenario where this won't work if the running container was kicked off using docker-compose. This is because docker-compose (I'm running docker 1.17) creates a new network. The way to address this scenario would be
docker network ls
Then append the following
docker run -d --name sqlplus --link db:db -p 1521:1521 sqlplus --net network_name
IPtables hacks don't work, at least on Docker 1.4.1.
The best way would be to run another container with the exposed port and relay with socat. This is what I've done to (temporarily) connect to the database with SQLPlus:
Dockerfile:
Here are some solutions:
https://forums.docker.com/t/how-to-expose-port-on-running-container/3252/12
While you cannot expose a new port of an existing container, you can start a new container in the same Docker network and get it to forward traffic to the original container.
Worked Example
Launch a web-service that listens on port 80, but do not expose its internal port 80 (oops!):
Find its Docker network IP:
Launch
verb/socat
with port 8080 exposed, and get it to forward TCP traffic to that IP's port 80:You can now access pastebin on http://localhost:8080/, and your requests goes to
socat:1234
which forwards it topastebin:80
, and the response travels the same path in reverse.It's not possible to do live port mapping but there are multiple ways you can give a Docker container what amounts to a real interface like a virtual machine would have.
Macvlan Interfaces
Docker now includes a Macvlan network driver. This attaches a Docker network to a "real world" interface and allows you to assign that networks addresses directly to the container (like a virtual machines bridged mode).
pipework
can also map a real interface into a container or setup a sub interface in older versions of Docker.Routing IP's
If you have control of the network you can route additional networks to your Docker host for use in the containers.
Then you assign that network to the containers and setup your Docker host to route the packets via the docker network.
Shared host interface
The
--net host
option allows the host interface to be shared into a container but this is probably not a good setup for running multiple containers on the one host due to the shared nature.In case no answer is working for someone - check if your target container is already running in docker network:
If yes, you should run the proxy container in the same network.
Final command: