Newbie: ERR_EMPTY_RESPONSE for Python service that works fine when not in container

My python service causes the browser to show ERR_EMPTY_RESPONSE when running inside the container. The same service running without the container works fine. In both cases I am running the service on the same machine as the client browser.

BACKGROUND
Docker newbie (1 week)
Mackbook Pro on OS Sierra w/ 16MB
Docker version = 18.09.2
Python version = 3.7.3

I used a Python virtual environment both inside and outside the container because I was unable to get Python to build otherwise. It kept failing while loading Python pandas for obscure reasons and the virtual environment cleaned all that up. I know this is not typical for containers … could this be the problem? (Hopefully not as I spent the bulk of 2 days wrestling my Python build to the ground.)

The expected response from the service is 21 KB of JSON.

NO DOCKER - THIS WORKS FINE
SHELL SCRIPT TO LAUNCH SERVICE:

#!/bin/bash
python3 -m venv env/
env/bin/pip3.7 install pip --upgrade
env/bin/pip3.7 install --no-cache-dir -r requirements.txt
env/bin/python3 code/p_search.py /Users/wallace/GitHub/pluot-labs/translate

Requirements.txt

numpy==1.16.3
pandas==0.24.2
tensorflow==1.13.1
nltk==3.4.1
sklearn==0.0
Flask==1.0.2

Results:
Final line of console: Running on http://localhost:5000/ (Press CTRL+C to quit)
URL to invoke service: http://localhost:5000/pluot/api/v1.0/search?q=jacket

WITH DOCKER - THIS FAILS TO WORK
SHELL SCRIPT TO LAUNCH DOCKER:

#!/bin/bash
docker build -t translate-test -f Dockerfile.test .
docker run -d -p 5000:5000 --name=translate translate-test
docker ps

Dockerfile.test

FROM python:3.7
WORKDIR translate

RUN python3 -m venv env/

COPY requirements.txt requirements.txt
RUN env/bin/pip3.7 install pip --upgrade
RUN env/bin/pip3.7 install --no-cache-dir -r requirements.txt

COPY code code
COPY data data

CMD env/bin/python3 /translate/code/p_search.py /translate

EXPOSE 5000

Results:
Final line of container logs: Running on http://localhost:5000/ (Press CTRL+C to quit)

docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9da0fec56a0 translate-test “/bin/sh -c 'env/bin…” 4 minutes ago Up 4 minutes 0.0.0.0:5000->5000/tcp translate

Client side - CHROME:

#This page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE

Client side - FIREFOX:

The connection was reset
The connection to the server was reset while the page was loading.

NOTE that this is different from a refused connection. If I change the URL port to 5001 (an unused port) I get the expected ERR_CONNECTION_REFUSED.

What have I tried?
Lots of forum surfing, especially Stack Overflow and this one. Although there were many issues around port numbers and connecting, I could not find anyone else with my problem.
Changed “-p 5000:5000” to -P
Different browsers
docker system prune
I’m kind of green, so don’t have a lot of ideas.

Docker Inspect
Usus:translate wallace$ docker inspect translate
[
{
“Id”: “e9da0fec56a0b1a2a178e3703a804922b40080423b67d8d93cbf054e2f7ff6f0”,
“Created”: “2019-04-26T05:58:36.9807123Z”,
“Path”: “/bin/sh”,
“Args”: [
“-c”,
“env/bin/python3 /translate/code/p_search.py /translate”
],
“State”: {
“Status”: “running”,
“Running”: true,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 26854,
“ExitCode”: 0,
“Error”: “”,
“StartedAt”: “2019-04-26T05:58:37.6570818Z”,
“FinishedAt”: “0001-01-01T00:00:00Z”
},
“Image”: “sha256:5f165431e2094d799f242c03585c7cdb06dad201a0eba3bf83cd867d748f3f0c”,
“ResolvConfPath”: “/var/lib/docker/containers/e9da0fec56a0b1a2a178e3703a804922b40080423b67d8d93cbf054e2f7ff6f0/resolv.conf”,
“HostnamePath”: “/var/lib/docker/containers/e9da0fec56a0b1a2a178e3703a804922b40080423b67d8d93cbf054e2f7ff6f0/hostname”,
“HostsPath”: “/var/lib/docker/containers/e9da0fec56a0b1a2a178e3703a804922b40080423b67d8d93cbf054e2f7ff6f0/hosts”,
“LogPath”: “/var/lib/docker/containers/e9da0fec56a0b1a2a178e3703a804922b40080423b67d8d93cbf054e2f7ff6f0/e9da0fec56a0b1a2a178e3703a804922b40080423b67d8d93cbf054e2f7ff6f0-json.log”,
“Name”: “/translate”,
“RestartCount”: 0,
“Driver”: “overlay2”,
“Platform”: “linux”,
“MountLabel”: “”,
“ProcessLabel”: “”,
“AppArmorProfile”: “”,
“ExecIDs”: null,
“HostConfig”: {
“Binds”: null,
“ContainerIDFile”: “”,
“LogConfig”: {
“Type”: “json-file”,
“Config”: {}
},
“NetworkMode”: “default”,
“PortBindings”: {
“5000/tcp”: [
{
“HostIp”: “”,
“HostPort”: “5000”
}
]
},
“RestartPolicy”: {
“Name”: “no”,
“MaximumRetryCount”: 0
},
“AutoRemove”: false,
“VolumeDriver”: “”,
“VolumesFrom”: null,
“CapAdd”: null,
“CapDrop”: null,
“Dns”: ,
“DnsOptions”: ,
“DnsSearch”: ,
“ExtraHosts”: null,
“GroupAdd”: null,
“IpcMode”: “shareable”,
“Cgroup”: “”,
“Links”: null,
“OomScoreAdj”: 0,
“PidMode”: “”,
“Privileged”: false,
“PublishAllPorts”: false,
“ReadonlyRootfs”: false,
“SecurityOpt”: null,
“UTSMode”: “”,
“UsernsMode”: “”,
“ShmSize”: 67108864,
“Runtime”: “runc”,
“ConsoleSize”: [
0,
0
],
“Isolation”: “”,
“CpuShares”: 0,
“Memory”: 0,
“NanoCpus”: 0,
“CgroupParent”: “”,
“BlkioWeight”: 0,
“BlkioWeightDevice”: ,
“BlkioDeviceReadBps”: null,
“BlkioDeviceWriteBps”: null,
“BlkioDeviceReadIOps”: null,
“BlkioDeviceWriteIOps”: null,
“CpuPeriod”: 0,
“CpuQuota”: 0,
“CpuRealtimePeriod”: 0,
“CpuRealtimeRuntime”: 0,
“CpusetCpus”: “”,
“CpusetMems”: “”,
“Devices”: ,
“DeviceCgroupRules”: null,
“DiskQuota”: 0,
“KernelMemory”: 0,
“MemoryReservation”: 0,
“MemorySwap”: 0,
“MemorySwappiness”: null,
“OomKillDisable”: false,
“PidsLimit”: 0,
“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/93dcef3b72aab8c05aad75735a0e16749e3ef9c68abb1d77c93afbbf8f4954c7-init/diff:/var/lib/docker/overlay2/9cffbe31412c7384ab1b6b201af85c0e1023a054311e0b5a57621b9adad914c6/diff:/var/lib/docker/overlay2/dacc179b5c7ca9e439489186c399b9cc95073420a6da808114afc9c7b0afca64/diff:/var/lib/docker/overlay2/e44bf9bbb4d99a0aba524cc319d5c98c846180fb54791d2c89cee545f5566084/diff:/var/lib/docker/overlay2/ee3d4a301053230f53fd7528ad88b4e2e9ea993a56879660786743c356461cb8/diff:/var/lib/docker/overlay2/eb187aedfe0c307ca70030f31f50ce6b6b57766f274ffaa7c11cd80700d44882/diff:/var/lib/docker/overlay2/6a2e98d107319a539befe8f67bd6efac71b31d93f173a0eba158e3a9978d0bb8/diff:/var/lib/docker/overlay2/3ed24027a38215ff71f71d1801bb84fc648914e6e3df282751acb8461ee6bc3b/diff:/var/lib/docker/overlay2/2c282168e6e35a3f05eb0083ca0576047aab2fb27ecb036a855ff1bacce5feb3/diff:/var/lib/docker/overlay2/d321b2b7e9b5f68b2b7f7bd88489649f474def01f94fcc6c2d251e3d7e8c138e/diff:/var/lib/docker/overlay2/9dc959b3e87909549a08025bc93d3434e0ee15e0b2bebd995d1a728f2bcfee5c/diff:/var/lib/docker/overlay2/39e2cf4f5b3c99a131a11473990dcafc8e42a5005724d4cfcf1271e4c7017bd3/diff:/var/lib/docker/overlay2/deb716eff9957beeedd184e4c91a716d05f3c7804e94f964057cfd719a56ca4e/diff:/var/lib/docker/overlay2/dab0d6077756abd1fe431b9922ab116a0446e13c1aafa2c13b201b2081e308fe/diff:/var/lib/docker/overlay2/e5004ecade1cfc37521cb87b55a91ef5e7fc17175975be47654e0e73dea4d51e/diff:/var/lib/docker/overlay2/6d4d3ab981ca9d236ce74cfa2142cd920fcc70226b2aeb2bbf857a97bd27714e/diff:/var/lib/docker/overlay2/e856e85f075e746f488064e9d3c8ea474062e1e6c973fc65a77bf276f443831d/diff”,
“MergedDir”: “/var/lib/docker/overlay2/93dcef3b72aab8c05aad75735a0e16749e3ef9c68abb1d77c93afbbf8f4954c7/merged”,
“UpperDir”: “/var/lib/docker/overlay2/93dcef3b72aab8c05aad75735a0e16749e3ef9c68abb1d77c93afbbf8f4954c7/diff”,
“WorkDir”: “/var/lib/docker/overlay2/93dcef3b72aab8c05aad75735a0e16749e3ef9c68abb1d77c93afbbf8f4954c7/work”
},
“Name”: “overlay2”
},
“Mounts”: ,
“Config”: {
“Hostname”: “e9da0fec56a0”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“ExposedPorts”: {
“5000/tcp”: {}
},
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“LANG=C.UTF-8”,
“GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D”,
“PYTHON_VERSION=3.7.3”,
“PYTHON_PIP_VERSION=19.1”
],
“Cmd”: [
“/bin/sh”,
“-c”,
“env/bin/python3 /translate/code/p_search.py /translate”
],
“ArgsEscaped”: true,
“Image”: “translate-test”,
“Volumes”: null,
“WorkingDir”: “/translate”,
“Entrypoint”: null,
“OnBuild”: null,
“Labels”: {}
},
“NetworkSettings”: {
“Bridge”: “”,
“SandboxID”: “68d11fd0146c6587a9523d2f965440f85d038e96fb3a24c42656157c0acd594c”,
“HairpinMode”: false,
“LinkLocalIPv6Address”: “”,
“LinkLocalIPv6PrefixLen”: 0,
“Ports”: {
“5000/tcp”: [
{
“HostIp”: “0.0.0.0”,
“HostPort”: “5000”
}
]
},
“SandboxKey”: “/var/run/docker/netns/68d11fd0146c”,
“SecondaryIPAddresses”: null,
“SecondaryIPv6Addresses”: null,
“EndpointID”: “75fdf6a1b352a2b938d7baadabc81a42d15c45b0ce2ca3878c3f807257405327”,
“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”: “d08f5997f3393cc984afe5d895d97b90ffb9aa623fbc991c83510f0391a1cdb6”,
“EndpointID”: “75fdf6a1b352a2b938d7baadabc81a42d15c45b0ce2ca3878c3f807257405327”,
“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
}
}
}
}
]

Did you ever figure this out? I have exactly the same problem only with a much simpler python http.server webpage. It works perfectly when i run python server.py, but when I do it in docker compose i get no logs and ERR_EMPTY_RESPONSE. I have verified the tcp port is listening (plus i wouldn’t have gotten the ERR_EMPTY_RESPONSE) if it was not reaching the server. Postman didn’t help either, just got a socket hang up error.

hola buenas tarde, queria saber si ya han resuelto ese problema porque estoy en el mismo problema, ejecuto fastapi en mi maquina y funciona bien, pero lo ejecuto con docker y me sale el mismo error que a ustedes, y en los logs todo esta funcionando bien

en mi maquina - navegador chrome

Esta página no funciona

localhost no ha enviado ningún dato.

ERR_EMPTY_RESPONSE

  • en el contenedor de docker (logs)

023-07-18 12:23:35 INFO: Will watch for changes in these directories: [‘/app’]
2023-07-18 12:23:35 INFO: Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)
2023-07-18 12:23:35 INFO: Started reloader process [1] using StatReload
2023-07-18 12:23:35 INFO: Started server process [8]
2023-07-18 12:23:35 INFO: Waiting for application startup.
2023-07-18 12:23:35 INFO: Application startup complete.