Skip to content

Flask project debug in docker container doesn't start #2716

@m-toman

Description

@m-toman

/Hi,

I've encountered the same issue as in #2313 and was not able to resolve it yet.
I am also on Linux but running a Flask app in a docker container.

Followed along https://code.visualstudio.com/docs/containers/debug-python#_for-flask-apps and ended up with:

launch.json

{
    "configurations": [
        {
            "name": "Docker: Python - Flask",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "/app"
                    }
                ],
                "projectType": "flask"
            }
        }
    ]
}

tasks.json

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "docker-build",
			"label": "docker-build",
			"platform": "python",
			"dockerBuild": {
				"tag": "gisapi:latest",
				"dockerfile": "${workspaceFolder}/Dev.Dockerfile",
				"context": "${workspaceFolder}",
				"pull": true
			}
		},
		{
			"type": "docker-run",
			"label": "docker-run: debug",
			"dependsOn": [
				"docker-build"
			],
			"dockerRun": {
				"env": {
					"FLASK_APP": "gisapi/__init__.py",
					"FLASK_ENV": "development"
				},
				"volumes": [
					{
						"containerPath": "/app",
						"localPath": "${workspaceFolder}"
					}
				],
				"ports": [
					{
						"containerPort": 8090,
						"hostPort": 8090,
						"protocol": "tcp"
					}
				]
			},
			"python": {
				"args": [
					"run",
					"--host",
					"0.0.0.0",
					"--port",
					"8090"
				],
				"module": "flask"
			}
		}
	]
}

When I run the container with F5 it just seems it just runs the "dummy" python interpreter but not actually the flask app.

If I exec -it /bin/bash into the container and run "python -m flask run --host 0.0.0.0 --port 8090" I can connect to the service and it works fine (of course without debugging).

Outputs:

Output: empty

Debug console: empty

Terminal 1: empty

Terminal 2:

> Executing task: docker-run: debug <

> docker run -dt -P --name "gisapi-dev" -e "FLASK_APP=gisapi/__init__.py" -e "FLASK_ENV=development"  --label "com.microsoft.created-by=visual-studio-code" -v "/home/mtoman/geoserver/gisapi:/app" -v "/home/mtoman/.vscode/extensions/ms-python.python-2021.1.502429796/pythonFiles/lib/python/debugpy:/debugpy:ro" -p "8090:8090/tcp" --entrypoint "python" "gisapi:latest" <

fb4ff5976e9b6b7030d8dae81c6ec23ae6cd654aef443b224cf13a05ac1dd968

Terminal will be reused by tasks, press any key to close it.

Terminal 3 (Python Debug Console): empty

At first I did not use docker-compose, after reading a few issues here I added the files as following and performed "compose up" on docker-compose.debug and then F5, but same result:

version: '3.4'

services:
  gisapi:
    image: gisapi
    build:
      context: .
      dockerfile: ./Dev.Dockerfile
    command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m flask run --no-debugger --no-reload --host 0.0.0.0 --port 8090"]
    ports:
      - 8090:8090
      - 5678:5678
    environment:
      - FLASK_APP=gisapi/__init__.py

Do I need docker compose?
If I run "compose up" it just gives me


 > Executing task: docker-compose -f "docker-compose.debug.yml" up -d --build <

Pushing the restart button gives me "There is already a debug configuration "Docker: Python - Flask" running.
Pushing stop seems to stop whatever it is doing in VS code but the docker container is still running (probably supposedly so?)

docker inspect bridge --format="{{(index .IPAM.Config 0).Gateway}}"
gives me
172.17.0.1

And docker inspect:

[
    {
        "Id": "fb4ff5976e9b6b7030d8dae81c6ec23ae6cd654aef443b224cf13a05ac1dd968",
        "Created": "2021-02-10T08:59:13.393043989Z",
        "Path": "python",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 121021,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-02-10T08:59:13.80107974Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ba5808d80a8de6e5a9e33651bad667c07a38a8ce4132947de0406ec6932af6c3",
        "ResolvConfPath": "/var/lib/docker/containers/fb4ff5976e9b6b7030d8dae81c6ec23ae6cd654aef443b224cf13a05ac1dd968/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/fb4ff5976e9b6b7030d8dae81c6ec23ae6cd654aef443b224cf13a05ac1dd968/hostname",
        "HostsPath": "/var/lib/docker/containers/fb4ff5976e9b6b7030d8dae81c6ec23ae6cd654aef443b224cf13a05ac1dd968/hosts",
        "LogPath": "/var/lib/docker/containers/fb4ff5976e9b6b7030d8dae81c6ec23ae6cd654aef443b224cf13a05ac1dd968/fb4ff5976e9b6b7030d8dae81c6ec23ae6cd654aef443b224cf13a05ac1dd968-json.log",
        "Name": "/gisapi-dev",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home/mtoman/geoserver/gisapi:/app",
                "/home/mtoman/.vscode/extensions/ms-python.python-2021.1.502429796/pythonFiles/lib/python/debugpy:/debugpy:ro"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "8090/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8090"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": true,
            "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,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "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/c8b99ea2411621fa05c2791bbf31ab18428b41287ef4711688103175748576c8-init/diff:/var/lib/docker/overlay2/f1faa8d71b19042fecc4246c2467ae96abdc2c89483591e2985e0dace20e9335/diff:/var/lib/docker/overlay2/3b210411b473609ac873f9b45fcd649efb0ccb51b824dcb7de2c5c03e118a83d/diff:/var/lib/docker/overlay2/6b913b03610cc054090c634f1e74bd7c1781ed69a6697aa0f7a73c27e660a216/diff:/var/lib/docker/overlay2/9bc9e12b9cb1095dff69300a56cfbd36f96e6616360f64dff5c635cebbfa9c6c/diff:/var/lib/docker/overlay2/7628c925f1f3641bf7c9a4d9bb66a6948eed2dbf1d348cc055e46300934db93e/diff:/var/lib/docker/overlay2/0f27688dc844d5aa4c5c510bf2a47ae09c236c40c49d8e2b2785edd8690de796/diff:/var/lib/docker/overlay2/a36d228aea208ce2b00f1d6e2500b489cb646aacdf843f9ad65189de8435c7f1/diff:/var/lib/docker/overlay2/5715b1171416b42dffa96414cd8456b641bf79a56ed384cc34a6d15fe9cb8ad7/diff:/var/lib/docker/overlay2/6a36834e85e25c117c9290ab87289f93a224d16c12f0d91ed553fff241290a1f/diff:/var/lib/docker/overlay2/79e743effc0ff392e8d760133a7ebe8e04a338c7330ed529747bb24bbbe25e91/diff:/var/lib/docker/overlay2/5d0ddab45a5ca4ac042a108ee850cf0463a4768280e2e8ffa0ae2b27b6e43e05/diff:/var/lib/docker/overlay2/f06ac4ff91b32e24b85e75b3de116cf925c3afcd5883f09feadcabfddf47137c/diff:/var/lib/docker/overlay2/0ffee031557ea51579e40a74261051e499a77949e02fc968598b9129e2fa7493/diff:/var/lib/docker/overlay2/7f4001cad9901f2c25f2c72314dfd1e3bd483286f32b16b077ae4904472ef24c/diff:/var/lib/docker/overlay2/9f2dd16e78e4a5f4bf4e5fa9838b946c55484d8a0e252bfe510744f5a3e7c010/diff:/var/lib/docker/overlay2/3d9cb408db638d2fbba1402e349a2b5f74c088be728f427b572b898a5f4d8746/diff:/var/lib/docker/overlay2/374eeee9bfcc51f4376b726d32ed96c7ba182c6ce082d3315f99bbf7df606dca/diff:/var/lib/docker/overlay2/7fbdc0be731517c6c1cefb87bedc94263a06a272fddd084bec328fb5a9ad2818/diff:/var/lib/docker/overlay2/52a31f03fbd3c0734f3a1e81f060d6620155272e58b92e17e10eaea2a93db116/diff:/var/lib/docker/overlay2/4a124dba93305e13c902aeb5adec0bc5a3056a30eb8466eabc05663b69b66f89/diff:/var/lib/docker/overlay2/426212026de255e39b06f112ceef8230f947a61ebd62515f9c7f8955b0cc3710/diff:/var/lib/docker/overlay2/fdc964e3a2e135139456342406e1050c2523d041db9a33cb78170a066f8cff62/diff:/var/lib/docker/overlay2/7bb457a3eb476c22175e114cffb54312f7dca4482a213999ed2ed4e1b4564251/diff:/var/lib/docker/overlay2/c0412af277431ef260ec862188f39f7ab86dbb828058371648f61c1623696812/diff:/var/lib/docker/overlay2/e979eb71f42f34d79a56aa61af0bf6e02e598168bce4c6efb4ad9e5e9da16108/diff:/var/lib/docker/overlay2/39285f50fbdbe12cc29171bd7d07718ca593694bd448d62b36c27d86766ad705/diff:/var/lib/docker/overlay2/c72d9c333f45c912026baa840100302660874af0b92c5b7dc2085f9aee9170f8/diff",
                "MergedDir": "/var/lib/docker/overlay2/c8b99ea2411621fa05c2791bbf31ab18428b41287ef4711688103175748576c8/merged",
                "UpperDir": "/var/lib/docker/overlay2/c8b99ea2411621fa05c2791bbf31ab18428b41287ef4711688103175748576c8/diff",
                "WorkDir": "/var/lib/docker/overlay2/c8b99ea2411621fa05c2791bbf31ab18428b41287ef4711688103175748576c8/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/mtoman/geoserver/gisapi",
                "Destination": "/app",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/home/mtoman/.vscode/extensions/ms-python.python-2021.1.502429796/pythonFiles/lib/python/debugpy",
                "Destination": "/debugpy",
                "Mode": "ro",
                "RW": false,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "fb4ff5976e9b",
            "Domainname": "",
            "User": "appuser",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8090/tcp": {}
            },
            "Tty": true,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "FLASK_APP=gisapi/__init__.py",
                "FLASK_ENV=development",
                "GISAPI_SETTING=/app/gisapi/settings_test.cfg",
                "GISAPI_LOGFILE=/var/log/gisapi.log",
                "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.6.12",
                "PYTHON_PIP_VERSION=21.0.1",
                "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/4be3fe44ad9dedc028629ed1497052d65d281b8e/get-pip.py",
                "PYTHON_GET_PIP_SHA256=8006625804f55e1bd99ad4214fd07082fee27a1c35945648a58f9087a714e9d4",
                "PYTHONDONTWRITEBYTECODE=1",
                "PYTHONUNBUFFERED=1"
            ],
            "Cmd": null,
            "Image": "gisapi:latest",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": [
                "python"
            ],
            "OnBuild": null,
            "Labels": {
                "com.microsoft.created-by": "visual-studio-code"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "df3bef31eafda3d23fdce090a3f981da7ee449e8acdc7b199ad2ad02fa0e7c5b",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8090/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8090"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/df3bef31eafd",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "b6d21ed7c0b0d9f3e66983366ef5acdeaa391663c86fdf0e58c207fbf52d38b4",
            "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": "eec3d6222fee208f46be648d684daee193314060167e9fbad637216ecbd4104a",
                    "EndpointID": "b6d21ed7c0b0d9f3e66983366ef5acdeaa391663c86fdf0e58c207fbf52d38b4",
                    "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
                }
            }
        }
    }
]


Thanks!

EDIT: Dev.Dockerfile:

# For more information, please refer to https://aka.ms/vscode-docker-python
#FROM python:3.8-slim-buster
FROM python:3.6

RUN apt-get update
RUN apt-get install -y apt-utils 
# TODO: fix versions
RUN apt-get install -y libgdal-dev
RUN apt-get install -y libmapnik-dev
RUN apt-get install -y mapnik-utils
RUN apt-get install -y libboost-python-dev gcc

RUN pip install flask
RUN pip install pygdal==`gdal-config --version`.*
RUN pip install owslib
RUN pip install psycopg2-binary
RUN pip install gevent

#== mapnik
# TODO archive not maintained, get via git clone?
RUN wget https://github.com/mapnik/python-mapnik/archive/v3.0.16.tar.gz && \
     tar -xf v3.0.16.tar.gz && rm v3.0.16.tar.gz && \
     cd python-mapnik-3.0.16 && \
     python setup.py install

EXPOSE 8090

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app

# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd appuser && chown -R appuser /app
RUN touch /var/log/gisapi.log && chown appuser /var/log/gisapi.log
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "-m", "flask", "run", "--host", "0.0.0.0", "--port", "8090"]

EDIT2:
ping 172.17.0.1 and ping 172.17.0.2 works.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions