简体   繁体   English

我应该如何实现更好的Docker工作流程?

[英]How should I Accomplish a Better Docker Workflow?

Everytime I change a file in the nodejs app I have to rebuild the docker image. 每次我在nodejs应用程序中更改文件时,都必须重建docker镜像。
This feels redundant and slows my workflow. 这感到多余,并且减慢了我的工作流程。 Is there a proper way to sync the nodejs app files without rebuilding the whole image again, or is this a normal usage? 有没有合适的方法来同步nodejs应用程序文件而无需再次重建整个映像,还是正常使用?

It sounds like you want to speed up the development process. 听起来您想加快开发过程。 In that case I would recommend to mount your directory in your container using the docker run -v option: https://docs.docker.com/engine/userguide/dockervolumes/#mount-a-host-directory-as-a-data-volume 在这种情况下,我建议使用docker run -v选项将目录挂载到容器中: https : //docs.docker.com/engine/userguide/dockervolumes/#mount-a-host-directory-as-a-数据量

Once you are done developing your program build the image and now start docker without the -v option. 完成开发程序后,构建映像,然后启动不带-v选项的-v

What I ended up doing was: 我最终要做的是:

1) Using volumes with the docker run command - so I could change the code without rebuilding the docker image every time. 1) 通过 docker run命令使用卷 -因此我可以更改代码而无需每次都重新构建docker镜像。

2) I had an issue with node_modules being overwritten because a volume acts like a mount - fixed it with node's PATH traversal. 2)我有一个问题,因为卷的行为就像是挂载,所以会覆盖node_modules-使用节点的PATH遍历修复了该问题。

Dockerfile: Dockerfile:

FROM node:5.2

# Create our app directories
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN npm install -g nodemon

# This will cache npm install
# And presist the node_modules
# Even after we are using the volume (overwrites)
COPY package.json /usr/src/
RUN cd /usr/src && npm install 

#Expose node's port
EXPOSE 3000

# Run the app
CMD nodemon server.js

Command-line: 命令行:

to build: 建立:

docker build -t web-image

to run: 跑步:

docker run --rm -v $(pwd):/usr/src/app -p 3000:3000 --name web web-image

You could have also done something like change the instruction and it says look in the directory specified by the build context argument of docker build and find the package.json file and then copy that into the current working directory of the container and then RUN npm install and afterwards we will COPY over everything else like so: 您可能还做了类似更改指令的操作,它说在docker build的build context参数指定的目录中查找并找到package.json文件,然后将其复制到容器的当前工作目录中,然后RUN npm install然后我们将COPY其他所有内容,例如:

# Specify base image
FROM node:alpine

WORKDIR /usr/app

# Install some dependencies
COPY ./package.json ./
RUN npm install

# Setup default command
CMD ["npm", "start"]

You can make as many changes as you want and it will not invalidate the cache for any of these steps here. 您可以根据需要进行任意更改,并且此处的任何这些步骤都不会使缓存无效。

The only time that npm install will be executed again is if we make a change to that step or any step above it. 再次执行npm安装的唯一时间是,如果我们对该步骤或其上方的任何步骤进行了更改。

So unless you make a change to the package.json file, the npm install will not be executed again. 因此,除非您对package.json文件进行更改,否则将不会再次执行npm安装。

So we can test this by running the docker build -t <tagname>/<project-name> . 因此,我们可以通过运行docker build -t <tagname>/<project-name> .

Now I have made a change to the Dockerfile so you will see some steps re run and eventually our successfully tagged and built image. 现在,我对Dockerfile进行了更改,因此您将看到重新运行一些步骤,并最终看到我们成功标记并构建的映像。

Docker detected the change to the step and every step after it, but not the npm install step. Docker检测到该步骤及其后每个步骤的更改,但未检测到npm install步骤。

The lesson here is that yes it does make a difference the order in which all these instructions are placed in a Dockerfile . 这里的教训是,是的,这确实对所有这些指令在Dockerfile中的放置顺序有所不同。

Its nice to segment out these operations to ensure you are only copying the bare minimum. 最好将这些操作分段,以确保仅复制最低限度的内容。

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

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