【问题标题】:Why does docker "--filter ancestor=imageName" find the wrong container?为什么 docker "--filter original=imageName" 会找到错误的容器?
【发布时间】:2016-09-12 07:44:14
【问题描述】:

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

我使用此处的答案按图像名称停止容器:Stopping docker containers by image name - Ubuntu

但是这个命令会停止没有指定镜像名称的容器。我做错了什么?

看这里看 docker 停止错误的容器:

这里是 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"]

我是这样构建的:

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"

【问题讨论】:

  • 您能否展示用于这些容器的 Dockerfile,尤其是 FROM 行?

标签: docker


【解决方案1】:

为什么不使用容器名称来区分您的环境?

docker run -d --rm --name nginx-dev nginx
40ca9a6db09afd78e8e76e690898ed6ba2b656f777b84e7462f4af8cb4a0b17d
docker run -d --rm --name nginx-qa nginx
347b32c85547d845032cbfa67bbba64db8629798d862ed692972f999a5ff1b6b
docker run -d --rm --name nginx nginx
3bd84b6057b8d5480082939215fed304e65eeac474b2ca12acedeca525117c36

然后使用docker ps

docker ps -f name=nginx$
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3bd84b6057b8        nginx               "nginx -g 'daemon ..."   30 seconds ago      Up 28 seconds       80/tcp, 443/tcp     nginx

【讨论】:

    【解决方案2】:

    根据the docs --filter ancestor 可能会找到错误的容器,如果它们是其他容器的子容器。

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

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

    然后在我的构建脚本中,将 dockerfile 复制到分发文件夹后,我将 DEVESTLIVE 替换为适当的环境:

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

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

    【讨论】:

      猜你喜欢
      • 2021-06-10
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      相关资源
      最近更新 更多