简体   繁体   中英

How To Access Docker Container Files From Vscode?

I am following this https://towardsdatascience.com/creating-your-own-object-detector-ad69dda69c85 to experiment with Tensorflow object detection. The tutorial does not use docker but I am trying to learn it as well so I am using docker.

Right now I have a problem. Currently whenever I make updates to anything I need to run

docker build -f research/object_detection/dockerfiles/tf2/Dockerfile -t od .

before any of the changes are recognized by my docker container. For example if I add a folder and try to access it before running docker build again, I will receive a file not found error. Same deal with any code files. Any python script I update must be saved and then I must run docker build again.

In addition one of the scripts I am using creates a new file, that file seems to only be accessible while I have the same cmd where I entered the command to create the file open. If I close that cmd and run (docker run -it od) to open another container the file is no longer accessible.

How can I directly access the files inside docker containers/images (whatever it is at this point I have a headache and no clue what is going on)

I looked into vscode's remote container extension but I am unsure how to access whatever the docker build command created.

Any help would be greatly appreciated. TIA

EDIT: I run the command docker run -it od(also ran the build from here too) in Users/zkj/models

My goal is to have any changes from Users/zkj/models/research/object_detection to be registered to the docker.

Broadly, there are four ways to accomplish this. I'm going to describe the simplest solutions first, and most complex last.

  1. Bind mount the source files into the container. Bind mounts allow you to create a directory which is accessible on both the container and the host. Here's an example. Suppose you have the directory /home/zkj/tensorflow . You want this to be available inside the docker container as /code . You would use the following option to docker run :

     docker run -v /home/zkj/tensorflow:/code...

    The advantage of a bind mount is that a change to one of the files inside the bind mount is instantly reflected inside the container. (Note: you may need to run docker run again to re-run the program.)

    ( Documentation .)

  2. Use docker cp to copy files out of the container. You must know the name of the container. This is not the same as the name of the image! Use docker ps to find the name of the container.

    ( Documentation .)

  3. Re-arrange the RUN steps so that expensive steps happen first, and copying your source code in happens last. This is technically not what you asked for, but by re-arranging the layers in your docker image, you can take advantage of caching, and make builds 10-100x times faster, which makes docker build much less painful.

    Here's an example. Suppose I have the following Dockerfile, which copies my source code in and installs dependencies:

     COPY code / RUN pip install -r /code/requirements.txt

    The problem with this is that it doesn't take advantage of build caching. If you make any change to your code, that will invalidate the cache for the first step, which in turn means that the step afterwards will be invalidated too. But if you install dependencies first, you will only need to re-build that step when your dependencies change:

     COPY code/requirements.txt / RUN pip install -r /requirements.txt COPY code /

    In this version, the first two steps can be cached, and the third step is very fast.

  4. Use docker exec to get a shell inside the container. Use docker ps to get the name of the container. Then, run:

     docker exec -it <container name> /bin/bash

    What's the difference between docker run and docker exec ? docker run creates a new container. docker exec runs a command inside an existing container.

The simplest way is to expose the folder with source files to docker container when you are building it using -v <your-local-folder>:/home . This way any files created during your container operation will also be accessible in your local folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM