Docker - accessing files inside container from host

喜你入骨 提交于 2019-12-24 06:33:45

问题


I am new to docker.

I ran a node-10 images and inside the running container I cloned a repository, ran the app which started a server with file watcher. I need to access the codebase inside the container, open it up in an IDE running on the windows host. If that is done, then I also want that as I change the files in the IDE these changes induce the filewatcher in the container.

Any help is appreciated. Thanks,


回答1:


The concept you are looking for is called volumes. You need to start a container and mount a host directory inside it. For the container, it will be a regular folder, and it will create files in it. For you, it will also be a regular folder. Changes made by either side will be visible to another.

docker run -v /a/local/dir:/a/dir/in/your/container

Note though that you can run into permission issues that you will need to figure out separately.




回答2:


Hi I think you should use mount volumes for the source code and edit your code from your IDE normally:

docker run -it -v "$PWD":/app -w /app -u node node:10 yarn dev

here docker will create an image setting the working dir to "/app", mount the current dir to "/app" and run "yarn dev" at start up with the "node" user (none root user)

Hope this is helpfull.




回答3:


It depends on what you want to do with the files.

There is the docker cp command that you can use to copy files to/from a container.

However, it sounds to me like you are using docker for development, so you should mount a volume instead, that is, you mount a directory on the host as a volume in docker, so anything written to that directory will show up in the container, and vice versa.

For instance if you have the code base that you develop against in C:\src on your windows machine, then you run docker like docker run -v c:\src:/app where /app is the location that node is looking in. However, for Windows there are a few things to consider since Docker is not native in Windows, so have a look at the documentation first.



来源:https://stackoverflow.com/questions/52856353/docker-accessing-files-inside-container-from-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!