简体   繁体   English

如何从 Vscode 访问 Docker 容器文件?

[英]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.我正在关注这个https://towardsdatascience.com/creating-your-own-object-detector-ad69dda69c85来试验 Tensorflow ZA8CFDE6331BD59EB2AC966F8911C46 检测。 The tutorial does not use docker but I am trying to learn it as well so I am using docker.本教程不使用 docker,但我也在尝试学习它,所以我使用的是 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.在我的 docker 容器识别任何更改之前。 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.例如,如果我添加一个文件夹并在再次运行 docker 构建之前尝试访问它,我将收到一个找不到文件的错误。 Same deal with any code files.同样处理任何代码文件。 Any python script I update must be saved and then I must run docker build again.必须保存我更新的任何 python 脚本,然后我必须再次运行 docker 构建。

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.此外,我使用的脚本之一创建了一个新文件,该文件似乎只有在我有相同的 cmd 时才能访问,我在其中输入了创建文件的命令打开。 If I close that cmd and run (docker run -it od) to open another container the file is no longer accessible.如果我关闭该 cmd 并运行(docker run -it od)以打开另一个容器,则该文件将不再可访问。

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)如何直接访问 docker 容器/图像中的文件(无论是什么,此时我都头疼,不知道发生了什么)

I looked into vscode's remote container extension but I am unsure how to access whatever the docker build command created.我查看了 vscode 的远程容器扩展,但我不确定如何访问 docker 构建命令创建的任何内容。

Any help would be greatly appreciated.任何帮助将不胜感激。 TIA TIA

EDIT: I run the command docker run -it od(also ran the build from here too) in Users/zkj/models编辑:我在 Users/zkj/models 中运行命令 docker run -it od(也从这里运行构建)

My goal is to have any changes from Users/zkj/models/research/object_detection to be registered to the docker.我的目标是将用户/zkj/models/research/object_detection 的任何更改注册到 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 .假设您有目录/home/zkj/tensorflow You want this to be available inside the docker container as /code .您希望它在 docker 容器中作为/code可用。 You would use the following option to docker run :您将使用以下选项来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.) (注意:您可能需要运行docker run才能重新运行程序。)

    ( Documentation .) 文档。)

  2. Use docker cp to copy files out of the container.使用docker cp将文件复制出容器。 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.使用docker ps查找容器的名称。

    ( Documentation .) 文档。)

  3. Re-arrange the RUN steps so that expensive steps happen first, and copying your source code in happens last.重新安排RUN步骤,使昂贵的步骤首先发生,最后复制源代码。 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.这在技术上不是您所要求的,但是通过重新排列 docker 映像中的层,您可以利用缓存,并使构建速度提高 10-100 倍,这使得docker build的痛苦大大减少。

    Here's an example.这是一个例子。 Suppose I have the following Dockerfile, which copies my source code in and installs dependencies:假设我有以下 Dockerfile,它会复制我的源代码并安装依赖项:

     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.使用docker exec在容器内获取 shell。 Use docker ps to get the name of the container.使用docker ps获取容器的名称。 Then, run:然后,运行:

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

    What's the difference between docker run and docker exec ? docker rundocker exec有什么区别? docker run creates a new container. docker run创建一个容器。 docker exec runs a command inside an existing container. docker exec现有容器中运行命令。

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 .最简单的方法是在使用-v <your-local-folder>:/home构建时将包含源文件的文件夹公开给 docker 容器。 This way any files created during your container operation will also be accessible in your local folder.这样,在您的容器操作期间创建的任何文件也可以在您的本地文件夹中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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