将目录绑定到 docker 容器 [英] Bind a directory to a docker container

查看:35
本文介绍了将目录绑定到 docker 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个需要项目目录之外的模块的测试项目.项目文件夹在 docker 中,我想将该模块目录绑定到我项目的 docker 容器中.甚至有可能做到吗?还是我问错了问题?顺便说一句,我还是 docker 新手,所以我只是在尝试.

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.

推荐答案

我的理解是,你需要将宿主文件夹挂载到容器中.所以试试这个:

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

解释

  • -v - --volume=[] 绑定挂载卷
  • /host/project_folder - 主机服务器的文件夹
  • /container/project - 容器的文件夹
  • explanation

    • -v - --volume=[] Bind mount a volume
    • /host/project_folder - host server's folder
    • /container/project - container's folder
    • 最新的 docker 版本 (v1.9.1) 支持新命令 volume.所以你应该更容易在 docker 中管理卷.

      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
      

      这样,您可以随时删除容器 mysql,而不会丢失数据库数据.

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

      这篇关于将目录绑定到 docker 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆