为什么docker“ --filter ancestor = imageName”找到错误的容器? [英] Why does docker "--filter ancestor=imageName" find the wrong container?

查看:123
本文介绍了为什么docker“ --filter ancestor = imageName”找到错误的容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部署脚本,该脚本可以构建新映像,停止具有相同映像名称的现有容器,然后从这些映像启动新容器。



我停止该容器按图像名称使用答案,方法如下:



这里是dockerfile:

 从ubuntu:14.04 
MAINTAINER j@eka.com

#设置
ENV NODE_VERSION 5.11.0
ENV NVM_DIR /root/.nvm
ENV NODE_PATH $ NVM_DIR / versions / node / v $ NODE_VERSION / lib / node_modules
ENV PATH $ NVM_DIR / versions / node / v $ NODE_VERSION / bin:$ PATH

#用bash替换shell这样我们就可以获取文件
RUN rm / bin / sh&& ln -s / bin / bash / bin / sh

#安装库
运行apt-get更新
运行apt-get安装curl -y
运行curl https ://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash \
&& chmod + x $ NVM_DIR / nvm.sh \
&&源$ NVM_DIR / nvm.sh \
&& nvm安装$ NODE_VERSION \
&& nvm别名默认$ NODE_VERSION \
&& nvm使用默认
运行apt-get clean

#安装应用程序
运行mkdir / app
COPY ./app / app

#运行应用程序
CMD [ node, /app/src/app.js]

我这样构建:

  docker build -t $ serverImageName。 

,然后像这样开始:

  docker run -d -p 3000: 3000 -e db_name = $ db_name -e db_username = $ db_username -e db_password = $ db_password -e db_host = $ db_host $ serverImageName 


解决方案

根据文档 -过滤祖先如果它们是其他容器的子容器,可能会找到错误的容器。



因此,请确保我的图像从一开始就与我分开,我添加了这一行在FROM和MAINTAINER命令之后,到我的dockerfile的开头:

  RUN echo DEVTESTLIVE:此行确保此容器永远不会被混淆为另一个环境的祖先

然后在将dockerfile复制到分发文件夹后在我的构建脚本中我用适当的替换DEVTESTLIVE恶劣的环境:

  sed -i -e s / DEVTESTLIVE / $ env / g ../dist/server/dockerfile 

这似乎有效;我现在拥有了同时运行的所有三个环境的容器,并且可以通过它们的映像名称自动启动和停止它们。


I have a deployment script that builds new images, stop the existing containers with the same image names, then starts new containers from those images.

I stop the container by image name using the answer here: Stopping docker containers by image name - Ubuntu

But this command stops containers that don't have the specified image name. What am I doing wrong?

See here to watch docker stopping the wrong container:

Here is the dockerfile:

FROM ubuntu:14.04
MAINTAINER j@eka.com

# Settings
ENV NODE_VERSION    5.11.0
ENV NVM_DIR         /root/.nvm
ENV NODE_PATH       $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH           $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

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

# Install libs
RUN apt-get update
RUN apt-get install curl -y
RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash \
     && chmod +x $NVM_DIR/nvm.sh \
    && source $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default
RUN apt-get clean

# Install app
RUN mkdir /app
COPY ./app /app

#Run the app
CMD ["node", "/app/src/app.js"]

I build like so:

docker build -t "$serverImageName" .

and start like so:

docker run -d -p "3000:"3000" -e db_name="$db_name" -e db_username="$db_username"  -e db_password="$db_password"  -e db_host="$db_host" "$serverImageName"

解决方案

According to the docs --filter ancestor could be finding the wrong containers if they are in any way children of other containers.

So to be sure my images are separate right from the start I added this line to the start of my dockerfile, after the FROM and MAINTAINER commands:

RUN echo DEVTESTLIVE: This line ensures that this container will never be confused as an ancestor of another environment

Then in my build scripts after copying the dockerfile to the distribution folder I replace DEVTESTLIVE with the appropriate environment:

sed -i -e "s/DEVTESTLIVE/$env/g" ../dist/server/dockerfile

This seems to have worked; I now have containers for all three environments running simultaneously and can start and stop them automatically through their image names.

这篇关于为什么docker“ --filter ancestor = imageName”找到错误的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆