0

On my laptop I want to restrict internet connectivity of a container. But I also want to connect to container from host's IP addresses so I need port mapping/forwarding to work. The problem is that port mapping is not working with my docker network. By not working I mean that container ports are not mapped to host ports.

Here's how I create my docker network:

docker network create -d bridge --attachable --internal nginxnet

I run the container this way:

docker run --rm --name='cntnginx' -p 8088:80  --network=nginxnet  -d nginx:stable

This starts the container successfully. I can access Nginx server using container's IP address. But accessing it as 127.0.0.1:8088 does not work. Also tried running the container with argument -p 0.0.0.0:8088:80. Same issue.

However, if I do not start my container with nginxnet network than ports are correctly forwarded.

docker port cntnginx also shows expected container to host port mapping in the latter case, but not when using the internal only network.

What gives?

I'm using Arch Linux with Docker v20.10.19 and containerd v1.6.8.

Note: I did not have a typo in brower's URL bar. I made sure of mentioning the port as 8088. I also tried with curl.

1 Answer 1

0

The docker command --internal argument conflicts with what you need

--internal Restrict external access to the network

Port mappings need to be in a subnet, which is already satisfied, but --internal isolates them like a firewall, so you can remove it

┌──[[email protected]]-[~]
└─$ docker network create -d bridge --attachable nginx_net
7099c25e475dc829cc6102a168da473e5d9283b82c282d5cd230a40803811983
┌──[[email protected]]-[~]
└─$ docker run  --name=cnt_nginx -p 8088:80  --network=nginx_net  -d nginx
ceea597d085863adaab4d80de6e1e096bebedf8d3455b488e5b8cc6afffd9508
┌──[[email protected]]-[~]
└─$ curl 127.0.0.1:8088
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
┌──[[email protected]]-[~]
└─$
2
  • Hello. I tried this. It does solves the problem of port mapping which I already figured out (see question). But your answer doesn't restrict internet connectivity from container. I can still access the internet within the container. I want to restrict that using docker (and not with a container OS specific tool) or a solution running on host OS.
    – Firelord
    Commented Oct 25, 2022 at 6:05
  • Well, it really does not meet your requirements, and bridging mode is inevitable. Commented Oct 25, 2022 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .