OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Issues in Debugging a Dockerized Web App in VS Code

  • Thread starter Thread starter Muhammad Ibtesam
  • Start date Start date
M

Muhammad Ibtesam

Guest
I am learning web development on my own through different youtube videos and books. I am using VS code and started building my own web app in Docker containers (read somewhere regarding benefits of using containers) . I have built a few apps within the project, however, as the apps are getting complex and complex it is difficult for me to debug the app for errors and see where am I going wrong. I am going to lay down the code I am using for docker files and task.json and launch.json.

DOCKERFILE


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

EXPOSE 8000
# Expose port for debugpy
EXPOSE 5678

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

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

# Install debugpy for debugging
RUN python -m pip install debugpy

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

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# # During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
# CMD ["gunicorn", "--bind", "0.0.0.0:8000", "DRM_Inventory.wsgi"]

# Command to start Django with debugpy

CMD ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading --noreload"]

# docker-compose.yml

Code:
version: "3.9"
services:
  web:
    build: .
    command: python /DRM_Django_App/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/DRM_Django_App
    ports:
      - 8000:8000
    depends_on:
      - db
  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"
    ports:
      - "5432:5432" # Expose PostgreSQL default port for debugging
    command:
      [
        "postgres",
        "-c",
        "log_statement=all",
        "-c",
        "log_destination=stderr"
      ]
volumes:
  postgres_data:

# docker-compose.debug.yml

Code:
version: '3.4'

services:
  drmdjangoapp:
    image: drmdjangoapp
    build:
      context: .
      dockerfile: ./Dockerfile
    command:
      [
        "sh",
        "-c",
        "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading --noreload"
      ]
    ports:
      - 8000:8000
      - 5678:5678
    volumes:
      - .:/app
    depends_on:
      - db

  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"
    ports:
      - "5432:5432"
volumes:
  postgres_data:

task.json:

Code:
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build",
            "platform": "python",
            "dockerBuild": {
                "tag": "drmdjangoapp:latest",
                "dockerfile": "${workspaceFolder}/Dockerfile",
                "context": "${workspaceFolder}",
                "pull": true
            }
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": [
                "docker-build"
            ],
            "dockerRun": {
                "env": {
                    "PYTHONUNBUFFERED": "1"
                },
                "ports": [
                    {
                        "containerPort": 8000,
                        "hostPort": 8000
                    },
                    {
                        "containerPort": 5678,
                        "hostPort": 5678
                    }
                ],
                "volumes": [
                    {
                        "containerPath": "/app",
                        "localPath": "${workspaceFolder}"
                    }
                ]
            }
        }
    ]
}

launch.json


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

I have tried removing all these files and used the command pallet to create all these files again from the scratch and update the relevant portions according to a few videos on the topic, but to no avail. I need help in sorting out the debugging and If there are any setting that I am missing or have done wrongly.
<p>I am learning web development on my own through different youtube videos and books. I am using VS code and started building my own web app in Docker containers (read somewhere regarding benefits of using containers) . I have built a few apps within the project, however, as the apps are getting complex and complex it is difficult for me to debug the app for errors and see where am I going wrong. I am going to lay down the code I am using for docker files and task.json and launch.json.</p>
<h1><strong>DOCKERFILE</strong></h1>
<pre><code># For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3-slim

EXPOSE 8000
# Expose port for debugpy
EXPOSE 5678

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

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

# Install debugpy for debugging
RUN python -m pip install debugpy

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

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# # During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
# CMD ["gunicorn", "--bind", "0.0.0.0:8000", "DRM_Inventory.wsgi"]

# Command to start Django with debugpy

CMD ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading --noreload"]
</code></pre>
<p><strong># docker-compose.yml</strong></p>
<pre><code>version: "3.9"
services:
web:
build: .
command: python /DRM_Django_App/manage.py runserver 0.0.0.0:8000
volumes:
- .:/DRM_Django_App
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:13
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
ports:
- "5432:5432" # Expose PostgreSQL default port for debugging
command:
[
"postgres",
"-c",
"log_statement=all",
"-c",
"log_destination=stderr"
]
volumes:
postgres_data:
</code></pre>
<p><strong># docker-compose.debug.yml</strong></p>
<pre><code>version: '3.4'

services:
drmdjangoapp:
image: drmdjangoapp
build:
context: .
dockerfile: ./Dockerfile
command:
[
"sh",
"-c",
"pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --nothreading --noreload"
]
ports:
- 8000:8000
- 5678:5678
volumes:
- .:/app
depends_on:
- db

db:
image: postgres:13
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
ports:
- "5432:5432"
volumes:
postgres_data:
</code></pre>
<p>task.json:</p>
<pre><code>{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "drmdjangoapp:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"dockerRun": {
"env": {
"PYTHONUNBUFFERED": "1"
},
"ports": [
{
"containerPort": 8000,
"hostPort": 8000
},
{
"containerPort": 5678,
"hostPort": 5678
}
],
"volumes": [
{
"containerPath": "/app",
"localPath": "${workspaceFolder}"
}
]
}
}
]
}
</code></pre>
<h1><strong>launch.json</strong></h1>
<pre><code>{
"version": "0.2.0",
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
]
}
</code></pre>
<p>I have tried removing all these files and used the command pallet to create all these files again from the scratch and update the relevant portions according to a few videos on the topic, but to no avail. I need help in sorting out the debugging and If there are any setting that I am missing or have done wrongly.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
6
Total visitors
6
Top