简体   繁体   中英

How to open files in a Docker container outside of container using the Remote Development extension for VSCode

Is it possible to open files in a Docker container in my local operating system outside of the container using the Remote Development extension for VSCode (such as using right-click 'Open in Explorer' as in the Remote - WSL extension for VSCode )

I tried to:

  • Build Docker Image Without Context
  • Mount filesystem Volume so that I could access files in my operating system from my container without copying.

To build without context I can use docker build - < Dockerfile (see docker docs ).

I can mount files via docker run -v <path-to-file-in-host>:<path-to-file-in-container> IMAGE_NAME (as discussed here , and in docker docs here )

From the devcontainer.json reference it's possible to pass build args to Docker build in your devcontainer.json but this doesn't work for the - < as vscode-dev-containers defaults to including build context...

The extension also also automatically runs a container after build so I'm not sure how to override this.

Any tips would be much appreciated!

TL;DR By default dev-containers extension (as of 22/10/2020) bind mounts files from local OS to container so can can open them on local OS.

For faster bind mounting (by skipping Sending build context to docker daemon... during docker build . step) ignore all in .dockerignore & specify bind mount in .devcontainer/devcontainer.json to mount file system to container.

.
├── .devcontainer/devcontainer.json
├── .dockerignore
├── .git
├── .gitignore
├── Dockerfile
├── LICENSE
├── README.md
├── data
├── src
└── tests

.dockerignore

# Ignore everything
**

.devcontainer/devcontainer.json

From Changing or removing the default source code mount

{
    ...
    "workspaceMount": "type=bind,source=${localWorkspaceFolder},target=/workspace,consistency=delegated",
    "workspaceFolder": "/workspace"
    ...
}

(backup-plan) docker cp

Can also use docker cp to copy files in and out of a container; if I have csv or txt files in my container's data folder that I want to view in Excel on my local OS I can run:

docker cp 12890c3a2602:/workspaces/drem/data/commercial_building_benchmarks comm_bldg_bmarks

Where 12890c3a2602 is my container id, /workspaces/drem/data/commercial_building_benchmarks is the path to my data folder within my container and comm_bldg_bmarks is the name of the destination folder on my local OS. See Copying Files To And From Docker Containers for more info...

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