在开发流程中使用码头 [英] Using docker during the development workflow
问题描述
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
更新:添加了我最后做的答案。
更新: em>使用boot2docker 1.3+添加更多当前答案和图。
这是我最后做的,到目前为止似乎工作,但我仍在挖掘:
#脚本位于bin / run
NS = mycompany
PROJECT = myproject
#kill和删除旧容器(如果存在)
docker kill $ PROJECT
docker rm $ PROJECT
#构建图像
docker标签$ NS / $ PROJECT $ NS / $ PROJECT:old
#构建新图像
docker build -t $ NS / $ PROJECT。
#删除旧图像
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
#用bash替换shell,以便我们可以源文件
RUN rm / bin / sh&& ln -s / bin / bash / bin / sh
#设置debconf以非交互方式运行
RUN echo'debconf debconf / frontend select Noninteractive'| debconf-set-choices
#安装基础依赖项
RUN apt-get update&& apt-get install -y -q --no-install-recommendations \
build-essential \
ca-certificates \
curl \
git \
libssl-dev \
python \
rsync \
软件属性常见\
wget \
&& rm -rf / var / lib / apt / lists / *
ENV NVM_DIR / usr / local / nvm
ENV NODE_VERSION 0.10.33
#安装nvm节点和npm
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash \
&&来源$ NVM_DIR / nvm.sh \
&& nvm install $ NODE_VERSION \
&&& nvm别名默认$ NODE_VERSION \
&&& nvm使用默认
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 /运行
:
#!/ usr / bin / env bash
# bin /运行脚本
docker运行--rm --volumes-from myvols myNodeImage \
rsync \
--delete \
--recursive \
--safe-links \
--exclude .git --exclude node_modules \
/ data / myProject / / home / myProject
fig up
我还有一个 bin / install
执行 node_modules
依赖关系安装的脚本。这假设我已经在我的主机上安装了npm,以便任何私有软件包都可以工作。另外,这对npm链接非常有用,您只需要从 / home / linkedProject
中的符号链接到 $ NODE_PATH / linkedProject
在您的容器中。
#!/ usr / bin / env bash
#这是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
所以,把这一切放在一起,这里是按顺序的步骤:
-
创建我的数据卷容器:
docker run -v $ HOME / data:/ data:ro \
-v / home \
-v / path / to / NODE_PATH \
--name myvols myNodeImage echo创建我的卷 -
运行我的安装脚本:
cd〜/ data / myProject&& ; ./bin/install
-
运行我的运行脚本:
nodemon -x bin / run
I'm using boot2docker on OS X and cloned the following repo:
https://github.com/enokd/docker-node-hello
It basically has a Dockerfile and a very simple express app that prints hello world. Everything runs great when I build and run the image, but of course if I make any changes to index.js on my Mac none of that gets reflected in my running image. I can't seem to find any references on how I'd setup docker so that I can run it in my development environment to automatically pick up my source code changes so I feel like I'm "Doing it wrong". Any suggestions?
Here's how I'm currently running it (I'm not using Vagrant, and not quite sure if that makes any difference):
$ docker build -t gasi/centos-node-hello .
$ docker run -p 49160:8080 -d gasi/centos-node-hello
$ curl localhost:49160
Update: Added an answer with what I ended up doing.
Update: Added more current answer using boot2docker 1.3+ and fig.
This is what I ended up doing, so far seems to work but I'm still digging into it:
# 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
In my project root, I simply run:
nodemon -x bin/run
Credit goes to this source.
Update for docker 1.3 and fig
Fig is great, it really took a lot of the complexity out of the script I had before. In addition, boot2docker now natively supports mounting volumes on Mac OS X using Virtual Box's shared folders. This is what I find works really well for me now:
First, the 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"]
The fig.yml
:
app:
image: myNodeImage
working_dir: /home/myProject
volumes_from:
- myvols
Here's the new 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:
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
Run my install script:
cd ~/data/myProject && ./bin/install
Run my run script:
nodemon -x bin/run
这篇关于在开发流程中使用码头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!