Build a Docker image, push it to Docker hub and clean up local disk space

This blog post does contain the tasks to create a Docker image and upload the image to dockerhub and clean up the image and container on the local machine.

Upload the image

1. Create a local Docker image using docker build

  • Dockerhub account name: tsuedbroecker
  • Dockerhub repository name: cns-workshop-tools
  • Tag: v4
  • Docker image name with tag: tsuedbroecker/cns-workshop-tools:v4
docker build -t "tsuedbroecker/cns-workshop-tools:v4" .

Example: If your Dockerfile has a different name or is in a different folder.

docker build -t "tsuedbroecker/cns-workshop-tools:v4" -f "build/Dockerfile" .

2. Run the Docker image as a container

Using docker run with the interactive terminal mode parameter -it to verify the content of your Docker image you just created.
For example: Check different CLI versions like the IBM Cloud CLI inside the running container of your image.

Container name: demo
Docker image name: tsuedbroecker/cns-workshop-tools:v4

docker run -it --name demo "tsuedbroecker/cns-workshop-tools:v4" /bin/bash

Here an example to verify a installed IBM Cloud CLI:

root@dbe62179b56a:/tmp# ibmcloud version 
 ibmcloud version 1.2.3+3577aee6-2020-09-25T14:34:09+00:00
 root@dbe62179b56a:/tmp# 

Note: Maybe you also want to mount a path from your local machine

docker run -it --privileged --name demo -v "$(pwd)":/localmachine "tsuedbroecker/cns-workshop-tools:v4" /bin/bash

3. Login to container registry

In our case the container registry is Dockerhub (other container registry example: docker login quay.io)

docker login

4. Push the Docker image to Dockerhub

docker push tsuedbroecker/cns-workshop-tools:v4

5. Verify at the image was uploaded to Dockerhub

Clean up

1. Show the existing running Docker containers

docker ps --all

Example output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbe62179b56a tsuedbroecker/cns-workshop-tools:v4 "/bin/bash" 15 minutes ago Exited (127) 12 minutes ago demo

Note: Stop all running container docker kill $(docker ps -a -q)

2. Delete a running container

Copy the CONTAINER ID and delete the running container with docker rm

docker rm dbe62179b56a

Example output

dbe62179b56a

Note: Remove all running container docker rm $(docker ps -a -q)

3. List the existing container images

docker image list

Example output:

REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
 tsuedbroecker/cns-workshop-tools   v4                  ff088202f1dc        31 minutes ago      925MB
 ubuntu                             latest              d70eaf7277ea        5 days ago          72.9MB
                                            c18da3b7a242        5 months ago        206MB

4. Remove the container image

Copy the image repository name and tag and delete the image with docker image rm.

  • Image name: tsuedbroecker/cns-workshop-tools
  • Tag: v4
docker image rm tsuedbroecker/cns-workshop-tools:v4

Note: You can delete all images with docker image prune -a

docker system prune --all --force && docker rmi --all

I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas

#Container, #Docker, #podman

2 thoughts on “Build a Docker image, push it to Docker hub and clean up local disk space

Add yours

  1. Hi thomas, thanks for this post! In this way is possible to save local disk space by moving all images in docker hub? I’m having problem with all docker images that even if they’re not running, are growing in usage disk space!
    Thanks a lot

    Like

    1. Hi Daniele, typically, you delete all container images that you don’t use on your computer and only upload container images that you need or want to share in a container registry. It’s really very unusual what you’re about to do, but nobody can stop you from doing it. Maybe you can take a look again in the options in the CLEAN UP section of the blog post or just search for delete local container images. I hope this helps a bit maybe I don’t get what you are saying. Greetings Thomas

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: