I have a Jenkins running in a docker container in Linux ec2 instance. I am running testcontainers within it and I want to expose all ports to the host. For that I am using network host.
When I run the jenkins container with -p 8080:8080 everything works fine and I am able to access jenkins on {ec2-ip}:8080
docker run id -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
however, If I want to run the same image using –network=host as I want to expose all ports to the host
docker run id --network=host jenkins/jenkins:lts
{ec2-ip}:8080 becomes unreachable. I can curl to it locally within the container localhost:8080 but accessing jenkins from the browser doesn’t work.
I am not sure how network host would change the way I access jenkins on port 8080. the application should be still available on port 8080 on the host IP address?
3
Answers
Figured it out. needed to update iptables to allow port 8080 on network host.
AFAIU
--network
doesn’t do what you expect it to do.--network
flag allows you to connect the container to a network. For example, when you do--nerwork=host
your container will be able to use the Docker host network stack. Not the other way around. Take a look at the official documentation.Check if you are enabling the port 8080 in the security group for the instance.
When a Docker container is running in the host network mode using the —network=host option, it shares the network stack with the Docker host. This means that the container is not isolated and uses the same network interface as the host.
In your case, you should be able to access the Jenkins from the browser with ec2-ip:8080
I tested it by running Jenkins with the following command:
if the issue still persists, you can check the following: