运行前,Docker组合卷安装 [英] Docker-compose volume mount before run

查看:115
本文介绍了运行前,Docker组合卷安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Docker文件,我从一个docker-compose.yml指向。



我想要将docker-compose.yml中的卷挂载到在Docker文件中的 RUN 之前发生。



Dockerfile:

  FROM node 

WORKDIR / usr / src / app

RUN npm install --global gulp-cli \
&& npm安装

ENTRYPOINT gulp手表

docker-compose.yml

 版本:'2'

服务:
build_tools:
build:docker / gulp
volumes_from:
- build_data:rw

build_data:
image:debian:jessie
volumes:
- 。:/ usr / src / app

首先做Dockerfile是完全有道理的,然后从docker-comp但是有一种方法来解决它。



我想保持Dockerfile通用,同时从compose传递更多的特定位。也许这不是最好的做法?

解决方案

Erik Dannenberg的是正确的,音量分层意味着我所试图做的没有任何意义。 (如果您想阅读更多内容,请 Docker网站另外提供了一个非常好的解释)。如果我想让Docker做 npm安装,那么我可以这样做:

  FROM node 

添加。 / usr / src / app
WORKDIR / usr / src / app

RUN npm install --global gulp-cli \
&& npm安装

CMD [gulp,watch]

,这不适合作为我的情况的解决方案。目标是使用NPM来安装项目依赖关系,然后运行gulp来构建我的项目。这意味着我需要读取和写入项目文件夹后,需要在容器停止后继续存在。






我需要做两件事情,在卷被挂载后,所以我想出了以下解决方案...



docker / gulp / Dockerfile:

  FROM node 

RUN npm install --global gulp-cli

ADD start-gulp.sh。

CMD ./start-gulp.sh

docker / gulp / start -gulp.sh:

 #!/ usr / bin / env bash 

直到cd / usr / src / app&&& npm安装
do
echo重试npm安装
done
gulp watch

docker-compose.yml:

  version:'2'

服务:
build_tools:
build:docker / gulp
volumes_from:
- build_data:rw

build_data:
image:debian:jessie
卷:
- 。:/ usr / src / app

所以现在,容器启动一个bash脚本,它会持续循环,直到它进入目录运行 npm install 。这还是很脆弱的,但它的作品。 :)


I have a Dockerfile I'm pointing at from a docker-compose.yml.

I'd like the volume mount in the docker-compose.yml to happen before the RUN in the Dockerfile.

Dockerfile:

FROM node

WORKDIR /usr/src/app

RUN npm install --global gulp-cli \
 && npm install

ENTRYPOINT gulp watch

docker-compose.yml

version: '2'

services:
  build_tools:
    build: docker/gulp
    volumes_from:
      - build_data:rw

  build_data:
    image: debian:jessie
    volumes:
      - .:/usr/src/app

It makes complete sense for it to do the Dockerfile first, then mount from docker-compose, however is there a way to get around it.

I want to keep the Dockerfile generic, while passing more specific bits in from compose. Perhaps that's not the best practice?

解决方案

Erik Dannenberg's is correct, the volume layering means that what I was trying to do makes no sense. (There is another really good explaination on the Docker website if you want to read more). If I want to have Docker do the npm install then I could do it like this:

FROM node

ADD . /usr/src/app
WORKDIR /usr/src/app

RUN npm install --global gulp-cli \
 && npm install

CMD ["gulp", "watch"]

However, this isn't appropriate as a solution for my situation. The goal is to use NPM to install project dependencies, then run gulp to build my project. This means I need read and write access to the project folder and it needs to persist after the container is gone.


I need to do two things after the volume is mounted, so I came up with the following solution...

docker/gulp/Dockerfile:

FROM node

RUN npm install --global gulp-cli

ADD start-gulp.sh .

CMD ./start-gulp.sh

docker/gulp/start-gulp.sh:

#!/usr/bin/env bash

until cd /usr/src/app && npm install
do
    echo "Retrying npm install"
done
gulp watch

docker-compose.yml:

version: '2'

services:
  build_tools:
    build: docker/gulp
    volumes_from:
      - build_data:rw

  build_data:
    image: debian:jessie
    volumes:
      - .:/usr/src/app

So now the container starts a bash script that will continuously loop until it can get into the directory and run npm install. This is still quite brittle, but it works. :)

这篇关于运行前,Docker组合卷安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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