Docker:挂载卷覆盖容器 [英] Docker: mounting a volume overwrites container

查看:501
本文介绍了Docker:挂载卷覆盖容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写 Dockerfile 用作标准化开发环境。

I'm trying to write a Dockerfile for use as a standardized development environment.

Dokerfile:

Dokerfile:

FROM node:9.5.0

WORKDIR /home/project

# install some stuff...

# clone the repo
RUN git clone https://github.com/... . 

# expose 
VOLUME /home/project

# fetch latest changes from git, install them and fire up a shell.
CMD git pull && npm install && /bin/bash

构建映像后(例如,标记为 dev ),
运行 docker run -it dev 给了我一个安装了最新项目的Shell。

After building the image (say with tag dev), Running docker run -it dev gives me a shell with the project installed and up-to-date.

当然,我在容器中所做的更改只是短暂的,因此我想将 / home / project 挂载在主机OS上,我转到主机上的目录 project 并运行:

Of course changes I make in the container are ephemeral so I want to mount /home/project on the host OS, I go to an empty directory project on the host and run:

docker run -it -v $ {pwd}:/ home / project dev

但是我的项目文件夹被覆盖,并且在

But my project folder gets overwritten and is empty inside the container.

如何安装卷以使容器写入主机文件夹而不是相反?

How can mount the volume such that the container writes to the host folder and not the opposite?

操作系统:Windows 10 Pro。

OS: Windows 10 Pro.

推荐答案

安装卷时,读/写默认情况下都是双向的。

When you mount a volume, read/write both are bi-directional by default.

这意味着,您在容器中编写的任何内容将显示在主机目录a中

That means, any thing you write in container will show up in host directory and vice versa.

我将尝试告诉您这里发生了什么。

I will try to tell you whats happening here.

在构建过程中,您正在克隆git存储库。在构建过程中,不会挂载卷。 Git数据直接附加在您的docker中。

In your build process, you are cloning git repository. During build process, volume do not mount. Git data directly are attached in your docker.

现在,当您运行docker容器时,您正在安装卷。容器中的安装路径将与源路径同步。这意味着容器目录将被主机目录覆盖。

Now, when you are running docker container, you are mounting volume. Mount path in your container will be synced with your source path. That means container directory will be over-written with host directory.

这就是为什么您的git数据丢失的原因。

Thats why your git data has been lost.

可能的解决方案:

以CMD运行脚本。该脚本将克隆git repo并执行其他操作。

Run a script as CMD. That script will clone git repo and will do other things.

文件run.sh

#!/bin/bash

# clone the repo
RUN git clone https://github.com/... . 

在Dockerfile中

In Dockerfile

RUN ADD run.sh /bin

# run run.sh, install them and fire up a shell.
CMD run.sh && npm install && /bin/bash

这篇关于Docker:挂载卷覆盖容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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