1

I have a docker image, and I am running it now (finishing with bash) When I do, I have a file structure inside the container.

However, this is not some file structure mapped (with -v) from outside the container. These files and folders exist only inside the container.

My question is, since it is bothersome to be opening each file with vi and navigating from the terminal, is there a way that I can open vscode on these files?

Be aware that these files do not exist outside the container

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • Remember that a container is just a wrapper around a process; if the container exits, any files you've edited inside the container will get lost. It's usually a better practice to edit the Dockerfile so that the container starts up with the correct file content. Your container shouldn't usually need bash. – David Maze May 18 '22 at 10:39
  • If you really need to do this, and are okay with potentially losing your edits, [open a file in docker container with vscode](https://stackoverflow.com/questions/53292611/open-a-file-in-docker-container-with-vscode) seems to have some concrete suggestions. – David Maze May 18 '22 at 10:41

2 Answers2

0

I found how to do it from this link However I used the "attach to running container" command

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
0

I rarely do that but when I have to I usually mount an empty volume to the container, then exec into the container copy the folder which I need into that empty volume, which is then replicated on my host machine. From my host machine I then open it in vscode.

However please be careful if you have sensitive information in that container, not to expose something by accident.

So the steps are:

  • Create empty volume ( docker-compose example ) Note do not overwrite the folder/file which you want to extract. containerpath is path which does not exist in the container prior to creating it.
volume:
  - ./hostpath:/containerpath
  • Find docker id so that you can use it to exec into it:
docker ps
  • Exec into the container:
docker exec -it <container_id> /bin/sh
  • Copy the file/folder to that empty volume:
cp -r folder containerpath

Exit the container and look at your files in ./hostpath folder.

Johnny9
  • 436
  • 5
  • 8