Bind a directory to a docker container

泪湿孤枕 提交于 2019-12-05 13:11:33

问题


I'm building a test project that requires a module outside of the project directory. The project folder is in docker, and I would like to bind that module directory to the docker container of my project. Is it even possible to do it? Or am I asking the wrong question? By the way, I'm still new to docker so I'm just trying things out.


回答1:


My understand is, you need mount the host folder to the container. So try this:

docker run -v /host/project_folder:/container/project -t avian/project_image bash

explanation

  • -v - --volume=[] Bind mount a volume
  • /host/project_folder - host server's folder
  • /container/project - container's folder

Update:

The latest docker version (v1.9.1) support a new command volume. So you should be easier to manage volume in docker.

# For example, I need attach a volume to mysql container.
docker volume create --name mysql-data
docker run --name mysql -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

With that, you can delete container mysql any time, without lost your database data.




回答2:


You can use the -v option to mount a volume (e.g. your folder) into the container. More details can be found in the docs.

E.g., from ghost blog platform's Dockerfile example:

docker run -v /data/ghost:/var/lib/ghost -d ghost

Which maps /data/ghost on the local drive to /var/lib/ghost inside the container.

You can also specify another docker container as the source of the data using --volumes-from option.



来源:https://stackoverflow.com/questions/30183978/bind-a-directory-to-a-docker-container

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