Skip to main content

Command Palette

Search for a command to run...

Docker Part 2 - Images and Containers - The core of Docker

Understand Docker Images and Containers in the simplest way - with clear examples, easy analogies and beginner-friendly hands-on commands

Published
7 min read

Welcome back!

In Part 1, we learned what Docker is and how its architecture works.
Now, it's time to explore the Core of Docker: Images and Containers.

Docker Images and Containers are the heart of everything you do in Docker.
If you understand these two clearly, the entire Docker concepts will become easy.

What are Docker Images and Containers

To understand Docker properly, you need to clearly grasp these two terms: Images and Containers.
They are the core of everything you do in Docker.

Let's explain them with a simple real-world example.

Imagine you have a movie.
A movie contains everything inside it:

  • video

  • audio

  • subtitles

  • effects

  • background music

  • editing

  • the full final production

This finished movie file is complete and unchanging.

This is exactly like a Docker Image. It is a fixed, packaged template with everything needed inside it.

Now think about how you watch the movie.
You can play the exact same movie on:

  • your laptop

  • your friend’s laptop

  • your TV

  • your mobile phone

Each device plays the same movie without any changes.
When you play the movie on any device, that “playing session” is temporary and can be stopped at any time.

This is exactly like a Docker Container. A Container is a running instance created from the Docker Image.

What is a Docker Image

  • A Docker Image is a packaged template that includes:

    • Your application (e.g., a Java or Python app)

    • Necessary software (e.g., JRE or Python installed)

    • Libraries and dependencies (e.g., Spring Boot libraries or Python packages like Flask)

    • Configuration files (e.g., application.properties or config.json)

    • Environment settings (e.g., port numbers or environment variables)

Now that you know what a Docker Image is, let’s look at what happens when that image actually starts running — this is where Docker Container comes in.

What is Docker Container

A Docker Container is simply a running instance of a Docker Image.

A Docker Container has these properties:

  • Temporary: You can start, stop, and delete containers at any time.

  • Lightweight: No full OS is needed; it starts in seconds.

  • Portable: Runs the same on any system (Windows, Mac, Linux, Cloud).

  • Isolated: Whatever happens inside doesn't affect your main system.

  • Repeatable: You can create unlimited containers from the same image.

you can create multiple containers from one image — just like playing the same movie on different devices at the same time.

Now, let’s understand this even better with a simple real-world example.

Let’s say you have a Docker Image of a Spring Boot application.

when you run:

docker run myapp
  • Docker creates a Container, which:

    • Starts your application

    • Runs it in an isolated environment

    • Behaves exactly the same everywhere

If that container crashes or you stop it, the image is still safe — you can start a new container anytime.

Docker Image Vs Docker Container

Docker ImageDocker Container
A read-only templateA running instance of an image
Contains your application, dependencies, configurations, and environmentTemporary and Lightweight
Cannot run on its ownStarts and stops at any time
Same everywhere (doesn't change)Isolated from your system

How Docker Images are Built

A Docker Image is usually created using a small text file called a Dockerfile.

A Dockerfile tells Docker three things:

  1. Which base image to start with

    • Example: Ubuntu, OpenJDK, Python
  2. What files to copy

    • Example: your application files
  3. What command to run when the container starts

Here's a very simple example:

FROM openjdk:17
COPY myapp.jar /app/myapp.jar
CMD ["java","-jar","/app/myapp.jar"]

This creates an image that contains everything your app needs to run.

Docker Workflow:

Getting Started with Docker: A Beginner's Guide | by Sachin Lakshan |  DevOps.dev

This diagram shows the Docker workflow: Dockerfile → Image → Docker Hub → Container.
Now let’s understand the docker run command.

Understanding Docker run and its Flags

Command to create a container:

docker run -itd --name myfirst-container -p 1234:80 nginx

The command above is used to create a basic image. Let's break it down to understand it better:

docker run is a CLI command used to create and start the container using a Docker image.

-it: stands for interactive terminal. This is useful when the container needs input from the user or when you want to interact with processes running inside the container. It helps the user interact with the bash shell inside the container.

-d: stands for detached mode in Docker. It runs a container in the background, freeing up your terminal so you can continue using it for other tasks. It is used to get the logs from our containers.

--name flag specifies the container name.

-p: is used for publishing the port number to access the applications running inside the container. In the command above, we have 2 ports.

  • 1234 : this represents the host port which is used to access the application through internet. This could be anything like we can give any number instead of 1234.

  • 80 : this is container port which depends on image.

    • For Example if we use httpd image then container port is 80

    • if we use jenkins image then container port is 8080

    • for nexus image the container port is 8081

    • for tomcat image the container port is 8080

    • for sonar image the container port is 9000

nginx : is a webserver image, we can keep our application image here.

When to use ITD

ScenarioFlags to UseWhy?
Running a background service-dKeeps the container running without blocking the terminal. Ex: database and app containers
Debugging or manual interaction-itAllows real-time interaction with the container's shell.
Long-running service with interaction-itdBackground service that you may want to interact with later.
Quick commands or batch jobsNo -i, -t, or -dRuns the container, executes the command, and exits.

Beginner-Friendly Docker commands:

Here are the most important beginner commands:

  • To install docker in Linux : yum install docker -y

  • To see the docker version : docker --version

  • To start the docker service : service docker start

  • To check service is start or not : service docker status

  • To check the docker information : docker info

  • To see all images in local machine : docker images

  • To find images in docker hub : docker search image name

  • To download image from docker hub to local : docker pull image name

  • To give names of a container : docker run -itd --name john -p 1234:80 image

  • To start container : docker start container-name/id

  • To go inside the container : docker attach container-name

  • To see all the details inside container : cat /etc/os-release

  • To get outside of the container : exit

  • To see all containers : docker ps -a

  • To see only running containers : docker ps (ps: process status)

  • To see only exited containers: docker ps -q -f "state=exited"

  • To stop the container : docker stop container name

  • To delete container : docker rm container name

  • To stop all the containers : docker stop $(docker ps -a -q)

  • To delete all the stopped containers : docker rm $(docker ps -a -q)

  • To delete all images : docker rmi -f $(docker images -q)

If you understand Images and Containers, you've already mastered the core of Docker.

That's it for Docker Images and Containers. Now you know what an image is, what a container is, how they work together, and how to run them using different flags.

In the next part, we'll learn about Dockerfile and how to create your own Docker images.

Stay tuned for Docker Part 3!

Docker

Part 2 of 3

In this series, I'll explain Docker in the simplest way possible using clear examples, real-world analogies, and beginner-friendly explanations. If you're new to Docker or DevOps, this series will help you understand the core concepts step-by-step

Up next

Docker Part 1 : Introduction and Architecture

Docker for Beginners - Simple, clear and enjoyable to learn.