简体   繁体   English

使用 docker 开发节点的工作流程

[英]workflow for node developing with docker

I'm developing a webapp and I need node for my development environment.我正在开发一个 webapp,我的开发环境需要节点。

I don't want a docker production container, but a development one: I need to share files between docker container and local development machines.我不想要一个 docker 生产容器,而是一个开发容器:我需要在 docker 容器和本地开发机器之间共享文件。 I don't want to run docker each time I change a source file.我不想每次更改源文件时都运行 docker。

Currently my dockerfile is:目前我的 dockerfile 是:

#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

I need directory to put all my source files (share a directory via data volume).我需要目录来放置我所有的源文件(通过数据卷共享一个目录)。 I also need to execute npm install in my dockerfile so I get my node_modules directory inside my sources directory (/src/node_modules).我还需要在我的 dockerfile 中执行 npm install 以便我在我的源目录 (/src/node_modules) 中获取我的 node_modules 目录。

However when I mount a host directory as a data volume, as /src dir already exists inside the container's image, its contents will be replaced by the contents of /src directory on the host so I don't have my /src/node_modules directory anymore:但是,当我将主机目录挂载为数据卷时,由于 /src 目录已存在于容器映像中,其内容将被主机上 /src 目录的内容替换,因此我没有 /src/node_modules 目录不再:

docker run -it  --volumes-from data-container --name node-dev user/dev-node /bin/bash

My host directory doesn't have node_modules directory because I get it through github and is not sync because it's quite a heavy dir.我的主机目录没有 node_modules 目录,因为我是通过 github 获取的,并且没有同步,因为它是一个非常重的目录。

My solution is to copy node_modules directory using an ENTRYPOINT directive.我的解决方案是使用 ENTRYPOINT 指令复制 node_modules 目录。

docker-entrypoint.sh: docker-entrypoint.sh:

#!/bin/bash
if ! [ -d node_modules ]; then
        cp -a /tmp/node_modules /src/
fi
exec "$@"

2 lines added to dockerfile: 2 行添加到 dockerfile:

COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

Disclaimer: the following has nothing to do with Docker specifically.免责声明:以下内容与 Docker 无关。

I use SSHFS.我使用 SSHFS。 The Ubuntu Wiki has a pretty good description: Ubuntu Wiki 有一个很好的描述:

SSHFS is a tool that uses SSH to enable mounting of a remote filesystem on a local machine; SSHFS 是一个工具,它使用 SSH 在本地机器上启用远程文件系统的挂载; the network is (mostly) transparent to the user.网络(大部分)对用户透明。

Not sure if this is useful in your scenario, but all my hosts run a SSH server anyways so it was a no-brainer for me.不确定这在您的场景中是否有用,但无论如何我所有的主机都运行 SSH 服务器,所以这对我来说是轻而易举的。 The win-sshfs project doesn't seem to be actively developed anymore, but it still runs fine in win 8/10 (though the setup is a little weird). win-sshfs项目似乎不再积极开发,但它在 win 8/10 中仍然运行良好(尽管设置有点奇怪)。 OS X and Linux both have better support for this through FUSE. OS X 和 Linux 都通过 FUSE 对此提供了更好的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM