Caddy on Docker swarm received HTTP 404 on port 80 or refuse to Connect if custom port

Caddy on Docker swarm received HTTP 404 on port 80 or refuse to Connect if custom port. Both the swarm and the Caddy setting is basic and simple. But on my Windows 11 just goes wrong.

The env is Docker v4.28.0 on Windows 11 and caddy:2.7.6-alpine.

  1. Below are the Caddyfile, Caddy.dockerfile and the swarm.yml

=== Caddy config file, Caddyfile ===

localhost

respond "Hello, world!"

==- Docker file, Caddy.dockerfile ===

FROM caddy:2.7.6-alpine

COPY Caddyfile /etc/caddy/Caddyfile

=== docker build and push ===

docker build -f caddy.dockerfile -t andrewwang1tw/micro-caddy:1.0.2 .  

docker push andrewwang1tw/micro-caddy:1.0.2

=== Docker swarm file, swarm.yml ====

version: '3'

services:

  caddy:
    image: andrewwang1tw/micro-caddy:1.0.2
    deploy:
      mode: replicated
      replicas: 1
    ports:  
      - "80:80"
      - "443:443"
    volumes:  
      - caddy_data:/data
      - caddy_config:/config  

volumes: 
  caddy_data:
    external: true
  caddy_config:
  1. The result, is received HTTP 404 on port 80, it seems hot to the “Server: Microsoft-HTTPAPI/2.0”
PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin> ./curl http://localhost -v
     
* Host localhost:80 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:80...
* Connected to localhost (::1) port 80
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/8.6.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/html; charset=us-ascii
< Server: Microsoft-HTTPAPI/2.0
< Date: Wed, 13 Mar 2024 02:11:41 GMT
< Connection: close
< Content-Length: 315
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Not Found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Not Found</h2>
<hr><p>HTTP Error 404. The requested resource is not found.</p>
</BODY></HTML>
* Closing connection
  1. The result is refused to Connect if use custom port 7878
PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin> ./curl http://localhost:7878 -v

* Host localhost:7878 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:7878...
*   Trying 127.0.0.1:7878...
* connect to ::1 port 7878 from :: port 4056 failed: Connection refused
* connect to 127.0.0.1 port 7878 from 0.0.0.0 port 4057 failed: Connection refused
* Failed to connect to localhost port 7878 after 2244 ms: Couldn't connect to server
* Closing connection
curl: (7) Failed to connect to localhost port 7878 after 2244 ms: Couldn't connect to server

PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin>

I don’t think it’s correct to use localhost, as that will not be exposed using ports, you probably need to listen to 0.0.0.0, meaning all available IPs inside the container.

Thanks for replying

The localhost in the Caddyfile is comes from Caddy quick-tutorial. But I still give it a try. Here is the result.

Caddyfile quick tutorial

PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin> ./curl http://localhost:7788 -v 

* Host localhost:7788 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:7788...
*   Trying 127.0.0.1:7788...
* connect to ::1 port 7788 from :: port 8059 failed: Connection refused
* connect to 127.0.0.1 port 7788 from 0.0.0.0 port 8060 failed: Connection refused
* Failed to connect to localhost port 7788 after 2244 ms: Couldn't connect to server
* Closing connection
curl: (7) Failed to connect to localhost port 7788 after 2244 ms: Couldn't connect to server

PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin>

The header ‘Server: Microsoft-HTTPAPI/2.0’ is typically outputted by Microsoft Internet Information Services (IIS). This indicates that the response is from a server that uses Microsoft’s HTTP API or from other Microsoft products or applications.

Normally, Caddy would respond with a header that specifies the server type and version of Caddy. The default header would be something like:

Server: Caddy

Hi, thanks for replying.

Caddy forum

Pic from my previous Caddy forum post

The link above is what I posted on Caddy Forum.

The strange thing is if I use port 80, then “Server: Microsoft-HTTPAPI/2.0”, does not hit the Caddy. However, I did not have IIS installed. That’s the reason why I tested on port 7878 later.

Also, if I try the Golang code below, it shows the access permissions issue. I guess the “Server: Microsoft-HTTPAPI/2.0” problem is due to the permission of port 80.

err := http.ListenAndServe(":80", nil)

listen tcp :80: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

Is IIS Express installed?
IIS Express is derived from IIS 7 and above and supports the core features of IIS.
Common IIS Express management tasks are also built into Visual Studio 2010 SP1 and later.

Can you provide us the changed configuration files and the output of

docker node ps

I do not get it about the “changed configuration files” ? Here is the docker node ps

PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end> docker node ps $(docker node ls -q)

ID             NAME            IMAGE                             NODE             DESIRED STATE   CURRENT STATE           ERROR     PORTS
is9iz7ounxd4   myapp_caddy.1   andrewwang1tw/micro-caddy:1.0.0   docker-desktop   Running         Running 3 minutes ago

PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end> docker node ps     
                
ID             NAME            IMAGE                             NODE             DESIRED STATE   CURRENT STATE            ERROR     PORTS
is9iz7ounxd4   myapp_caddy.1   andrewwang1tw/micro-caddy:1.0.0   docker-desktop   Running         Running 13 minutes ago

PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end> 

No IIS installed, but have the folder “C:\Program Files\IIS Express”. However, from the task manager, the IISExpress.exe did not run.

Thanks. By changed configuration files I mean like:

  • Caddyfile
  • Dockerfile
  • Docker swarm file

Can you also provide us the output of

docker container ls

and maybe also the output of

docker container inspect [id of container]

Edit: @andrewwang1tw Please do not forget to format the output here in the forum

From

Run the command:

netsh http show servicestate view=requestq

This will give snapshot of all the HTTP listeners.

Here are the “docker container ls” and docker container inspect [id of container]

The forum does not let me upload the changed configuration files; they are pretty simple. I have posted all the changed configuration file content on the post (the top)

docker container ls

CONTAINER ID   IMAGE                             COMMAND                   CREATED          STATUS          PORTS                                NAMES
745fa3e5bdc2   andrewwang1tw/micro-caddy:1.0.0   "caddy run --config …"   33 minutes ago   Up 33 minutes   80/tcp, 443/tcp, 2019/tcp, 443/udp   myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2

docker container inspect [id of container]

PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end> docker container inspect 745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd
[
    {
        "Id": "745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd",
        "Created": "2024-03-14T06:47:51.95412209Z",
        "Path": "caddy",
        "Args": [
            "run",
            "--config",
            "/etc/caddy/Caddyfile",
            "--adapter",
            "caddyfile"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 30423,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2024-03-14T06:47:52.913473129Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:48d068c9d2dd2da10f4d26c5ff5a62bf20f3e73f85db331319b3af787377ba6e",
        "ResolvConfPath": "/var/lib/docker/containers/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd/hostname",
        "HostsPath": "/var/lib/docker/containers/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd/hosts",
        "LogPath": "/var/lib/docker/containers/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd-json.log",
        "Name": "/myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                0,
                0
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "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": "default",
            "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": false,
            "PidsLimit": null,
            "Ulimits": [],
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "Mounts": [
                {
                    "Type": "volume",
                    "Source": "caddy_data",
                    "Target": "/data",
                    "VolumeOptions": {}
                },
                {
                    "Type": "volume",
                    "Source": "myapp_caddy_config",
                    "Target": "/config",
                    "VolumeOptions": {
                        "Labels": {
                            "com.docker.stack.namespace": "myapp"
                        }
                    }
                }
            ],
            "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",
                "/sys/devices/virtual/powercap"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/f9529ffd6a05ee39d8c3386aec8cbf4c3eb8da0da3660f7d82a6a40a6f1a16d7-init/diff:/var/lib/docker/overlay2/rtmnbp6fh7ceb1n8v84khid7u/diff:/var/lib/docker/overlay2/5eb75a4c14e8faae24427a3230e69336fc39187ec7d3afe36f836cd5e4a4b06d/diff:/var/lib/docker/overlay2/0f7dd5a42fe66eb24a48b0b1b356d7355785b45056b31873b64693915c1d7431/diff:/var/lib/docker/overlay2/b4b534fe46378113da0e1aa0b3ca73f02464e29560a5455fa381cd77ad434a06/diff:/var/lib/docker/overlay2/b499d33e3f810b6e035cae3e95a433736e974541cea28b7b942bbcf529645d4e/diff",
                "MergedDir": "/var/lib/docker/overlay2/f9529ffd6a05ee39d8c3386aec8cbf4c3eb8da0da3660f7d82a6a40a6f1a16d7/merged",
                "UpperDir": "/var/lib/docker/overlay2/f9529ffd6a05ee39d8c3386aec8cbf4c3eb8da0da3660f7d82a6a40a6f1a16d7/diff",
                "WorkDir": "/var/lib/docker/overlay2/f9529ffd6a05ee39d8c3386aec8cbf4c3eb8da0da3660f7d82a6a40a6f1a16d7/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "myapp_caddy_config",
                "Source": "/var/lib/docker/volumes/myapp_caddy_config/_data",
                "Destination": "/config",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "caddy_data",
                "Source": "/var/lib/docker/volumes/caddy_data/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "745fa3e5bdc2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "2019/tcp": {},
                "443/tcp": {},
                "443/udp": {},
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "CADDY_VERSION=v2.7.6",
                "XDG_CONFIG_HOME=/config",
                "XDG_DATA_HOME=/data"
            ],
            "Cmd": [
                "caddy",
                "run",
                "--config",
                "/etc/caddy/Caddyfile",
                "--adapter",
                "caddyfile"
            ],
            "Image": "andrewwang1tw/micro-caddy:1.0.0@sha256:e90f08d0a3388e7121114ba450f66026ea73c04508d54dd5141e8a3922bc487b",
            "Volumes": null,
            "WorkingDir": "/srv",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "com.docker.stack.namespace": "myapp",
                "com.docker.swarm.node.id": "j3pvfqu59ma8oosqc5dm5bd26",
                "com.docker.swarm.service.id": "y8lu7jpr3ehs4ig2vhgibn0a8",
                "com.docker.swarm.service.name": "myapp_caddy",
                "com.docker.swarm.task": "",
                "com.docker.swarm.task.id": "is9iz7ounxd40ya08c9i5y3a2",
                "com.docker.swarm.task.name": "myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2",
                "org.opencontainers.image.description": "a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go",
                "org.opencontainers.image.documentation": "https://caddyserver.com/docs",
                "org.opencontainers.image.licenses": "Apache-2.0",
                "org.opencontainers.image.source": "https://github.com/caddyserver/caddy-docker",
                "org.opencontainers.image.title": "Caddy",
                "org.opencontainers.image.url": "https://caddyserver.com",
                "org.opencontainers.image.vendor": "Light Code Labs",
                "org.opencontainers.image.version": "v2.7.6"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "914f33f145e73a4444f2d0f94ef6fa61625b916651cb301ae6851747bc9c0b0b",
            "SandboxKey": "/var/run/docker/netns/914f33f145e7",
            "Ports": {
                "2019/tcp": null,
                "443/tcp": null,
                "443/udp": null,
                "80/tcp": null
            },
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "ingress": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.0.4"
                    },
                    "Links": null,
                    "Aliases": [
                        "745fa3e5bdc2"
                    ],
                    "MacAddress": "02:42:0a:00:00:04",
                    "NetworkID": "yws7fiexazk7is2xulfd9chsp",
                    "EndpointID": "3890f39ec50ab1ef81e13ada324378d0366ed306c2c6c9dc05c853f02f8df18a",
                    "Gateway": "",
                    "IPAddress": "10.0.0.4",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": [
                        "myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2",
                        "745fa3e5bdc2"
                    ]
                },
                "myapp_default": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.1.3"
                    },
                    "Links": null,
                    "Aliases": [
                        "745fa3e5bdc2"
                    ],
                    "MacAddress": "02:42:0a:00:01:03",
                    "NetworkID": "1e6bw6oqdj0ljr4lpli4iea5l",
                    "EndpointID": "3f4ae84be8cd9e160e6be69f1dfb91c9ecce636061b8ec39dacbf46e407646fb",
                    "Gateway": "",
                    "IPAddress": "10.0.1.3",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": [
                        "myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2",
                        "745fa3e5bdc2"
                    ]
                }
            }
        }
    }
]
PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end>
````Preformatted text`

docker container ls

PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end> docker container ls
CONTAINER ID   IMAGE                             COMMAND                   CREATED          STATUS          PORTS                                NAMES
745fa3e5bdc2   andrewwang1tw/micro-caddy:1.0.0   "caddy run --config …"   45 minutes ago   Up 45 minutes   80/tcp, 443/tcp, 2019/tcp, 443/udp   myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2
PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end>

docker container inspect [id of container]

PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end> docker container inspect 745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd
[
    {
        "Id": "745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd",
        "Created": "2024-03-14T06:47:51.95412209Z",
        "Path": "caddy",
        "Args": [
            "run",
            "--config",
            "/etc/caddy/Caddyfile",
            "--adapter",
            "caddyfile"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 30423,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2024-03-14T06:47:52.913473129Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:48d068c9d2dd2da10f4d26c5ff5a62bf20f3e73f85db331319b3af787377ba6e",
        "ResolvConfPath": "/var/lib/docker/containers/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd/hostname",
        "HostsPath": "/var/lib/docker/containers/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd/hosts",
        "LogPath": "/var/lib/docker/containers/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd/745fa3e5bdc25ef40bb4c289170849debfa27194298524d6b994b7bfde3c13dd-json.log",
        "Name": "/myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                0,
                0
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "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": "default",
            "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": false,
            "PidsLimit": null,
            "Ulimits": [],
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "Mounts": [
                {
                    "Type": "volume",
                    "Source": "caddy_data",
                    "Target": "/data",
                    "VolumeOptions": {}
                },
                {
                    "Type": "volume",
                    "Source": "myapp_caddy_config",
                    "Target": "/config",
                    "VolumeOptions": {
                        "Labels": {
                            "com.docker.stack.namespace": "myapp"
                        }
                    }
                }
            ],
            "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",
                "/sys/devices/virtual/powercap"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/f9529ffd6a05ee39d8c3386aec8cbf4c3eb8da0da3660f7d82a6a40a6f1a16d7-init/diff:/var/lib/docker/overlay2/rtmnbp6fh7ceb1n8v84khid7u/diff:/var/lib/docker/overlay2/5eb75a4c14e8faae24427a3230e69336fc39187ec7d3afe36f836cd5e4a4b06d/diff:/var/lib/docker/overlay2/0f7dd5a42fe66eb24a48b0b1b356d7355785b45056b31873b64693915c1d7431/diff:/var/lib/docker/overlay2/b4b534fe46378113da0e1aa0b3ca73f02464e29560a5455fa381cd77ad434a06/diff:/var/lib/docker/overlay2/b499d33e3f810b6e035cae3e95a433736e974541cea28b7b942bbcf529645d4e/diff",
                "MergedDir": "/var/lib/docker/overlay2/f9529ffd6a05ee39d8c3386aec8cbf4c3eb8da0da3660f7d82a6a40a6f1a16d7/merged",
                "UpperDir": "/var/lib/docker/overlay2/f9529ffd6a05ee39d8c3386aec8cbf4c3eb8da0da3660f7d82a6a40a6f1a16d7/diff",
                "WorkDir": "/var/lib/docker/overlay2/f9529ffd6a05ee39d8c3386aec8cbf4c3eb8da0da3660f7d82a6a40a6f1a16d7/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "myapp_caddy_config",
                "Source": "/var/lib/docker/volumes/myapp_caddy_config/_data",
                "Destination": "/config",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "caddy_data",
                "Source": "/var/lib/docker/volumes/caddy_data/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "745fa3e5bdc2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "2019/tcp": {},
                "443/tcp": {},
                "443/udp": {},
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "CADDY_VERSION=v2.7.6",
                "XDG_CONFIG_HOME=/config",
                "XDG_DATA_HOME=/data"
            ],
            "Cmd": [
                "caddy",
                "run",
                "--config",
                "/etc/caddy/Caddyfile",
                "--adapter",
                "caddyfile"
            ],
            "Image": "andrewwang1tw/micro-caddy:1.0.0@sha256:e90f08d0a3388e7121114ba450f66026ea73c04508d54dd5141e8a3922bc487b",
            "Volumes": null,
            "WorkingDir": "/srv",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "com.docker.stack.namespace": "myapp",
                "com.docker.swarm.node.id": "j3pvfqu59ma8oosqc5dm5bd26",
                "com.docker.swarm.service.id": "y8lu7jpr3ehs4ig2vhgibn0a8",
                "com.docker.swarm.service.name": "myapp_caddy",
                "com.docker.swarm.task": "",
                "com.docker.swarm.task.id": "is9iz7ounxd40ya08c9i5y3a2",
                "com.docker.swarm.task.name": "myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2",
                "org.opencontainers.image.description": "a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go",
                "org.opencontainers.image.documentation": "https://caddyserver.com/docs",
                "org.opencontainers.image.licenses": "Apache-2.0",
                "org.opencontainers.image.source": "https://github.com/caddyserver/caddy-docker",
                "org.opencontainers.image.title": "Caddy",
                "org.opencontainers.image.url": "https://caddyserver.com",
                "org.opencontainers.image.vendor": "Light Code Labs",
                "org.opencontainers.image.version": "v2.7.6"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "914f33f145e73a4444f2d0f94ef6fa61625b916651cb301ae6851747bc9c0b0b",
            "SandboxKey": "/var/run/docker/netns/914f33f145e7",
            "Ports": {
                "2019/tcp": null,
                "443/tcp": null,
                "443/udp": null,
                "80/tcp": null
            },
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "ingress": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.0.4"
                    },
                    "Links": null,
                    "Aliases": [
                        "745fa3e5bdc2"
                    ],
                    "MacAddress": "02:42:0a:00:00:04",
                    "NetworkID": "yws7fiexazk7is2xulfd9chsp",
                    "EndpointID": "3890f39ec50ab1ef81e13ada324378d0366ed306c2c6c9dc05c853f02f8df18a",
                    "Gateway": "",
                    "IPAddress": "10.0.0.4",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": [
                        "myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2",
                        "745fa3e5bdc2"
                    ]
                },
                "myapp_default": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.1.3"
                    },
                    "Links": null,
                    "Aliases": [
                        "745fa3e5bdc2"
                    ],
                    "MacAddress": "02:42:0a:00:01:03",
                    "NetworkID": "1e6bw6oqdj0ljr4lpli4iea5l",
                    "EndpointID": "3f4ae84be8cd9e160e6be69f1dfb91c9ecce636061b8ec39dacbf46e407646fb",
                    "Gateway": "",
                    "IPAddress": "10.0.1.3",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": [
                        "myapp_caddy.1.is9iz7ounxd40ya08c9i5y3a2",
                        "745fa3e5bdc2"
                    ]
                }
            }
        }
    }
]
PS C:\Users\andrewwang1\source\repos\udemy-microservice-golang\front-end>

Here is the changed configuration file (Because I cannot upload the files via the forum)

== caddy.dockerfile ===

FROM caddy:2.7.6-alpine

COPY Caddyfile /etc/caddy/Caddyfile

=== swarm.yml ===

version: '3'

services:

  caddy:
    image: andrewwang1tw/micro-caddy:1.0.0
    deploy:
      mode: replicated
      replicas: 1
    ports:  
      - "7788:7788"
      - "443:443"
    volumes:  
      - caddy_data:/data
      - caddy_config:/config  

volumes: 
  caddy_data:
    external: true
  caddy_config:

=== Caddyfile ===

localhost:7788

respond "Hello, world!"

netsh http show servicestate view=request

this helps, I found 80 and 443 already registered by “MS Report Server”

   HTTPS://LOCALHOST:443/REPORTSERVER/
   HTTP://+:80/REPORTSERVER/

After uninstalling the report server, port 80 is free to bind (below golang code)

err := http.ListenAndServe(":80", nil)

As @bluepuma77 already said, I also think that localhost is not enough.

With other webservers you usually use 0.0.0.0 or a wildcard like *

With Caddy it is apparently just nothing
So I would try the following:

:7788

respond "Hello, world!"

Does a

docker container ls

display the published ports?
If not you could you provide us the output of

docker container inspect [id of container]
1 Like
:7788

respond "Hello, world!"

Yes, finally saw “Hello world” from Caddy server, it works. if :7788


PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin> ./curl http://localhost:7788 -v 

* Host localhost:7788 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:7788...
* Connected to localhost (::1) port 7788
> GET / HTTP/1.1
> Host: localhost:7788
> User-Agent: curl/8.6.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< Server: Caddy
< Date: Thu, 14 Mar 2024 08:40:16 GMT
< Content-Length: 13
< 
Hello, world!* Connection #0 to host localhost left intact

PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin>

But if :80, still not work

:80

respond "Hello, world!"
PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin> ./curl http://localhost -v   

* Host localhost:80 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:80...
*   Trying 127.0.0.1:80...
* connect to ::1 port 80 from :: port 5655 failed: Connection refused
* connect to 127.0.0.1 port 80 from 0.0.0.0 port 5656 failed: Connection refused
* Failed to connect to localhost port 80 after 2247 ms: Couldn't connect to server
* Closing connection
curl: (7) Failed to connect to localhost port 80 after 2247 ms: Couldn't connect to server

PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin>

and I think port 80 is free now, can not find port 80 from below command

netsh http show servicestate view=request

Here is the “docker container inspect [id of container]” for “:7788”

PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin> docker container inspect aa6b4201e4f2ddd18bd79efa6aa7d34d93c61ab4941aca915d4a2232fe0fe48b
[
    {
        "Id": "aa6b4201e4f2ddd18bd79efa6aa7d34d93c61ab4941aca915d4a2232fe0fe48b",
        "Created": "2024-03-14T09:04:14.128347951Z",
        "Path": "caddy",
        "Args": [
            "run",
            "--config",
            "/etc/caddy/Caddyfile",
            "--adapter",
            "caddyfile"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 42745,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2024-03-14T09:04:14.879760109Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:572904a7f82f89d27e4f64f4a7d9807b77cfa233825107d13171014254a06f4e",
        "ResolvConfPath": "/var/lib/docker/containers/aa6b4201e4f2ddd18bd79efa6aa7d34d93c61ab4941aca915d4a2232fe0fe48b/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/aa6b4201e4f2ddd18bd79efa6aa7d34d93c61ab4941aca915d4a2232fe0fe48b/hostname",
        "HostsPath": "/var/lib/docker/containers/aa6b4201e4f2ddd18bd79efa6aa7d34d93c61ab4941aca915d4a2232fe0fe48b/hosts",
        "LogPath": "/var/lib/docker/containers/aa6b4201e4f2ddd18bd79efa6aa7d34d93c61ab4941aca915d4a2232fe0fe48b/aa6b4201e4f2ddd18bd79efa6aa7d34d93c61ab4941aca915d4a2232fe0fe48b-json.log",
        "Name": "/myapp_caddy.1.rdbxu8iy62gv8erzmov5eif6s",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                0,
                0
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "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": "default",
            "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": false,
            "PidsLimit": null,
            "Ulimits": [],
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "Mounts": [
                {
                    "Type": "volume",
                    "Source": "caddy_data",
                    "Target": "/data",
                    "VolumeOptions": {}
                },
                {
                    "Type": "volume",
                    "Source": "myapp_caddy_config",
                    "Target": "/config",
                    "VolumeOptions": {
                        "Labels": {
                            "com.docker.stack.namespace": "myapp"
                        }
                    }
                }
            ],
            "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",
                "/sys/devices/virtual/powercap"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/600bb3ff373442d271efbd993fc5322ce874fceba5316257134a986a7f5b4394-init/diff:/var/lib/docker/overlay2/cg51n9oz7td40nwl2qrt6jywg/diff:/var/lib/docker/overlay2/5eb75a4c14e8faae24427a3230e69336fc39187ec7d3afe36f836cd5e4a4b06d/diff:/var/lib/docker/overlay2/0f7dd5a42fe66eb24a48b0b1b356d7355785b45056b31873b64693915c1d7431/diff:/var/lib/docker/overlay2/b4b534fe46378113da0e1aa0b3ca73f02464e29560a5455fa381cd77ad434a06/diff:/var/lib/docker/overlay2/b499d33e3f810b6e035cae3e95a433736e974541cea28b7b942bbcf529645d4e/diff",
                "MergedDir": "/var/lib/docker/overlay2/600bb3ff373442d271efbd993fc5322ce874fceba5316257134a986a7f5b4394/merged",
                "UpperDir": "/var/lib/docker/overlay2/600bb3ff373442d271efbd993fc5322ce874fceba5316257134a986a7f5b4394/diff",
                "WorkDir": "/var/lib/docker/overlay2/600bb3ff373442d271efbd993fc5322ce874fceba5316257134a986a7f5b4394/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "caddy_data",
                "Source": "/var/lib/docker/volumes/caddy_data/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "myapp_caddy_config",
                "Source": "/var/lib/docker/volumes/myapp_caddy_config/_data",
                "Destination": "/config",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "aa6b4201e4f2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "2019/tcp": {},
                "443/tcp": {},
                "443/udp": {},
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "CADDY_VERSION=v2.7.6",
                "XDG_CONFIG_HOME=/config",
                "XDG_DATA_HOME=/data"
            ],
            "Cmd": [
                "caddy",
                "run",
                "--config",
                "/etc/caddy/Caddyfile",
                "--adapter",
                "caddyfile"
            ],
            "Image": "andrewwang1tw/micro-caddy:0.0.0.0.7788@sha256:eeb48ca0b73dc789253022e0177118926912ef9e8382e993120dd4ff8da3e53a",
            "Volumes": null,
            "WorkingDir": "/srv",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "com.docker.stack.namespace": "myapp",
                "com.docker.swarm.node.id": "psa9fb7qsse5swtni5yjwqli9",
                "com.docker.swarm.service.id": "k7os1ism4o3cvntqg8pq151xn",
                "com.docker.swarm.service.name": "myapp_caddy",
                "com.docker.swarm.task": "",
                "com.docker.swarm.task.id": "rdbxu8iy62gv8erzmov5eif6s",
                "com.docker.swarm.task.name": "myapp_caddy.1.rdbxu8iy62gv8erzmov5eif6s",
                "org.opencontainers.image.description": "a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go",
                "org.opencontainers.image.documentation": "https://caddyserver.com/docs",
                "org.opencontainers.image.licenses": "Apache-2.0",
                "org.opencontainers.image.source": "https://github.com/caddyserver/caddy-docker",
                "org.opencontainers.image.title": "Caddy",
                "org.opencontainers.image.url": "https://caddyserver.com",
                "org.opencontainers.image.vendor": "Light Code Labs",
                "org.opencontainers.image.version": "v2.7.6"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "85a958d4cb1567d4886e1e5b9475fd4e36d2b1d8f1dc115cac92b5726748aa6b",
            "SandboxKey": "/var/run/docker/netns/85a958d4cb15",
            "Ports": {
                "2019/tcp": null,
                "443/tcp": null,
                "443/udp": null,
                "80/tcp": null
            },
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "ingress": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.0.4"
                    },
                    "Links": null,
                    "Aliases": [
                        "aa6b4201e4f2"
                    ],
                    "MacAddress": "02:42:0a:00:00:04",
                    "NetworkID": "r2chth8ookv7rlsebmv0plfqe",
                    "EndpointID": "4f4c695fcf9517df90e68ec134f69e3f80c28afbaa89b289f3d7b870653399d0",
                    "Gateway": "",
                    "IPAddress": "10.0.0.4",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": [
                        "myapp_caddy.1.rdbxu8iy62gv8erzmov5eif6s",
                        "aa6b4201e4f2"
                    ]
                },
                "myapp_default": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.1.3"
                    },
                    "Links": null,
                    "Aliases": [
                        "aa6b4201e4f2"
                    ],
                    "MacAddress": "02:42:0a:00:01:03",
                    "NetworkID": "oshbke93xghlfn9scw1kxqzji",
                    "EndpointID": "9454702333a26352cb90f55396d0ae503adf3426acd1162827f3087965510dbe",
                    "Gateway": "",
                    "IPAddress": "10.0.1.3",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": [
                        "myapp_caddy.1.rdbxu8iy62gv8erzmov5eif6s",
                        "aa6b4201e4f2"
                    ]
                }
            }
        }
    }
]
PS C:\ProgramData\chocolatey\lib\curl\tools\curl-8.6.0_1-win64-mingw\bin>

Just received a helpful message from the Caddy forum.

And that’s why “:7788” works and “localhost:7788” and “:80” does not work.

I think the issue is closed and thanks for your help.

Caddyfile Concepts — Caddy Documentation

1 Like