Why does docker “--filter ancestor=imageName” find

2019-07-14 07:23发布

问题:

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"

回答1:

Why not use the container name to differentiate you environments?

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

Then use 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:

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