I am a newbie in Docker, just. following some tutorial, and tried to dockerize a simple dotnet core 7 rest api app, which listens to http://localhost:5206.
Everything looks ok, when I try to do the build steps from inside the base ubuntu image. Even Docker build also succeeds, even docker run also succeeds.
But I am not able to access the app from host no matter what I try:
I am on Mac 12.6:
Dockerfile:
`FROM ubuntu
RUN apt-get update
RUN apt-get install -y dotnet-sdk-7.0
RUN apt-get install -y aspnetcore-runtime-7.0
RUN apt-get install -y git
COPY /helloWorld/ /app
WORKDIR /app
EXPOSE 5206
ENTRYPOINT dotnet run --project helloWorld.csproj`
Build Output:
`[+] Building 0.1s (12/12) FINISHED
=> [internal] load build definition from Dockerfile
=> => transferring dockerfile: 524B
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [internal] load metadata for docker.io/library/ubuntu:latest
=> [1/7] FROM docker.io/library/ubuntu
=> [internal] load build context
=> => transferring context: 4.26kB
=> CACHED [2/7] RUN apt-get update
=> CACHED [3/7] RUN apt-get install -y dotnet-sdk-7.0
=> CACHED [4/7] RUN apt-get install -y aspnetcore-runtime-7.0
=> CACHED [5/7] RUN apt-get install -y git
=> CACHED [6/7] COPY /helloWorld/ /app
=> CACHED [7/7] WORKDIR /app
=> exporting to image
=> => exporting layers
=> => writing image sha256:9de0bdec1be190f4bde20e211a746f3a813353dca1124b849086f18ef0437312
=> => naming to docker.io/dotnet/api/1 `
Run Command:
`docker run -p 127.0.0.1:8080:5206 9de0bdec1be1`
Output:
`Welcome to .NET 7.0!
---------------------
SDK Version: 7.0.105
----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app`
Now trying to access http//:127.0.0.1:8080 or http://localhost:8080 from my Mac’s chrome browser results in ‘Site cant be reached’
Inspect output:
`[
{
"Id": "baef637d76ec08644cef9e642938d3912719267d1eb3adb6f6debbc7f6cd6224",
"Created": "2023-06-11T02:34:22.251278271Z",
"Path": "/bin/sh",
"Args": [
"-c",
"dotnet run --project helloWorld.csproj"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 94955,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-06-11T02:34:22.511203476Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:9de0bdec1be190f4bde20e211a746f3a813353dca1124b849086f18ef0437312",
"ResolvConfPath": "/var/lib/docker/containers/baef637d76ec08644cef9e642938d3912719267d1eb3adb6f6debbc7f6cd6224/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/baef637d76ec08644cef9e642938d3912719267d1eb3adb6f6debbc7f6cd6224/hostname",
"HostsPath": "/var/lib/docker/containers/baef637d76ec08644cef9e642938d3912719267d1eb3adb6f6debbc7f6cd6224/hosts",
"LogPath": "/var/lib/docker/containers/baef637d76ec08644cef9e642938d3912719267d1eb3adb6f6debbc7f6cd6224/baef637d76ec08644cef9e642938d3912719267d1eb3adb6f6debbc7f6cd6224-json.log",
"Name": "/reverent_cohen",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
** "5206/tcp": [
{
"HostIp": "127.0.0.1",
"HostPort": "8080"
}**
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
53,
190
],
"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/85b0df60bdcf8d34a5d2896f26e10dd46ed896520e72fc4d4420da001a738956-init/diff:/var/lib/docker/overlay2/owr15a3kq6meankosw96l2stv/diff:/var/lib/docker/overlay2/fto2qe2rhicv7anktmku3bh3i/diff:/var/lib/docker/overlay2/qohre7sxyinw4iki1le0vymfz/diff:/var/lib/docker/overlay2/iezfa6seuu9k27cdtjyehvn4d/diff:/var/lib/docker/overlay2/u5f34z9qtne9sfomvl6j5wnw2/diff:/var/lib/docker/overlay2/rb61ba4hwhzq00qmzeftqdipp/diff:/var/lib/docker/overlay2/e330dc80a6050f494584fe2311e31862c8ce98d6392fa58c1c231b943c28a693/diff",
"MergedDir": "/var/lib/docker/overlay2/85b0df60bdcf8d34a5d2896f26e10dd46ed896520e72fc4d4420da001a738956/merged",
"UpperDir": "/var/lib/docker/overlay2/85b0df60bdcf8d34a5d2896f26e10dd46ed896520e72fc4d4420da001a738956/diff",
"WorkDir": "/var/lib/docker/overlay2/85b0df60bdcf8d34a5d2896f26e10dd46ed896520e72fc4d4420da001a738956/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "baef637d76ec",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"ExposedPorts": {
"5206/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": null,
"Image": "9de0bdec1be1",
"Volumes": null,
"WorkingDir": "/app",
"Entrypoint": [
"/bin/sh",
"-c",
"dotnet run --project helloWorld.csproj"
],
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "22.04"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "df620ef8f9b0f094344c87ba6511a7bfc90587566cc905418e39b91d526e4a97",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
** "Ports": {
"5206/tcp": [
{
"HostIp": "127.0.0.1",
"HostPort": "8080"
}
]
}**,
"SandboxKey": "/var/run/docker/netns/df620ef8f9b0",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7fa795a95fafad9ec1f1ad835edc3337758ce99e6630f6b04ee99d0731d4e315",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "8df8403af0189a1f4ca71732d675d7533e48035088d754bfdb6088046f44ead2",
"EndpointID": "7fa795a95fafad9ec1f1ad835edc3337758ce99e6630f6b04ee99d0731d4e315",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]`
OR:
% docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
baef637d76ec 9de0bdec1be1 "/bin/sh -c 'dotnet …" 27 seconds ago Up 26 seconds 127.0.0.1:8080->5206/tcp reverent_cohen
I can clearly see ports are mapped, but not sure why it doesnot work, other run configuration I tried:
docker run -p 80:5206 9de0bdec1be1
docker run -p 5206:5206 9de0bdec1be1
Each time I can see the ports are mapped in NetworkSettings section of inspect, but with same results. Also I am sure ports used for host are all available(8080, 80, 5206)
What am I doing wrong, is there anything else to check in the inspect? Frustrateted that I am stuck in this basic step preventing me to move to next topic in my learning path, please help.
docker run -d -p 127.0.0.1:80:5206 9de0bdec1be1
docker run -p 80:5206 9de0bdec1be1
docker run -p 5206:5206 9de0bdec1be1
Was expecting to able to access the app from host’s(Mac) web browser.
Note each time I try a new run config, I stop the previous running container.
% docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
%
2
Answers
In your config I see empty property for 5206 port, That ia one area to look for possible config mistake.
Another thing is, is your project really running and delivering a response? can you try to connect to the container and find a way to check this before testing on your browser?
Best!
Makanist
You can try by providing actual ip address of your machine in the run command,
E.g. docker run -p actual_ip_address:8080:5206
And then access the url http://actual_ip_address:8080/