skip to Main Content

I’ve made an Python+Django+git docker container.
Now, I would like to ‘Attach to a running container..’ with VSCode to develop, i.e. run and debug, a Python app inside.
Is it good idea? Or it is better only setting up VSCode to run app inside the container?
I don’t want VSCode make a docker container by itself.
Thanks.

I tried to ‘Attach to a running container..’ but have got ‘error xhr failed…’ etc.

3

Answers


  1. Chosen as BEST ANSWER

    I use such an environment to develop python app inside a container.

    image_create.sh # script to create image to use it local and on the server
    
    image_dockerfile # dockerfile with script how to create an image
    
    container_create.sh  # create named container from image
    
    container_restart.sh # restart existing container
    
    container_stop.sh  # stop existing container
    

    Examples:

    image_dockerfile :

    FROM python:3.9.15-slim-bullseye
    USER root
    RUN pip3 install requests telethon
    RUN apt-get update
    RUN apt-get --assume-yes install git
    

    image_create.sh :

    docker rmi python_find_a_job:lts
    docker build . -f python_find_a_job -t python_find_a_job:lts
    

    container_create.sh :

    docker rm -f python_find_a_job
    docker run -t -d --name python_find_a_job -i python_find_a_job:lts
    docker ps -aq
    

    container_restart.sh :

    docker container restart python_find_a_job
    docker ps -aq
    

    container_stop.sh :

    docker stop python_find_a_job
    docker ps -aq
    

    For VSCcode:

    a) Prepare files (see above).

    b) Run:

    image_create.sh

    container_create.sh

    c) Open project folder in VSCode

    d) Click on left bottom green / Attach to running container / select container name (python_find_a_job).

    e) Clone repository.

    f) Install extension 'Python'.

    Now you can run and debug inside the container.

    After work:

    git push

    container_stop.sh

    Before work:

    container_restart.sh

    git pull


  2. Visual Studio Code, and Docker Desktop each offer a feature called "Dev Containers" (VSCode) or "Dev Environments" (DD), or CodeSpaces (GitHub)

    In this approach, a Docker container is created by scanning the source, and generating a container that contains the development toolchain. Visual Studio then attaches to the container, and allows you to develop even though you do not have node/python3/dotnet/etc. installed on your development PC.

    The xhr error indicates something went wrong downloading a scanning image or something else is strange about your project.
    There is an optional Dockerfile that can be created if scanning fails to find an image, that is normally kept in a .devcontainers / .devenvironments folder depending on which of Docker / VSCode / GitHub / other you are using.

    Your project might also have one (or more) Dockerfile’s that are used to package the running app up as a docker image, so don’t be confused if you end up with 2. Thats not a problem and is expected really.

    Login or Signup to reply.
  3. I have got good results with VSCode using docker compose with compose.yml. But in this case, docker compose will create a new container from image every time.

    compose.yml example:

    version: '3.3'
    services:
      my_container:
        image: python_find_a_job:lts
        stdin_open: true # docker run -i
        tty: true        # docker run -t
        container_name: find_a_job
        network_mode: "host"
        volumes:
          - ~/.PYTHON/find_a_job:/opt/find_a_job
      mongodb_container:
        image: mongo:latest
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: pass
        network_mode: "host"
        ports:
          - 27017:27017
        volumes:
          - mongodb_data_container:/data/db
    volumes:
      mongodb_data_container:
    

    Before work:

    docker compose up
    

    VSCode / Attach to Running Container …

    File / Open Folder (/opt/find_a_job from above example of compose.yml).

    After work:

    docker compose down
    

    P.S. I have tried PyCharm community ed. But it can’t add Docker interpreter. Only PyCharm Professional ed. can do it.

    So I use VSCode to run & debug (F5) inside a docker container. There was some minor problems with setting VSCode tasks, extensions etc. But main part about run & debug inside a container works perfect. Changes in a project can be saved locally or pushed to git by VSCode or manually.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search