Things You Should Know About Docker Image Vs Container
Docker is one of the most trending things in the containerized world. Many large companies like Paypal, Spotify, Yelp, eBay, etc. are continuously moving their services to containerization environments, and docker has been the top choice for them.
Docker is a great DevOps tool that developers use to create, deploys, and runs applications in containerized environments without worrying about the platform or environment-related issues. All the application related libraries, dependencies, system tools, code, and runtime are bundled in a small shippable unit and that can be deployed anywhere.
Docker got enormous popularity due to simplicity, isolation, faster configurations, scalability, security, and rapid deployment. It’s one of the most CI efficient tools and eliminates problems like “it is working on my computer but not working on production”.
When a non-containerized application or service is moved from one host to another, then it leads to some dependency issues, and docker is a brilliant choice to overcome such problems.
Docker's popularity comes from its standardized way to let you ship code faster, platform-independent, and cost-efficiency by improving resource utilization.
When we work on docker, then there are two main terminologies used regressively, and those are - docker image and docker container. Both are primary units of the Docker containerization.
Let’s see the difference - docker images vs containers :
What is Docker Image
A docker image is a beginning point for anyone who is starting to use docker.
A docker image is a file (or collection of files) and with some meta-data, that is built from a multiple series of layers.
Conceptually, images are made of layers, stacked on top of each other. Multiple docker images can share the layers that reduce disk usage, memory use, and transfer time.
A docker image, bundles all the essentials together, such as - packages, dependencies, config files, run-time, and application code that are required to run a fully operational container on any host machine.
We also know a docker image as a read-only template with instructions for creating one or more instances of containers.
Mostly, a docker image is started from another base image with some additional customization. For instance - you may build an image using a centos base image then install the Nginx web server and other dependencies, and copy the application code to make your application running. You can find plenty of them ready to use the base docker image on Docker Hub.
If you need complete control of the contents of your docker image, then you can also build your docker base image entirely from scratch. In such cases, docker’s own reserved, minimal image - “scratch” is used as a beginning point.
The most common method for creating a docker image is using a Dockerfile. Dockerfile is a text document that contains a series of commands (or instructions) to create the image and run it.
Each instruction from the Dockerfile creates an additional layer in the docker image. Each layer represents an intermediate image, built one on top of the other in stages, where each layer depends on the layer immediately below it.
If you change the Dockerfile and rebuild the image, then only changed layers are re-built. This is how the docker images are so lightweight, fast, and small. By default, Docker shows the top layer of an image, and it caches all intermediate layers.
Any docker image that is created locally or pulled from a remote repository is stored in the host machine and identified by an image-id (or with name + tag).
Here is an example of a Dockerfile:
FROM alpine:3.7 RUN apk update RUN apk add htop RUN apk add wget
Now use the docker image build command to create an image from Dockerfile and see the output:
If you look at the above screen-shot, then you can see the following things :
- Docker build shows the output in 4 steps - step ¼, step 2/4, step ¾, and step 4/4.
- Docker build executes one line (or instruction) at a time from Dockerfile.
- The Docker build creates an intermediary image for every step from Dockerfile. It started from a base image (alpine:3.7) and executed RUN apk update, which added another layer on the top of the base layer. The same process is repeated for the subsequent two instructions.
- It also tells us that the final docker image has four layers, and every layer has its image-id associated with it. We can also use these intermediary layers as separate images. Docker uses these intermediary layers as an image cache for faster image builds in the future.
You can find a new image in the host machine using docker images command.
Docker images are immutable as they can’t be modified. Though you can copy, share, and delete them. The immutability is a very useful characteristic during tests and production release of the application, as it allows easy restoration of the application.
What is Docker Container
As explained above, a docker image is a template or a portable file that bundles all the packages, dependencies, application code, and runtime so that the application runs fast and reliably on any environment unconcerned of the running platform.
A runnable instance of an image is called a docker container. Docker Engine, a containerization software is required on host machines to run docker containers from docker images.
Or a docker container is a running docker image.
When you create a container from an image, you add a new writable layer on top of the underlying image layers. We also know this layer as a container layer. Any changes in the running container like adding/changing/deleting files are written to this top container layer.
Mostly, a docker create command is used to create a writable container layer over an image and prepares to start a container and run the specified commands. It ensures that it configures the container in advance and ready to start when you need it.
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
The docker run is another commonly used command for creating and starting a container.
docker run = docker create + docker start
All the containers running on a host machine run isolated from one another and the host machine, by default.
All the containers run isolated from one another and host machines, by default. Isolation of containers ensures that a container runs uniformly on any host machine regardless of platform (Linux, macOS, or Windows) or environment - development, QA, or production.
Although containers run in isolation, you can control some configurations and underlying sub-systems like network, storage, etc, from host machines and other containers.
A docker image defines its containers and you can also provide custom configurations for a container when it is created or started.
- A docker image is a file or a read-only template that contains all the required - libraries, runtimes, application code, etc to run fully functional containers.
- Docker images are made of layers, stacked on each other. A layer is nothing but an intermediate image that gets added during the image build process.
- The most common method of building docker images is the use of Dockerfile. Though, you can also build images from a running container by modifying it and then creating an image from it.
- Docker reduces and optimizes system resources like disk, memory, and transfer time, etc by allowing multiple images to share layers.
- A docker container is a runnable instance of the docker image, whereas a docker image is a read-only file-system. Containers run isolated from each other and host-machine. Isolation is the greatest characteristic that ensures containers run any host machine regardless of platform or environment.