1

My goal is to use an nginx container to reverse-proxy specific paths to my application(s) running in my vps. I've successfully done it by setting up a nginx container manually, but not when using docker-compose.

With nginx container run manually (successfull) :

sudo docker run -it -d=true --restart=unless-stopped --name=nginx -p=80:80 nginx

then updating the : nginx:/etc/nginx/conf.d/default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # custom
    location /ping {
        proxy_pass http://my-vps-bla-bla.com:13671/ping;
    }
}

With this configuration I can successfully call :

  • curl --location 'http://my-vps-bla-bla.com' ✅ (nginx default welcome response)
  • curl --location 'http://my-vps-bla-bla.com/ping' ✅ (my service response)

With nginx from docker-compose (unsuccessful) :

sudo docker-compose up -d using the following docker-compose.yaml :

version: "3"
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./data/nginx/conf.d/:/etc/nginx/conf.d/

(the mounted nginx:/etc/nginx/conf.d/default.conf is the same as above)

With this configuration, calling my vps on /ping doesn't work

  • curl --location 'http://my-vps-bla-bla.com'
  • curl --location 'http://my-vps-bla-bla.com/ping'
<body>
    <h1>An error occurred.</h1>
    <p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>

I've tried to compare both outputs from sudo docker inspect nginx but I am clueless where I have to look / fix. Here are the two outputs :

sudo docker inspect nginx from the 'manual' nginx container setup

[
    {
        "Id": "b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39",
        "Created": "2023-11-30T17:13:10.075669841Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1928612,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-11-30T17:13:10.394942106Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:a6bd71f48f6839d9faae1f29d3babef831e76bc213107682c5cc80f0cbb30866",
        "ResolvConfPath": "/var/lib/docker/containers/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39/hostname",
        "HostsPath": "/var/lib/docker/containers/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39/hosts",
        "LogPath": "/var/lib/docker/containers/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39-json.log",
        "Name": "/nginx",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "unless-stopped",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                24,
                176
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/fbf2e1d00d7fcad7ebb6a270d58090e19c99a07d9957e6c82e537818067043c9-init/diff:/var/lib/docker/overlay2/ccf450a15bc9b7180010e6d2e409a3c9df4979248c75426c60f1e9d161f868d5/diff:/var/lib/docker/overlay2/63815499c80141a4d1af5a8e2c142b4087e32f8f86455372dda5825470a8980a/diff:/var/lib/docker/overlay2/43497557d17d33626e4dedaef8ba71c1e379a4fb134ea97e1c706ed08a0656de/diff:/var/lib/docker/overlay2/6f03359d7e49862599ec61b7f085af5ddc2edb3a30dec2a9bc8812853ef955c7/diff:/var/lib/docker/overlay2/df62ce4c1da140df692bffc5dc5ba2db38b14497b0c8f6a586aa33d685923c7e/diff:/var/lib/docker/overlay2/b53502919ddaf322d2433b5c3194c0d04ab45d838f64fb1e8ca9ab363a77082c/diff:/var/lib/docker/overlay2/d9166dd4fa2cef1a1bed1401e4e8ea1745f1a8a4d50f5d2924d8c54bed019c4a/diff",
                "MergedDir": "/var/lib/docker/overlay2/fbf2e1d00d7fcad7ebb6a270d58090e19c99a07d9957e6c82e537818067043c9/merged",
                "UpperDir": "/var/lib/docker/overlay2/fbf2e1d00d7fcad7ebb6a270d58090e19c99a07d9957e6c82e537818067043c9/diff",
                "WorkDir": "/var/lib/docker/overlay2/fbf2e1d00d7fcad7ebb6a270d58090e19c99a07d9957e6c82e537818067043c9/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "b9e77c868685",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.25.3",
                "NJS_VERSION=0.8.2",
                "PKG_RELEASE=1~bookworm"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGQUIT"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "0c9b693da3685ad37e758da8e67d5d537e744c77bba0c13b2b2fa95c37eeede8",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/0c9b693da368",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "7beba34d7a1c3739dadf6b14856295e1849007e86792c70e2d76483d94a00b4e",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "aa:bb:cc:dd:ee:ff,
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "751980ff5b2110b5c0072ddd62e953a5e6575819e3a25319868474ee4b530e65",
                    "EndpointID": "7beba34d7a1c3739dadf6b14856295e1849007e86792c70e2d76483d94a00b4e",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "aa:bb:cc:dd:ee:ff",
                    "DriverOpts": null
                }
            }
        }
    }
]

sudo docker inspect docker-stuff_default from the docker-compose nginx container setup

[
    {
        "Id": "2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f",
        "Created": "2023-11-30T17:09:18.381573448Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1928407,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-11-30T17:09:18.752743033Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:a6bd71f48f6839d9faae1f29d3babef831e76bc213107682c5cc80f0cbb30866",
        "ResolvConfPath": "/var/lib/docker/containers/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f/hostname",
        "HostsPath": "/var/lib/docker/containers/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f/hosts",
        "LogPath": "/var/lib/docker/containers/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f-json.log",
        "Name": "/docker-stuff_nginx_1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home/ubuntu/projects/docker-stuff/data/nginx/conf.d:/etc/nginx/conf.d:rw"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "docker-stuff_default",
            "PortBindings": {
                "443/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "443"
                    }
                ],
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "ConsoleSize": [
                0,
                0
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/38441d680a06140f681240786942a4eee6fbd37eb2697db64be1647427bca63b-init/diff:/var/lib/docker/overlay2/ccf450a15bc9b7180010e6d2e409a3c9df4979248c75426c60f1e9d161f868d5/diff:/var/lib/docker/overlay2/63815499c80141a4d1af5a8e2c142b4087e32f8f86455372dda5825470a8980a/diff:/var/lib/docker/overlay2/43497557d17d33626e4dedaef8ba71c1e379a4fb134ea97e1c706ed08a0656de/diff:/var/lib/docker/overlay2/6f03359d7e49862599ec61b7f085af5ddc2edb3a30dec2a9bc8812853ef955c7/diff:/var/lib/docker/overlay2/df62ce4c1da140df692bffc5dc5ba2db38b14497b0c8f6a586aa33d685923c7e/diff:/var/lib/docker/overlay2/b53502919ddaf322d2433b5c3194c0d04ab45d838f64fb1e8ca9ab363a77082c/diff:/var/lib/docker/overlay2/d9166dd4fa2cef1a1bed1401e4e8ea1745f1a8a4d50f5d2924d8c54bed019c4a/diff",
                "MergedDir": "/var/lib/docker/overlay2/38441d680a06140f681240786942a4eee6fbd37eb2697db64be1647427bca63b/merged",
                "UpperDir": "/var/lib/docker/overlay2/38441d680a06140f681240786942a4eee6fbd37eb2697db64be1647427bca63b/diff",
                "WorkDir": "/var/lib/docker/overlay2/38441d680a06140f681240786942a4eee6fbd37eb2697db64be1647427bca63b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/ubuntu/projects/docker-stuff/data/nginx/conf.d",
                "Destination": "/etc/nginx/conf.d",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "2129e96a18fc",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "443/tcp": {},
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.25.3",
                "NJS_VERSION=0.8.2",
                "PKG_RELEASE=1~bookworm"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx:latest",
            "Volumes": {
                "/etc/nginx/conf.d": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "58e1e3097c631d13e6eff6c524cfc92917aeb08954585d0a455d0908f3ba728c",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "docker-stuff",
                "com.docker.compose.project.config_files": "docker-compose.yaml",
                "com.docker.compose.project.working_dir": "/home/ubuntu/projects/docker-stuff",
                "com.docker.compose.service": "nginx",
                "com.docker.compose.version": "1.29.2",
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGQUIT"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "8b61c1e6e2f1a1650da0ea014baaa6d174f3a6ae29e589015bd6ef6de3b0dc3c",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "443/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "443"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "443"
                    }
                ],
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/8b61c1e6e2f1",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "docker-stuff_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "nginx",
                        "2129e96a18fc"
                    ],
                    "NetworkID": "6aa6d52fb8c8945e6a36f5229892d14cb8e39e1dbf6fcf8c351464fd59ba83a3",
                    "EndpointID": "5f3010c7f1c9119c50cdff6f924470f7ceab64bc66b8d09f12d524ffa8b9b1ff",
                    "Gateway": "172.24.0.1",
                    "IPAddress": "172.24.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "aa:bb:cc:dd:ee:ff",
                    "DriverOpts": null
                }
            }
        }
    }
]
New contributor
pogorouge1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

0

The difference is in the network. From the command line, the container, as stated in the docker domentation

By default, containers inherit the DNS settings of the host, as defined in the /etc/resolv.conf configuration file. Containers that attach to the default bridge network receive a copy of this file. Containers that attach to a custom network use Docker's embedded DNS server. The embedded DNS server forwards external DNS lookups to the DNS servers configured on the host.

As you can see, the container produced by the docker-compose.yml file, uses a custom network, so you should configure (in the same YAML file, under dns:), the DNS configuration you want or add

network-mode: bridge

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .