IPTABLES - How does the Docker port a:b work?












0















I am learning iptables in combination with Docker. I am figuring out how the docker-compose host:port:port for port forwarding actually works. I understood it does some iptables magic. So I did a little test.



First I had this docker-compose.yml:



version: "3"
services:
postgres:
image: postgres:latest
ports:
- 127.0.0.1:5432:5432
networks:
- network
network:
network:


When running this, iptables -S gives:



-P INPUT ACCEPT
-P FORWARD DROP
-P OUTPUT ACCEPT
-N DOCKER
-N DOCKER-ISOLATION
-N DOCKER-USER
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION
-A FORWARD -o br-bd4b05981a0f -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o br-bd4b05981a0f -j DOCKER
-A FORWARD -i br-bd4b05981a0f ! -o br-bd4b05981a0f -j ACCEPT
-A FORWARD -i br-bd4b05981a0f -o br-bd4b05981a0f -j ACCEPT
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A DOCKER -d 172.18.0.2/32 ! -i br-bd4b05981a0f -o br-bd4b05981a0f -p tcp -m tcp --dport 5432 -j ACCEPT
-A DOCKER-ISOLATION -i docker0 -o br-bd4b05981a0f -j DROP
-A DOCKER-ISOLATION -i br-bd4b05981a0f -o docker0 -j DROP
-A DOCKER-ISOLATION -j RETURN
-A DOCKER-USER -j RETURN


I verified that port 5432 was indeed not accessible over the internet and only on localhost. Great!



After this, I rebooted, cleared docker (docker system prune --all --volumes --force) and started with following docker-compose.yml:



version: "3"
services:
postgres:
image: postgres:latest
ports:
- 5432:5432
networks:
- network
network:
network:


Note that 127.0.0.1: is no long present in this configuration. Now, when running iptables -S, I am getting exact the same configuration:



-P INPUT ACCEPT
-P FORWARD DROP
-P OUTPUT ACCEPT
-N DOCKER
-N DOCKER-ISOLATION
-N DOCKER-USER
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION
-A FORWARD -o br-6ada9a016213 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o br-6ada9a016213 -j DOCKER
-A FORWARD -i br-6ada9a016213 ! -o br-6ada9a016213 -j ACCEPT
-A FORWARD -i br-6ada9a016213 -o br-6ada9a016213 -j ACCEPT
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A DOCKER -d 172.18.0.2/32 ! -i br-6ada9a016213 -o br-6ada9a016213 -p tcp -m tcp --dport 5432 -j ACCEPT
-A DOCKER-ISOLATION -i docker0 -o br-6ada9a016213 -j DROP
-A DOCKER-ISOLATION -i br-6ada9a016213 -o docker0 -j DROP
-A DOCKER-ISOLATION -j RETURN
-A DOCKER-USER -j RETURN


However, this time, the service is accessible via internet. This is intended as per the docker-compose.yml I am using this time. However, the iptables configuration is exact the same as the one above. Strange?



How is it possible that two exact iptables configurations have different behavior? I guess I am missing some piece to understand the specifics of the Docket port forwarding functionality.










share|improve this question





























    0















    I am learning iptables in combination with Docker. I am figuring out how the docker-compose host:port:port for port forwarding actually works. I understood it does some iptables magic. So I did a little test.



    First I had this docker-compose.yml:



    version: "3"
    services:
    postgres:
    image: postgres:latest
    ports:
    - 127.0.0.1:5432:5432
    networks:
    - network
    network:
    network:


    When running this, iptables -S gives:



    -P INPUT ACCEPT
    -P FORWARD DROP
    -P OUTPUT ACCEPT
    -N DOCKER
    -N DOCKER-ISOLATION
    -N DOCKER-USER
    -A FORWARD -j DOCKER-USER
    -A FORWARD -j DOCKER-ISOLATION
    -A FORWARD -o br-bd4b05981a0f -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
    -A FORWARD -o br-bd4b05981a0f -j DOCKER
    -A FORWARD -i br-bd4b05981a0f ! -o br-bd4b05981a0f -j ACCEPT
    -A FORWARD -i br-bd4b05981a0f -o br-bd4b05981a0f -j ACCEPT
    -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
    -A FORWARD -o docker0 -j DOCKER
    -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
    -A FORWARD -i docker0 -o docker0 -j ACCEPT
    -A DOCKER -d 172.18.0.2/32 ! -i br-bd4b05981a0f -o br-bd4b05981a0f -p tcp -m tcp --dport 5432 -j ACCEPT
    -A DOCKER-ISOLATION -i docker0 -o br-bd4b05981a0f -j DROP
    -A DOCKER-ISOLATION -i br-bd4b05981a0f -o docker0 -j DROP
    -A DOCKER-ISOLATION -j RETURN
    -A DOCKER-USER -j RETURN


    I verified that port 5432 was indeed not accessible over the internet and only on localhost. Great!



    After this, I rebooted, cleared docker (docker system prune --all --volumes --force) and started with following docker-compose.yml:



    version: "3"
    services:
    postgres:
    image: postgres:latest
    ports:
    - 5432:5432
    networks:
    - network
    network:
    network:


    Note that 127.0.0.1: is no long present in this configuration. Now, when running iptables -S, I am getting exact the same configuration:



    -P INPUT ACCEPT
    -P FORWARD DROP
    -P OUTPUT ACCEPT
    -N DOCKER
    -N DOCKER-ISOLATION
    -N DOCKER-USER
    -A FORWARD -j DOCKER-USER
    -A FORWARD -j DOCKER-ISOLATION
    -A FORWARD -o br-6ada9a016213 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
    -A FORWARD -o br-6ada9a016213 -j DOCKER
    -A FORWARD -i br-6ada9a016213 ! -o br-6ada9a016213 -j ACCEPT
    -A FORWARD -i br-6ada9a016213 -o br-6ada9a016213 -j ACCEPT
    -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
    -A FORWARD -o docker0 -j DOCKER
    -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
    -A FORWARD -i docker0 -o docker0 -j ACCEPT
    -A DOCKER -d 172.18.0.2/32 ! -i br-6ada9a016213 -o br-6ada9a016213 -p tcp -m tcp --dport 5432 -j ACCEPT
    -A DOCKER-ISOLATION -i docker0 -o br-6ada9a016213 -j DROP
    -A DOCKER-ISOLATION -i br-6ada9a016213 -o docker0 -j DROP
    -A DOCKER-ISOLATION -j RETURN
    -A DOCKER-USER -j RETURN


    However, this time, the service is accessible via internet. This is intended as per the docker-compose.yml I am using this time. However, the iptables configuration is exact the same as the one above. Strange?



    How is it possible that two exact iptables configurations have different behavior? I guess I am missing some piece to understand the specifics of the Docket port forwarding functionality.










    share|improve this question



























      0












      0








      0








      I am learning iptables in combination with Docker. I am figuring out how the docker-compose host:port:port for port forwarding actually works. I understood it does some iptables magic. So I did a little test.



      First I had this docker-compose.yml:



      version: "3"
      services:
      postgres:
      image: postgres:latest
      ports:
      - 127.0.0.1:5432:5432
      networks:
      - network
      network:
      network:


      When running this, iptables -S gives:



      -P INPUT ACCEPT
      -P FORWARD DROP
      -P OUTPUT ACCEPT
      -N DOCKER
      -N DOCKER-ISOLATION
      -N DOCKER-USER
      -A FORWARD -j DOCKER-USER
      -A FORWARD -j DOCKER-ISOLATION
      -A FORWARD -o br-bd4b05981a0f -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
      -A FORWARD -o br-bd4b05981a0f -j DOCKER
      -A FORWARD -i br-bd4b05981a0f ! -o br-bd4b05981a0f -j ACCEPT
      -A FORWARD -i br-bd4b05981a0f -o br-bd4b05981a0f -j ACCEPT
      -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
      -A FORWARD -o docker0 -j DOCKER
      -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
      -A FORWARD -i docker0 -o docker0 -j ACCEPT
      -A DOCKER -d 172.18.0.2/32 ! -i br-bd4b05981a0f -o br-bd4b05981a0f -p tcp -m tcp --dport 5432 -j ACCEPT
      -A DOCKER-ISOLATION -i docker0 -o br-bd4b05981a0f -j DROP
      -A DOCKER-ISOLATION -i br-bd4b05981a0f -o docker0 -j DROP
      -A DOCKER-ISOLATION -j RETURN
      -A DOCKER-USER -j RETURN


      I verified that port 5432 was indeed not accessible over the internet and only on localhost. Great!



      After this, I rebooted, cleared docker (docker system prune --all --volumes --force) and started with following docker-compose.yml:



      version: "3"
      services:
      postgres:
      image: postgres:latest
      ports:
      - 5432:5432
      networks:
      - network
      network:
      network:


      Note that 127.0.0.1: is no long present in this configuration. Now, when running iptables -S, I am getting exact the same configuration:



      -P INPUT ACCEPT
      -P FORWARD DROP
      -P OUTPUT ACCEPT
      -N DOCKER
      -N DOCKER-ISOLATION
      -N DOCKER-USER
      -A FORWARD -j DOCKER-USER
      -A FORWARD -j DOCKER-ISOLATION
      -A FORWARD -o br-6ada9a016213 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
      -A FORWARD -o br-6ada9a016213 -j DOCKER
      -A FORWARD -i br-6ada9a016213 ! -o br-6ada9a016213 -j ACCEPT
      -A FORWARD -i br-6ada9a016213 -o br-6ada9a016213 -j ACCEPT
      -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
      -A FORWARD -o docker0 -j DOCKER
      -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
      -A FORWARD -i docker0 -o docker0 -j ACCEPT
      -A DOCKER -d 172.18.0.2/32 ! -i br-6ada9a016213 -o br-6ada9a016213 -p tcp -m tcp --dport 5432 -j ACCEPT
      -A DOCKER-ISOLATION -i docker0 -o br-6ada9a016213 -j DROP
      -A DOCKER-ISOLATION -i br-6ada9a016213 -o docker0 -j DROP
      -A DOCKER-ISOLATION -j RETURN
      -A DOCKER-USER -j RETURN


      However, this time, the service is accessible via internet. This is intended as per the docker-compose.yml I am using this time. However, the iptables configuration is exact the same as the one above. Strange?



      How is it possible that two exact iptables configurations have different behavior? I guess I am missing some piece to understand the specifics of the Docket port forwarding functionality.










      share|improve this question
















      I am learning iptables in combination with Docker. I am figuring out how the docker-compose host:port:port for port forwarding actually works. I understood it does some iptables magic. So I did a little test.



      First I had this docker-compose.yml:



      version: "3"
      services:
      postgres:
      image: postgres:latest
      ports:
      - 127.0.0.1:5432:5432
      networks:
      - network
      network:
      network:


      When running this, iptables -S gives:



      -P INPUT ACCEPT
      -P FORWARD DROP
      -P OUTPUT ACCEPT
      -N DOCKER
      -N DOCKER-ISOLATION
      -N DOCKER-USER
      -A FORWARD -j DOCKER-USER
      -A FORWARD -j DOCKER-ISOLATION
      -A FORWARD -o br-bd4b05981a0f -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
      -A FORWARD -o br-bd4b05981a0f -j DOCKER
      -A FORWARD -i br-bd4b05981a0f ! -o br-bd4b05981a0f -j ACCEPT
      -A FORWARD -i br-bd4b05981a0f -o br-bd4b05981a0f -j ACCEPT
      -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
      -A FORWARD -o docker0 -j DOCKER
      -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
      -A FORWARD -i docker0 -o docker0 -j ACCEPT
      -A DOCKER -d 172.18.0.2/32 ! -i br-bd4b05981a0f -o br-bd4b05981a0f -p tcp -m tcp --dport 5432 -j ACCEPT
      -A DOCKER-ISOLATION -i docker0 -o br-bd4b05981a0f -j DROP
      -A DOCKER-ISOLATION -i br-bd4b05981a0f -o docker0 -j DROP
      -A DOCKER-ISOLATION -j RETURN
      -A DOCKER-USER -j RETURN


      I verified that port 5432 was indeed not accessible over the internet and only on localhost. Great!



      After this, I rebooted, cleared docker (docker system prune --all --volumes --force) and started with following docker-compose.yml:



      version: "3"
      services:
      postgres:
      image: postgres:latest
      ports:
      - 5432:5432
      networks:
      - network
      network:
      network:


      Note that 127.0.0.1: is no long present in this configuration. Now, when running iptables -S, I am getting exact the same configuration:



      -P INPUT ACCEPT
      -P FORWARD DROP
      -P OUTPUT ACCEPT
      -N DOCKER
      -N DOCKER-ISOLATION
      -N DOCKER-USER
      -A FORWARD -j DOCKER-USER
      -A FORWARD -j DOCKER-ISOLATION
      -A FORWARD -o br-6ada9a016213 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
      -A FORWARD -o br-6ada9a016213 -j DOCKER
      -A FORWARD -i br-6ada9a016213 ! -o br-6ada9a016213 -j ACCEPT
      -A FORWARD -i br-6ada9a016213 -o br-6ada9a016213 -j ACCEPT
      -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
      -A FORWARD -o docker0 -j DOCKER
      -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
      -A FORWARD -i docker0 -o docker0 -j ACCEPT
      -A DOCKER -d 172.18.0.2/32 ! -i br-6ada9a016213 -o br-6ada9a016213 -p tcp -m tcp --dport 5432 -j ACCEPT
      -A DOCKER-ISOLATION -i docker0 -o br-6ada9a016213 -j DROP
      -A DOCKER-ISOLATION -i br-6ada9a016213 -o docker0 -j DROP
      -A DOCKER-ISOLATION -j RETURN
      -A DOCKER-USER -j RETURN


      However, this time, the service is accessible via internet. This is intended as per the docker-compose.yml I am using this time. However, the iptables configuration is exact the same as the one above. Strange?



      How is it possible that two exact iptables configurations have different behavior? I guess I am missing some piece to understand the specifics of the Docket port forwarding functionality.







      linux networking firewall iptables docker






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 12 at 18:03









      Flux

      1145




      1145










      asked Nov 21 '17 at 13:03









      Dave TeezoDave Teezo

      104




      104






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Have a look at the nat table with iptables -L -t nat.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1270459%2fiptables-how-does-the-docker-port-ab-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Have a look at the nat table with iptables -L -t nat.






            share|improve this answer




























              0














              Have a look at the nat table with iptables -L -t nat.






              share|improve this answer


























                0












                0








                0







                Have a look at the nat table with iptables -L -t nat.






                share|improve this answer













                Have a look at the nat table with iptables -L -t nat.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '17 at 13:14









                towotowo

                50938




                50938






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1270459%2fiptables-how-does-the-docker-port-ab-work%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Probability when a professor distributes a quiz and homework assignment to a class of n students.

                    Aardman Animations

                    Are they similar matrix