- 1 Post-Install Configuration must be done.
Assumptions
- Containers are reached through the NGINX configured in 3 NGINX Configuration.
- Firewalld is still off. Docker’s Firewalld integration is finished in 5 Firewalld Configuration.
- Commands are run on the server with root rights (
sudo).
Check and Install#
On RHEL-based systems, Podman, Buildah and runc may come preinstalled. Since they may clash with Docker’s own packages, let’s remove them and install Docker from its own repository:
sudo dnf remove podman buildah runcLet’s add the Docker repository and install it:
sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginBefore starting Docker, let’s write our configuration to /etc/docker/daemon.json. If the directory doesn’t exist yet, let’s create it first:
sudo mkdir -p /etc/docker{
"ip": "127.0.0.1",
"default-network-opts": {
"bridge": { "com.docker.network.bridge.host_binding_ipv4": "127.0.0.1" }
},
"log-driver": "journald",
"live-restore": true,
"default-address-pools": [
{ "base": "172.20.0.0/16", "size": 24 }
]
}Why each line is there:
"ip"and"default-network-opts": By default, Docker binds ports published without an IP address, like-p 8080:80, to0.0.0.0and writes its own rules for them. Traffic to these ports is passed to the container without going through Firewalld’s zone rules; even with Firewalld on, the port stays open to the internet. The services that need to be reached will be reached through NGINX, so no container needs to be open to the internet; being reachable over localhost is enough. The"ip"setting only applies to the defaultbridgenetwork (docker run). For the networks Compose creates, we set the same default withdefault-network-opts. This setting is applied when a network is created; if it is added later, it doesn’t affect the existing networks. Still, in the compose files we’ll write the ports explicitly, like127.0.0.1:8080:80."log-driver": "journald": By default, logs are written to files with thejson-filedriver. To reach and manage all logs from one place, we send them to journald. Thedocker logscommand keeps working."live-restore": true: Keeps the containers running when the Docker service is restarted or updated."default-address-pools": Sets the range the container networks get their IP addresses from.
We can start the service:
sudo systemctl enable --now dockerWe can check that Docker works with the command below:
sudo docker run --rm hello-worldThe output should have these lines:
Hello from Docker!
This message shows that your installation appears to be working correctly.Let’s check that a port published without an IP address is bound only to localhost on the kind of network Compose uses too:
sudo docker network create porttest
sudo docker network inspect porttest --format '{{json .Options}}'
sudo docker run -d --rm --name porttest --network porttest -p 18080:80 alpine sleep 60
sudo ss -tln | grep 18080
sudo docker rm -f porttest && sudo docker network rm porttestThe inspect output should show "com.docker.network.bridge.host_binding_ipv4":"127.0.0.1", and the ss output only 127.0.0.1:18080; 0.0.0.0 or [::] should not show up.
Things to Keep in Mind#
- We don’t add the normal user to the
dockergroup, because that would let the user become root without a password. - When NGINX proxies to a container, it must send the
X-Forwarded-ForandX-Real-IPheaders. Otherwise the applications see every visitor as 172.20.x.x. For this we’ll create a shared template in 4.1 A Ready Template for Docker Containers.
journalctl reads them.