Here’s a detailed Tutorial and Lab Guide for the listed Docker commands, focused on practical usage, explanations, and step-by-step labs with Ubuntu + git + apache2 as the image base. The guide is split into two labs as you requested, plus explanations and examples for every command.
Docker Image and Image Management: Tutorial & Lab Guide
Table of Contents
- Introduction
- Lab 1: Creating Docker Images Using a Container (commit/export/import)
- Lab 2: Creating Docker Images Using a Dockerfile (build)
- Image Management Commands: Tutorial & Examples
- images
- history
- inspect
- rmi
- tag
- export
- import
- save
- load
- commit
Introduction
Docker images are the blueprint for containers, holding everything needed to run an application—code, dependencies, and runtime. Managing images efficiently is critical for any container workflow. This guide will help you understand not only how to build images, but also how to manage, tag, save, and load them.
Lab 1: Creating Docker Images Using a Container
Objective
Create a custom Docker image based on Ubuntu by launching a container, installing git and apache2, and committing the container’s state as a new image.
Steps
1. Pull the Ubuntu base image
docker pull ubuntu:latest
Code language: CSS (css)
2. Run a container interactively
docker run -it --name myubuntu ubuntu:latest
Code language: CSS (css)
3. Install git and apache2 inside the container
Inside the container shell:
apt-get update
apt-get install -y git apache2
exit
Code language: JavaScript (javascript)
4. Commit the container to a new image
docker commit myubuntu ubuntu-git-apache2:v1
Code language: CSS (css)
5. Verify the image
docker images
You should see ubuntu-git-apache2
listed.
6. Tag your image for clarity
docker tag ubuntu-git-apache2:v1 myrepo/ubuntu-git-apache2:lab1
7. (Optional) Run a container from your new image
docker run -it myrepo/ubuntu-git-apache2:lab1 bash
Lab 2: Creating Docker Images Using a Dockerfile
Objective
Create a Docker image using a Dockerfile that sets up Ubuntu with git and apache2.
Steps
1. Create a new directory for your Dockerfile
mkdir dockerfile-lab
cd dockerfile-lab
2. Create the Dockerfile
# Dockerfile
FROM ubuntu:latest
RUN apt-get update && \
apt-get install -y git apache2 && \
apt-get clean
CMD ["/bin/bash"]
Code language: CSS (css)
Save this as Dockerfile
.
3. Build the image
docker build -t ubuntu-git-apache2:dockerfile .
Code language: CSS (css)
4. List the images
docker images
5. Run the new image
docker run -it ubuntu-git-apache2:dockerfile
Code language: CSS (css)
Image and Container Management Commands: Explanations & Examples
Below are the required commands, with clear explanations and hands-on examples using the images created in Labs 1 and 2.
1. images — List Images
Usage:
docker images
What it does:
Lists all Docker images present locally.
2. history — Show Image History
Usage:
docker history ubuntu-git-apache2:dockerfile
Code language: CSS (css)
What it does:
Displays the history of an image—each layer and its command.
3. inspect — Inspect Docker Object
Usage:
docker inspect ubuntu-git-apache2:dockerfile
Code language: CSS (css)
What it does:
Gives detailed low-level JSON information about an image or container.
4. rmi — Remove Image(s)
Usage:
docker rmi ubuntu-git-apache2:dockerfile
Code language: CSS (css)
What it does:
Removes a specified Docker image. (Stop running containers first.)
5. tag — Tag Images
Usage:
docker tag ubuntu-git-apache2:dockerfile myrepo/ubuntu-git-apache2:prod
What it does:
Creates a new tag pointing to the same image (useful for versioning).
6. export — Export Container Filesystem
Usage:
docker export myubuntu > myubuntu.tar
Code language: JavaScript (javascript)
What it does:
Exports the filesystem of a container as a tar archive (no history/layers).
7. import — Import a Tarball as Image
Usage:
cat myubuntu.tar | docker import - myimported/ubuntu-git-apache2:v2
Code language: JavaScript (javascript)
What it does:
Creates an image from a tarball (reverse of export
, but history lost).
8. save — Save Image(s) to Tar Archive
Usage:
docker save -o ubuntu-git-apache2-lab2.tar ubuntu-git-apache2:dockerfile
Code language: CSS (css)
What it does:
Saves one or more images to a tar archive (preserves history/layers).
9. load — Load Image(s) from Tar Archive
Usage:
docker load -i ubuntu-git-apache2-lab2.tar
Code language: CSS (css)
What it does:
Loads an image (including all tags and layers) from a tar archive created with docker save
.
10. commit — Create Image from Container
Usage:
docker commit myubuntu myrepo/ubuntu-git-apache2:manual
What it does:
Creates a new image from a container’s current state.
Full Example: End-to-End Workflow
- Start with Ubuntu, install software in a container:
docker run -it --name ub-test ubuntu
apt-get update && apt-get install -y git apache2
exit
- Commit the container as an image:
docker commit ub-test ub-git-apache:manual
- Save and Load the image:
docker save -o ub-git-apache.tar ub-git-apache:manual
docker load -i ub-git-apache.tar
- Export/Import container as image:
docker export ub-test > ub-test.tar cat ub-test.tar
docker import - ub-git-apache:imported
- Build image via Dockerfile:
- Create Dockerfile as shown above.
- Build:
docker build -t ub-git-apache:dockerfile .
- Tag, Inspect, Remove:
docker tag ub-git-apache:dockerfile myrepo/ub-git-apache:prod
docker inspect myrepo/ub-git-apache:prod
docker rmi myrepo/ub-git-apache:prod
- List Images, View History:
docker images docker history ub-git-apache:dockerfile
Conclusion
You now have hands-on experience in:
- Building Docker images both from containers and Dockerfiles.
- Managing, saving, loading, and distributing Docker images.
- Using all essential Docker image management commands with practical Ubuntu+git+apache2 examples.
Reference
- https://www.devopsschool.com/blog/example-and-sample-programs-of-dockerfile/
- https://www.devopsschool.com/blog/understanding-dockerfiles-instructions-options-of-docker/
- https://www.devopsschool.com/blog/dockerfile-lab-exercise-1/
- https://www.devopsschool.com/blog/how-to-create-a-image-using-dockerfile/

















I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at WIZBRAND