skip to Main Content

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


  1. Chosen as BEST ANSWER

    Figured it out. needed to update iptables to allow port 8080 on network host.

    sudo iptables -D INPUT -i eth0 -p tcp -m tcp --dport 8080 -m comment --comment "# jenkins #" -j ACCEPT
    

  2. 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.

    Login or Signup to reply.
  3. 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:

    docker run -id --name jenkins --network=host jenkins/jenkins:lts
    

    if the issue still persists, you can check the following:

    • make sure the container is running
    • make sure that there is no other process is running on port 8080
    • make sure that you enabled the port 8080 for your ec2
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search