Home > Enterprise >  How to use VSCode with the existing docker container
How to use VSCode with the existing docker container

Time:11-27

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related