Docker containers and images 🐳

Docker enables developers to package applications into containers β€” standardized executable components combining application source code with the OS libraries and dependencies required to run that code in any environment. Docker is very powerful and can be overwhelming. Hopefully this post will be a good overview of Docker containers and images which are core Docker concepts.

Docker images :

You can think of docker images as classes in programming languages. We will also explore how we can think of images like stopped containers. Images are built time constructs whereas containers are run time constructs.

A container is to run an application or service hence the image a container is created from contains OS and all application files requires to run the service or app. Since containers are all about being fast and lightweight, the images are usually small and stripped of all non essential parts.

Image layers :

There is an important concept to image called image layers. Every instruction represents a layer in your Dockerfile. An image is built from multiple such layers based on different instructions. Docker caches each of these layers. When you re-build an image, only the instructions where something is changed and all the instructions thereafter are re-evaluated.

For example , the following Dockerfile for an image contains 6 layers.

Let’s say we built this image for the first time. Now Docker has cached the layers. Now when we change something in our source code, docker will run everything from COPY instruction till the end again. For the top two instructions though it will use the cache.

Docker containers :

Container is the runtime instance of an image. Containers share the OS/Kernel with the host they’re running on. Easiest way to start a container is by using the docker container run command.

You can visualize container as an outer layer to the image its build from. You can create multiple containers from same image. Hence the analogy of classes for docker images.

When we run the docker run command :

  • Docker looks for image locally, if docker doesn’t find it, it looks it in remote image repository and downloads latest version (or the specified version).
  • Creates container based on the image
  • Gives it virtual IP on a private network inside docker engine
  • Opens up port if stated (port is stated with -p flag)
  • Starts container by running the command stated by CMD instruction in image DockerFile

There is so much more to Docker but hopefully this is a good overview if you are just starting out your Docker journey 🐳

More resources:

--

--