Cleaning up while playing with Dockerfiles

/, Short Notice/Cleaning up while playing with Dockerfiles

Cleaning up while playing with Dockerfiles

As I play around a lot with Docker these days, I recognized how fast one is running out of hard disk space while building and running a lot of Dockerfiles. When one starts with using Dockerfiles there is a lot of trail and error until the expected services are running properly.

In my case, I created a very simple bash script, that I copy into the folder where I create my Dockerfile.

For instance, I have the following setup, where I create and host my Dockerfiles:

seiler@docker:~/dockerfiles$ tree
.
├── datacontainer
│   ├── Dockerfile
│   └── d.sh
├── mysql
│   ├── Dockerfile
│   ├── d.sh
│   ├── starting.sh
│   └── supervisord.conf
├── nginx
│   ├── Dockerfile
│   ├── Dockerfile.old
│   ├── d.sh
│   ├── index.php
│   ├── nginx.conf
│   ├── php.ini
│   ├── startingUp.sh
│   └── supervisord.conf
├── postgresql
│   ├── Dockerfile
│   ├── d.sh
│   ├── starting.sh
│   └── supervisord.conf
└── tomcat
    ├── Dockerfile
    └── d.sh

For each Dockerfile I have a script, called d.sh, that can build and run the docker files for me, while also cleaning all data up. It is important to know, that this script is stopping and deleting the other docker container instances! This makes a lot of sense, when you are tinkering around with specific setting in your Dockerfile but always like to use the same port as before.

The d.sh Script can be called with two parameters:

  1. The name of the Docker Container that will be used while creating the container.
    If no name is given, the container is named „ramdom-
    [a random number]“
  2. The Ports that should be exposed to the outer world (aka host machine).

While the script is running, all other docker containers are stopped and deleted (line 12-13). There is also a parameter to delete the created images (line 14), but this one is currently disabled. Instead of „ubuntu|test“ you can add there other names of images you do not like to remove while running the script.

  1. To stop all currently running docker files, one can call:
    sudo docker stop $(sudo docker ps -a -q) 
  2. To remove all created docker container files, one can call:
    sudo docker rm $(sudo docker ps -a -q)
  3. To remove all created docker images, one can call:
    sudo docker rmi $(sudo docker images | awk '$1!~/ubuntu|test/ && NR>1 {print $3}')

    This command will delete all images, but not „ubuntu“ and „test“. You can add additional ones here.

After the building and running process it will list all currently running docker containers.

#!/bin/bash
if [ -n "${1}" ]; then
 NAME=${1}
else
 NAME=random-$RANDOM
fi
echo "The container is named $NAME"

PORTS=${2}

echo "Cleaning up, this process will take some time"
sudo docker stop $(sudo docker ps -a -q) 2>&1 >/dev/null
sudo docker rm $(sudo docker ps -a -q) 2>&1 >/dev/null
#sudo docker rmi $(sudo docker images | awk '$1!~/ubuntu|test/ && NR>1 {print $3}')


sudo docker build -t $NAME . 2>&1 >/dev/null

if [ -n "$PORTS" ]; then
    echo "Running the container $NAME with the following ports opened: $PORTS"
    sudo docker run -i -t -p $PORTS -d $NAME
else
    echo "Running the container $NAME with no ports opened"
    sudo docker run -i -t -d $NAME
fi
echo "The following docker containers are running currently:"
sudo docker ps

When you call the script without any parameters it will look like:

seiler@docker:~/dockerfiles/nginx$ ./d.sh
The container is named random-24048
Cleaning up, this process will take some time
[sudo] password for seiler:
Uploading context 18.43 kB
Uploading context
Running the container random-24048 with no ports opened
34467782f140284f22f2d29f53f3901193dcae5a8931cb29b5194212a794b8ac
The following docker containers are running currently:
CONTAINER ID        IMAGE                 COMMAND                CREATED                  STATUS                  PORTS               NAMES
34467782f140        random-24048:latest   /bin/bash /startingU   Less than a second ago   Up Less than a second   80/tcp              desperate_galileo

When giving the name and a port:

seiler@docker:~/dockerfiles/nginx$ ./d.sh MyContainer 80:80
The container is named MyContainer
Cleaning up, this process will take some time
Uploading context 18.43 kB
Uploading context
Running the container MyContainer with the following ports opened: 80:80 

When giving just the name:

seiler@docker:~/dockerfiles/nginx$ ./d.sh MyContainer
The container is named MyContainer
Cleaning up, this process will take some time
Uploading context 18.43 kB
Uploading context
Running the container MyContainer with no ports opened

Please use this script at your own risk, as I just wrote, it will delete docker files from your server, when not commenting out!

2019-01-03T16:47:20+00:00Tags: , , |

Share This Story, Choose Your Platform!

Leave A Comment