使用 docker 进行节点开发的工作流程
workflow for node developing with docker
我正在开发一个 webapp,我的开发环境需要 node。
我不想要 docker 生产容器,而是开发容器:我需要在 docker 容器和本地开发机器之间共享文件。我不想每次更改源文件时 运行 docker。
目前我的docker文件是:
#React development
FROM node:4.1.1-wheezy
MAINTAINER xxxxx
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get -y install sudo locales apt-utils
RUN locale-gen es_ES.UTF-8
RUN dpkg-reconfigure locales
RUN echo Europe/Madrid | sudo tee /etc/timezone && sudo dpkg-reconfigure --frontend noninteractive tzdata
ADD src/package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /src && cp -a /tmp/node_modules /src/
WORKDIR /src
EXPOSE 3000
VOLUME /src
我需要目录来放置我所有的源文件(通过数据卷共享一个目录)。我还需要在我的 docker 文件中执行 npm install,以便在我的源目录 (/src/node_modules) 中获得我的 node_modules 目录。
然而,当我将主机目录挂载为数据卷时,由于 /src 目录已经存在于容器映像中,其内容将被主机上 /src 目录的内容替换,所以我没有我的/src/node_modules 目录:
docker run -it --volumes-from data-container --name node-dev user/dev-node /bin/bash
我的主机目录没有 node_modules 目录,因为我通过 github 获取它并且不同步,因为它是一个很重的目录。
我的解决方案是使用 ENTRYPOINT 指令复制 node_modules 目录。
docker-entrypoint.sh:
#!/bin/bash
if ! [ -d node_modules ]; then
cp -a /tmp/node_modules /src/
fi
exec "$@"
2 行添加到 docker 文件:
COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
免责声明:以下内容与Docker具体无关
我使用 SSHFS。 Ubuntu Wiki 有一个很好的描述:
SSHFS is a tool that uses SSH to enable mounting of a remote
filesystem on a local machine; the network is (mostly) transparent to
the user.
不确定这在您的场景中是否有用,但我所有的主机 运行 无论如何都是 SSH 服务器,所以这对我来说很简单。 win-sshfs
项目似乎不再积极开发,但它在 win 8/10 中仍然 运行 没问题(尽管设置有点奇怪)。 OS X 和 Linux 都通过 FUSE 对此提供了更好的支持。
我正在开发一个 webapp,我的开发环境需要 node。
我不想要 docker 生产容器,而是开发容器:我需要在 docker 容器和本地开发机器之间共享文件。我不想每次更改源文件时 运行 docker。
目前我的docker文件是:
#React development
FROM node:4.1.1-wheezy
MAINTAINER xxxxx
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get -y install sudo locales apt-utils
RUN locale-gen es_ES.UTF-8
RUN dpkg-reconfigure locales
RUN echo Europe/Madrid | sudo tee /etc/timezone && sudo dpkg-reconfigure --frontend noninteractive tzdata
ADD src/package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /src && cp -a /tmp/node_modules /src/
WORKDIR /src
EXPOSE 3000
VOLUME /src
我需要目录来放置我所有的源文件(通过数据卷共享一个目录)。我还需要在我的 docker 文件中执行 npm install,以便在我的源目录 (/src/node_modules) 中获得我的 node_modules 目录。
然而,当我将主机目录挂载为数据卷时,由于 /src 目录已经存在于容器映像中,其内容将被主机上 /src 目录的内容替换,所以我没有我的/src/node_modules 目录:
docker run -it --volumes-from data-container --name node-dev user/dev-node /bin/bash
我的主机目录没有 node_modules 目录,因为我通过 github 获取它并且不同步,因为它是一个很重的目录。
我的解决方案是使用 ENTRYPOINT 指令复制 node_modules 目录。
docker-entrypoint.sh:
#!/bin/bash
if ! [ -d node_modules ]; then
cp -a /tmp/node_modules /src/
fi
exec "$@"
2 行添加到 docker 文件:
COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
免责声明:以下内容与Docker具体无关
我使用 SSHFS。 Ubuntu Wiki 有一个很好的描述:
SSHFS is a tool that uses SSH to enable mounting of a remote filesystem on a local machine; the network is (mostly) transparent to the user.
不确定这在您的场景中是否有用,但我所有的主机 运行 无论如何都是 SSH 服务器,所以这对我来说很简单。 win-sshfs
项目似乎不再积极开发,但它在 win 8/10 中仍然 运行 没问题(尽管设置有点奇怪)。 OS X 和 Linux 都通过 FUSE 对此提供了更好的支持。