How to transfer to a docker container?

I would like to forward port 8080 to port 80 with iptables in the Docker container. In the assembly, I have an error message, as you can see below.

Here is the Docker file:

FROM fedora RUN whoami && \ iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080 

Here is the result:

 [~]# docker build -t temp /home/edfromhadria/Documents/Docker/temp/. Sending build context to Docker daemon 2.048 kB Sending build context to Docker daemon Step 0 : FROM fedora ---> 834629358fe2 Step 1 : RUN whoami && iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080 ---> Running in 95046cf959bf root iptables v1.4.21: can't initialize iptables table `nat': Permission denied (you must be root) Perhaps iptables or your kernel needs to be upgraded. INFO[0001] The command [/bin/sh -c whoami && iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080] returned a non-zero code: 3 

Thanks in advance for any help you can provide.

+5
source share
1 answer

Firstly, running the iptables command during the docker build process will never make sense; even if it works, the iptables command only changes the configuration of your kernel runtime. These changes will not be saved in the Docker image and will not be available when the container starts.

Secondly, even if you start the iptables container after starting the container (and not when creating the container), it will still fail, because Docker containers by default do not have the necessary rights to change the iptables configuration (either change the network as a whole or mount file systems, etc.). You can run a container with the --privileged flag, but this is probably not what you want to do (since it gives a number of additional privileges on the container that are probably not needed, and from a security point of view, it is a good idea to only grant privileges which are absolutely necessary).

You usually use this with the Docker -p option to connect your host ports to ports in your container, for example:

 docker run -p 80:8080 temp 

This will connect port 80 on your host to port 8080 on the container.

If this is not what you need, the easiest way is to configure the application in your container to work on the desired port.

+9
source

All Articles