How can I get a docker container to expose a port while blocking the internet at large?

Viewed 296

I want to run a docker container that has no access to the outside internet. I've been using --network=none for this successfully. But now I want to host a web server from that container, and access it from outside. When I try, I find that the port mapping is totally ignored:

$ docker run --rm -it -p 8000:8000 --network=none python bash
# python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Now from outside the container:

$ docker port 981f253788ad 
$ curl localhost:8000
curl: (7) Failed to connect to localhost port 8000: Connection refused
1 Answers

You can try using a custom network with --internal option and then attaching your container to this network:

$ docker network create --internal internal-network
$ docker run --rm -it -p 8000:8000 --network=internal-network python bash

Related