[Django]-Can't debug django app in VS Code with docker database: 'could not translate host name "db" to address: Name or service not known'

0👍

I think that this version of your docker compose file does not support what you want. Change your docker version to 3.7 as below:

version: '3.7'
👤N01

0👍

Well you can’t debug inside docker container like this in VS code. For that, you need run the VS Code from inside of the containers. To do that, you can follow these steps:

First Step: Having Appropriate Docker Environment

Well, if you have a Dockerfile or a docker-compose.yml file, thats super cool. If you don’t have it, thats fine too. In that case, you can use an Docker Image to build the docker environment. FYI, if you are using Alpine Based Docker environment, then you need to use VS Code Insiders Edition.

Second Step: Creating .devcontainer Folder

In this step, you need to create a new folder named .devcontainer inside your source directory. Inside that, create a devcontainer.json file.

Third Step: Configuring devcontainer.json

If you are using docker-compose, then you can use the following code:

{
  "name": "Python 3",
  "context": "..",
  "dockerComposeFile": ["../docker-compose.yml"], // You need to point it your `docker-compose.yml` file with proper path.
  // Uncomment the next line if you want to publish any ports.
  "appPort": 8000,

  // Uncomment the next line to run commands after the container is created.
  // "postCreateCommand": "python --version",
  "service": "web", // You must define which service you are going to use from docker compose.
  "workspaceFolder": "/app", // path to your source inside docker file
  "extensions": [
    // extensions which is going to be installed inside the docker environment
    "ms-python.python",
    
  ],
  "settings": {
    // additional settings for VS Code configurations
    // You can copy paste them from `settings.json` of your workspace
    // Reference: https://code.visualstudio.com/docs/getstarted/settings#_settings-file-locations
    "python.pythonPath": "/usr/local/bin/python",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true
  }
}

Fourth Step: Installing Remote Development Extension Pack

Install Remote Development Extension Pack from VS Code Market, or use VS Code’s integrated market to install it:

Final Step: Run VS Code from Container

After installing, an icon will appear at bottom left of your VS Code:

(source: ruddra.com)

Now, click on that, and few options will appear like this:


(source: ruddra.com)

Now click on Remote-Containers: Reopen Folder in Container option, the VS Code will reload. Now, you are inside the Docker Environment!!

You can also create a debugger and put break points in source code to see if it hits any.

More information can be found in the official documentation or you can checkout this blog as well.


unless you are inside the docker container, the error psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known will not be resolved. Still if you intend to debug in your local machine, then you can take the advantage of your environment variable like this:

DATABASES = {
    'default': {
        'ENGINE': '...',
        'NAME': os.environ.get('db_name'),
        'USER': os.environ.get('user'),
        'PASSWORD': os.environ.get('password'),
        'HOST': os.environ.get('host'),
        'PORT': '3306',
    },
}

You need to make sure that you have a postgresql running in local machine or expose the database docker ports to access it.

👤ruddra

Leave a comment