节点Docker容器 - 缓存容器启动逻辑

时间:2014-12-04 02:34:58

标签: docker dockerfile

我的Docker容器从Git中提取我的Node App并安装所需的依赖项。但是,在初始运行后对Docker Start的后续调用中重新运行此逻辑。有没有办法设置我的Entrypoint脚本只在调用Docker运行时从Git中提取应用程序?我假设我可以在初始设置完成后始终将文件写入容器并在从Git中提取之前检查该文件?是否有更好,更干净的方法来实现这种行为?

Dockerfile:

# Generic Docker Image for Running Node app from Git Repository
FROM    node:0.10.33-slim
ENV NODE_ENV production

# Add script to pull Node app from Git and run the app
COPY docker-node-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

EXPOSE  8080
CMD ["--help"]

入口点脚本:

#!/bin/bash
set -e
# Run the command passed in if it isn't to start a node app
if [ "$1" != 'node-server' ]; then
   exec "$@"
fi
# Logic for pulling the node app and starting it
cd /usr/src
# try to remove the repo if it already exists
rm -rf node-app; true
echo "Pulling Node app's source from $2"
git clone $2 node-app
cd node-app
# Check if we should be running a specific commit from the git repo
if [ ! -z "$3" ]; then
  echo "Changing to commit $3"
  git checkout $3
fi
npm install
echo "Starting the app"
exec node .

1 个答案:

答案 0 :(得分:1)

理想情况下,每个节点项目都有自己的Dockerfile,因此,不要将git clone推迟到docker run时间,而是将容器设置为完全设置并准备运行。

可能你可以为每个包含变体的git repo添加一个Dockerfile

FROM node:onbuild,它会自动默认运行您的节点应用。