Application website is not opening when container created with host network

I have created container called web03 with host network. so when I browse that application with host ip , it supposted to browse the site but it is not happening. Please suggest

shyam@DESKTOP-OO7EVK2:~$ docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS                          PORTS                  NAMES
8b62bb7a2801   nginx                       "/docker-entrypoint.…"   6 minutes ago    Up 6 minutes                                           web03

You can see below host cant be reached with host ip:

shyam@DESKTOP-OO7EVK2:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.19.118.177  netmask 255.255.240.0  broadcast 172.19.127.255
        inet6 fe80::215:5dff:fe6f:bdee  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:6f:bd:ee  txqueuelen 1000  (Ethernet)
        RX packets 4893  bytes 1559829 (1.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 694  bytes 90231 (90.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 58  bytes 6137 (6.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 6137 (6.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Thank you.

Did you map the relevant ports?
Would you show us the compose file / command you ran to start the container?

This is the command i have used:
shyam@DESKTOP-OO7EVK2:~$ docker run -d --name web03 --network host nginx

Two reasons come to mind, why this might not work:

  • Docker Desktop is used
  • The listen directive is not configured to bind port 80

Please share the output of docker info (formatted in a code block), and the content of the relevant nginx configuration that shows the listen directive.

Yes, I am using Docker Desktop

Docker info:

shyam@DESKTOP-OO7EVK2:~$ docker info
Client:
 Version:    27.1.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.16.1-desktop.1
    Path:     /usr/local/lib/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.29.1-desktop.1
    Path:     /usr/local/lib/docker/cli-plugins/docker-compose
  debug: Get a shell into any image or container (Docker Inc.)
    Version:  0.0.34
    Path:     /usr/local/lib/docker/cli-plugins/docker-debug
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.2
    Path:     /usr/local/lib/docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.25
    Path:     /usr/local/lib/docker/cli-plugins/docker-extension
  feedback: Provide feedback, right in your terminal! (Docker Inc.)
    Version:  v1.0.5
    Path:     /usr/local/lib/docker/cli-plugins/docker-feedback
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v1.3.0
    Path:     /usr/local/lib/docker/cli-plugins/docker-init
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     /usr/local/lib/docker/cli-plugins/docker-sbom
  scout: Docker Scout (Docker Inc.)
    Version:  v1.11.0
    Path:     /usr/local/lib/docker/cli-plugins/docker-scout

Server:
 Containers: 54
  Running: 22
  Paused: 0
  Stopped: 32
 Images: 22
 Server Version: 27.1.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 2bf793ef6dc9a18e00cb12efb64355c2c9d5eb41
 runc version: v1.1.13-0-g58aa920
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
 Kernel Version: 5.15.153.1-microsoft-standard-WSL2
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.72GiB
 Name: docker-desktop
 ID: b587712e-7ae6-4ddf-a393-73384c4329a1
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Labels:
  com.docker.desktop.address=unix:///var/run/docker-cli.sock
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No blkio throttle.read_bps_device support
WARNING: No blkio throttle.write_bps_device support
WARNING: No blkio throttle.read_iops_device support

nginx configuration:

shyam@DESKTOP-OO7EVK2:~$ docker exec -it web03 /bin/bash
root@docker-desktop:/# cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
root@docker-desktop:/#

Docker Desktop for Windows allows enabling Host-Networking.

You can find the setting in version v4.34.0 here:

The nginx configuration you shared has everything, but not the listen directive I specifically asked for. So basically we (still) have no idea how the nginx is configured exactly…

This is what I see from network section in my docker-desktop

Please update to version 4.34.0. There is a reason why I specifically wrote it above the screenshot :wink:

But how to update?

I see it is already up to date message

You can download it from here: https://docs.docker.com/desktop/release-notes/

Interesting that Docker Desktop is not aware of the update.

1 Like

After updating to latest version and enabling to host network, it is working
Thank you :slight_smile: