0

Im having trouble connecting to a django process running inside a container spawned with vs-code. Everything seem to be working and I get the startup message for the server, but when connecting to localhost:8000, I get no response...

I get a published port message when starting the container:

Published Ports: 8000/tcp -> 127.0.0.1:8000

and also a clean start when starting launch.json debug

System check identified no issues (0 silenced). October 13, 2019 - 17:45:05 Django version 2.2.6, using settings 'fpl-django.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

Why cant I access the site on: localhost:8000?

devcontainer.json:

    // For format details, see https://aka.ms/vscode-remote/devcontainer.json or the definition README at
// https://github.com/microsoft/vscode-dev-containers/tree/master/containers/docker-existing-dockerfile
{
  "name": "Existing Dockerfile",
  // Sets the run context to one level up instead of the .devcontainer folder.
  "context": "..",
  // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
  "dockerFile": "../docker/dev/python/Dockerfile",
  // The optional 'runArgs' property can be used to specify additional runtime arguments.
  "runArgs": [
    // Uncomment the next line to use Docker from inside the container. See https://aka.ms/vscode-remote/samples/docker-in-docker for details.
    // "-v","/var/run/docker.sock:/var/run/docker.sock",
    // Uncomment the next line if you will be using a ptrace-based debugger like C++, Go, and Rust.
    // "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined"
    // You may want to add a non-root user to your Dockerfile. On Linux, this will prevent
    // new files getting created as root. See https://aka.ms/vscode-remote/containers/non-root-user
    // for the needed Dockerfile updates and then uncomment the next line.
    // "-u", "vscode"
    "--network",
    "fpl-django_default"
  ],
  // Use 'settings' to set *default* container specific settings.json values on container create.
  // You can edit these settings after create using File > Preferences > Settings > Remote.
  "settings": {
    "terminal.integrated.shell.linux": "/bin/bash",
    "python.pythonPath": "/usr/local/bin/python",
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "/usr/local/bin/pylint",
    "python.linting.enabled": true
  },
  // 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 - for example installing git.
  // "postCreateCommand": "apt-get update && apt-get install -y git",
  // Add the IDs of extensions you want installed when the container is created in the array below.
  "extensions": [
    "ms-python.python",
  ]
}

launch.json:

    {
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Django",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/manage.py",
      "console": "integratedTerminal",
      "args": [
        "runserver",
        "--noreload",
        "--nothreading"
      ],
      "django": true
    }
  ]
}
5
  • 1
    Hi @TheKaizer, which OS you're using now? With Windows OS, sometime I have to use IP like 192.168.x.x to be able to connect to container. And I also have to run my app using this: "./manage.py runserver 0.0.0.0:8000 --noreload --nothreading" to be able to connect from outside Commented Oct 13, 2019 at 17:59
  • @ToanQuocHo Im using Mac! Will try with your setup and get back to you! Commented Oct 14, 2019 at 6:34
  • Have you tried using the 'Remote - Container' extension to develop within your container? Commented Oct 18, 2019 at 17:49
  • @ToanQuocHo it worked, do you want to add it as an answer so I can accept it? Commented Oct 19, 2019 at 15:56
  • Glad to hear that @TheKaizer Commented Oct 19, 2019 at 16:19

1 Answer 1

5

Try to run manage.py runserver with this param: 0.0.0.0:8000, this will tell your app to listen request from any host. Like so:

{
      "name": "Python: Django",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/manage.py",
      "console": "integratedTerminal",
      "args": [
        "runserver",
        "0.0.0.0:8000",
        "--noreload",
        "--nothreading"
      ],
      "django": true
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.