Docker Community Forums

Share and learn in the Docker community.

Unable to destroy containers with grep


(Griffuth) #1

Hello,

So I’m working on a script which helps managing the Docker dev environment to make it easier to use for the developers. I already got some of the apps running, but I’m having trouble trying to run the “dockerdestroy” part of the script with Gearman workers.
I had no trouble with this script when I was using generic docker images before trying to add Gearman workers. That’s why I think it may come from the “Workerfile”

Here’s the part of the script not working :

dockerdestroy() {
	sudo docker stop $(sudo docker ps -a | grep 'igor'); sleep 3 && sudo docker rm $(sudo docker ps -a | grep 'igor')
}

Error message I’m getting while trying to use it :

➜  vagrant-puppet git:(docker) ✗ ./igor.sh dockerdestroy
Destroying containers... please use [init] or [dockercreate] to recreate
unknown shorthand flag: 'c' in -c
See 'docker stop --help'.
unknown shorthand flag: 'c' in -c
See 'docker rm --help'.

The ‘grep’ thing is here to focus on the current project and not destroy other containers. The compose file is generated by a PHP script using the chosen config from a config.yaml, and contains the prefix ‘igor’ for all the containers.

Here’s my docker ps for one of these workers, I can see the -c flag there but I don’t understand the issue, it should look for “igor” and delete containers with this word :

CONTAINER ID        IMAGE                                                     COMMAND                  CREATED             STATUS              PORTS                              NAMES
50355b7b9b75        vagrantpuppet_dev_worker_local__cache_ad_stats_0          "/bin/sh -c '['/bin/…"   19 minutes ago      Created                                                igor_dev_worker_local__cache_ad_stats_0

My “Workerfile”

FROM debian:stretch

# add repository for php7.2
RUN apt update
RUN apt install -y apt-transport-https lsb-release ca-certificates wget > /dev/null
RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list

RUN apt update

# install
RUN apt install -y curl \
	supervisor \
	php7.2 \
        libevent-dev \
        libgearman-dev \
        php-gearman \
        php-memcached > /dev/null && apt clean

# Configure services
ADD start.sh /start.sh
RUN chmod 755 /start.sh

# Run container
CMD ['/bin/bash', '/start/sh']

And his start.sh :

#!/bin/bash

__run_supervisor() {
    echo "Running the run_supervisor function."
    supervisord -n
}

# Call all functions
__run_supervisor

Docker version : Docker version 17.12.0-ce, build c97c6d6
Docker-compose version : docker-compose version 1.19.0-rc3, build cdd0282

Thanks for the help :hugs:


(Sam) #2

you are passing too much info to the docker commands, they want JUST the container_id, not the whole line from docker ps -a

so, add | awk ‘{print $1 }’ to the end of the stmt, like this

$(sudo docker ps -a | grep 'igor' | awk '{print $1}')

(Griffuth) #3

Solved ! Thanks !

Have a good day :slight_smile: