I’ve recently learned that UFW firewall rules do not affect Docker containers. I am looking into learning firewall rules in depth but in the meantime I want make sure I don’t fuck something up, so here are a few questions:

1- On a host that drops all incoming connections (configured through UFW), if I have a container with only a single port mapping 127.0.0.1:8080:80 is there any way to access this container through the public internet, what about 8080:80 or no port mapping at all?

2- How do I drop all incoming connections to all Docker containers and do I need to do that? Similar to ufw default deny incoming?

3- Is there a way to see all incoming/outgoing connections of all containers?

Thanks in advance and any resource advice for securing docker for dummies is appreciated.

  • tux7350@lemmy.world
    link
    fedilink
    English
    arrow-up
    4
    ·
    3 hours ago

    Oh boy I went down this same rabbit hole awhile ago. Here is a git repository that will explain why this happens and also offers a fix on how to modify your IP tables to ensure that docker respects the UFW.

    Docker and UFW

  • Max-P@lemmy.max-p.me
    link
    fedilink
    English
    arrow-up
    7
    ·
    4 hours ago

    With Docker, the internal network is just a bridge interface. The reason most firewall rules don’t apply is a combination of:

    • Containers have their own namespace including network namespace, so each container have a blank iptables just for them.
    • For container communication, that goes through the FORWARD table, not the INPUT/OUTPUT ones.
    • Docker adds its own rules to ensure that this works as expected.

    The only thing that should be affected by the host firewall is the proxy service Docker uses to listen on a port on the host and send it to the container.

    When using Docker, each container acts like an independent machine, and your host gets configured to act as a router. You can firewall Docker containers, the rules just need to be in the right place to work.

    • Quail4789OP
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      40 minutes ago

      Thanks, just to clarify, even if I deny all forwards on the host using UFW, that still won’t have an affect on Docker because Docker inserts its rules above UFW rules. Correct?

  • BK85@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    4 hours ago

    Try tailscale for remote acess run it as a docker container. I csnt help you with the other questions.

  • Dangerhart@lemm.ee
    link
    fedilink
    English
    arrow-up
    1
    ·
    4 hours ago

    I too read that it didn’t work with docker but that was not my experience on Ubuntu 24.04. Maybe it’s just docker desktop but I had all sorts of other issues with docker desktop and ditched it for plain docker, using lazydocker for an interface. I think one of the issues for outgoing connections at least is that IPs for the containers can change. I don’t remember exactly what my setup is currently but you shouldn’t have an issue opening up just specific ports mapped to a container while having default deny incoming. Not specific to containers but there are a few different Linux commands to get all ports and the processes listening on them, I used them extensively to debug my firewall setup. I can’t remember them either off the top of my head but you should be able to google for your distro.

    • Dangerhart@lemm.ee
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      4 hours ago

      To add, you may not need to worry too much about setting up a firewall if your machine is behind a router/gateway that also has a firewall. There are arguments both ways, I would suggest researching that some. I did it because I could so why not, but I also only have wireguard exposed publicly so it may be over kill

    • Quail4789OP
      link
      fedilink
      English
      arrow-up
      3
      arrow-down
      2
      ·
      3 hours ago

      telling someone asking these questions to read up on networking is the same as telling someone who’s asking why they can’t fly in the air to “just” read up on physics

      • just_another_person@lemmy.world
        link
        fedilink
        English
        arrow-up
        2
        arrow-down
        8
        ·
        3 hours ago

        Then maybe you’re not equipped to be attempting this. It’s basic networking.

        If I ask and invite an electrician out to my house and proceed to tell him how I can just do it all myself, I deserve the horrible outcome.

        DF.

        • monkeyman512@lemmy.world
          link
          fedilink
          English
          arrow-up
          3
          arrow-down
          1
          ·
          3 hours ago

          I think the important factor is that you have contributed nothing of value to help this person learn. You could have linked to a useful resources, you have suggested Google searches to point in the correct direction. But you basically said “This is easy, RTFM”. Next time at least send them the manual they should read.