一尘不染

在开发工作流程中使用docker

docker

我在OS X上使用boot2docker并克隆了以下存储库:

https://github.com/enokd/docker-node-hello

它基本上有一个Dockerfile和一个非常简单的Express应用程序,可以打印世界。在构建和运行映像时,一切运行良好,但是,如果我在Mac上对index.js进行了任何更改,这些都不会反映在运行的映像中。我似乎找不到任何有关如何设置docker的参考,以便可以在开发环境中运行它以自动获取源代码更改,因此我感觉自己“做错了”。有什么建议么?

这是我当前正在运行的方式(我没有使用Vagrant,并且不确定是否有什么不同):

$ docker build -t gasi/centos-node-hello .
$ docker run -p 49160:8080 -d gasi/centos-node-hello
$ curl localhost:49160

更新: 添加了我最终所做的答案。

更新: 使用boot2docker 1.3+和图添加了更多当前答案。


阅读 532

收藏
2020-06-17

共1个答案

一尘不染

这是我最终要执行的操作,到目前为止似乎仍然有效,但我仍在研究它:

# script located in bin/run
NS=mycompany
PROJECT=myproject

# kill and remove old container if it exists
docker kill $PROJECT
docker rm $PROJECT

# tag the previously built image
docker tag $NS/$PROJECT $NS/$PROJECT:old

# build the new image
docker build -t $NS/$PROJECT .

# remove the old image
docker rmi $NS/$PROJECT:old

docker run -dP --name=$PROJECT $NS/$PROJECT /sbin/my_init

在我的项目根目录中,我只需运行:

nodemon -x bin/run

信誉归功于这一来源

Docker 1.3和Fig的更新

无花果很棒,它确实消除了我以前编写的脚本的很多复杂性。此外,boot2docker现在本机支持使用Virtual
Box的共享文件夹在Mac OS X上安装卷。这是我发现现在非常适合我的东西:

首先,Dockerfile

FROM ubuntu:14.04

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# Set debconf to run non-interactively
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# Install base dependencies
RUN apt-get update && apt-get install -y -q --no-install-recommends \
        build-essential \
        ca-certificates \
        curl \
        git \
        libssl-dev \
        python \
        rsync \
        software-properties-common \
        wget \
    && rm -rf /var/lib/apt/lists/*

ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 0.10.33

# Install nvm with node and npm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash \
    && source $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/v$NODE_VERSION/bin:$PATH

CMD ["npm", "start"]

fig.yml

app:
    image: myNodeImage
    working_dir: /home/myProject
    volumes_from:
     - myvols

这是新的bin/run

#!/usr/bin/env bash
# This is the the bin/run script

docker run --rm --volumes-from myvols myNodeImage \
    rsync \
        --delete \
        --recursive \
        --safe-links \
        --exclude .git  --exclude node_modules  \
    /data/myProject/ /home/myProject

fig up

I also have a bin/install script that does the node_modules dependency
installs. This assumes I’ve already done an npm install on my host so that any
private packages will work. Also, this works great with npm links, you just
need to make a symlink from your /home/linkedProject into
$NODE_PATH/linkedProject in your container.

#!/usr/bin/env bash
# This is the the bin/install script

docker run --rm --volumes-from myvols myNodeImage \
    rm -rf /home/myProject && \
    rsync \
        --delete \
        --recursive \
        --safe-links \
        --exclude .git \
        /data/myProject/ /home/myProject && \
    cd /home/myProject && \
    npm rebuild

So, to put this all together, here’s the steps in order:

  1. Create my data volume container:

docker run -v $HOME/data:/data:ro \ -v /home \ -v /path/to/NODE_PATH \ --name myvols myNodeImage echo Creating my volumes

  1. Run my install script: cd ~/data/myProject && ./bin/install

  2. Run my run script: nodemon -x bin/run

2020-06-17