Skip to content

Difference between Docker Image and Container

Docker builds images and runs containers by using the docker engine on the host machine. Docker containers consist of all the dependencies and software needed to run an application in different environments.

What is Docker Image ?

The concept of Image and Container is like class and object, in which an object is an instance of a class, and a class is the blueprint of the object. Images are different in Virtual Machines and dockers. In virtual machines, images are just snapshots of running virtual machines at different points in time, but Docker images are a little bit different. The most important and major difference is that Docker images are immutable. That is they can not be changed. In the real world, it happens a lot that software works on one computer but it does not work on others due to different environments. This issue is completely solved by docker images and using this, the application will work the same on everyone’s PC. Every developer on a team will have the exact same development instance. Each testing instance is exactly the same as the development instance. Your production instance is exactly the same as the testing instance. Also, developers around the world can share their Docker images on a platform called DockerHUB.

What is Docker Container ?

They are actually Docker Virtual Machines but are commonly called Docker Containers. If a Docker image is a map of the house, then a Docker container is an actually built house, or in other words, we can call it an instance of an image. As per the official website, a container is a runnable instance of an image. You can create, start, stop, move, or delete a container using Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state. An application runs using a cluster of containers that are self-isolated from one another and also from the host machine where they are running.

Example: If a backend application is running on a Docker container at port 8000 and you tried to access it from the host machine, you will not be able to access it, as containers are self-isolated and in that case, you have to explicitly expose your application at a certain port and connect your machine port to that port.

Run Docker Container

docker run --publish 8000:8080 --detach --name alias_name application_name:1.0 

Here an application running at port 8080 in a container is connected to port 8000 at the host machine. Now the host can access the application using URL localhost:8000

Difference between Docker Images and Containers

Aspect Docker Image Docker Container
Definition A blueprint of the container An instance of the image
Entity Type Logical entity Real-world entity
Creation Images are created only once Containers can be created any number of times using an image
Immutability Images are immutable. You cannot attach volumes and networks Containers change only if the old image is deleted and replaced with a new one. You can attach volumes, networks
Resource Usage Images do not require computing resources to exist Containers require computing resources to run (use Docker VM resources)
Creation Method Create an image by writing a script in a Dockerfile Create a container by running the command docker run <image>
Purpose Used to package applications and pre-configured server environments Use server info and filesystem provided by an image to operate
Sharing Docker Images can be shared on Docker Hub Sharing containers does not make sense; only images are shared
Running State Docker Images do not have a running state Containers consume RAM when created and in a running state
Removal Requirement Images can be removed regardless of state Containers must be stopped before they can be removed
Connectivity Cannot connect to images (they are like snapshots) Cannot connect to running containers and execute commands
Layer Type Images have multiple read-only layers Containers have a single writable layer
Existence Images can exist in isolation Containers cannot exist without images

Difference between Docker Container and VM Image

Aspect Docker Container VM Image
Startup Time Docker containers can be started within seconds VM images take minutes to start
Resource Usage Docker container resource usage is very low VM images are resource-intensive
Isolation Level Provides isolation at the OS level Provides isolation at the process level
Dependency Management Manages dependencies at the application level Manages dependencies at the system level