Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker Lab & Assignment – Creating an Image

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

  1. Introduction
  2. Lab 1: Creating Docker Images Using a Container (commit/export/import)
  3. Lab 2: Creating Docker Images Using a Dockerfile (build)
  4. 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

  1. 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
  2. Commit the container as an image:
    docker commit ub-test ub-git-apache:manual
  3. Save and Load the image:
    docker save -o ub-git-apache.tar ub-git-apache:manual
    docker load -i ub-git-apache.tar
  4. Export/Import container as image:
    docker export ub-test > ub-test.tar cat ub-test.tar
    docker import - ub-git-apache:imported
  5. Build image via Dockerfile:
    • Create Dockerfile as shown above.
    • Build: docker build -t ub-git-apache:dockerfile .
  6. 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
  7. 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

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x