2016-05-16 135 views
0

我有一個部署腳本,用於構建新圖像,停止具有相同圖像名稱的現有容器,然後從這些圖像啓動新的容器。爲什麼docker「--filter ancestor = imageName」找到錯誤的容器?

我這裏使用的答案被形象的名字停止容器:Stopping docker containers by image name - Ubuntu

但該命令停止沒有指定的圖像名稱容器。我究竟做錯了什麼?

看到這裏觀看碼頭工人停止錯誤的容器:

enter image description here

這裏是dockerfile:

FROM ubuntu:14.04 
MAINTAINER [email protected] 

# 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" 
+0

可以顯示Dockerfile用於這些容器,尤其是'FROM'線? – user2915097

回答

0

根據the docs--filter ancestor可能會發現錯誤的容器,如果他們以任何方式是其他容器的子項。

所以要確保我的圖片是從我加入這一行我dockerfile開始啓動獨立權,FROM和維護者命令後:

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

然後在我的構建腳本複製dockerfile後到分發文件夾我用適當的環境替換DEVTESTLIVE:

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

這似乎工作;我現在有三個同時運行的環境的容器,可以通過它們的圖像名稱自動啓動和停止它們。

0

爲什麼不使用容器名稱來區分你的環境?

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 

然後使用泊塢窗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 
相關問題