Managing Docker containers – basics, part 2

Managing Docker containers via command-line is very important.

In this article I am trying to explain the details of Docker images, Docker containers and the relationship between them. Managing Docker containers (run, start, stop, remove, exec etc) via Linux command line.

I recommend you to read the article “An introduction note to Docker containers” before starting managing Docker containers.

Docker images are used to create Docker containers. Docker image is an insert template used to create Docker containers. Understanding how Docker builds and store images and how these images used by Docker containers will help you to create/design a best Docker container for your application.

Docker images and it’s layers

Layers? Yeah, Docker image is built up from a series of layers. A set of Read Only layers. These layers are defined in Docker file. Each line in the Dockerfile (instructions) represents as a layer.

Each layer except the last top layer is Read Only. The last layer (container layer) is Read Write.

Sample Docker file

FROM ubuntu:16.04
RUN apt-get update && apt-get upgrade
RUN apt-get install -y apache2 
EXPOSE 80
CMD ["apache2ctl", "-D", "FOREGROUND"]

The above Dockerfile is a sample Dockerfile to deploy Apache on Ubuntu 16.04 base image. Each of the line in this Dockerfile will create each layer.

The FROM statement starts out by creating a layer from the ubuntu:16.04 image. The RUN statements upgrade it and the next RUN will install Apache2 on this base image. The EXPOSE statement will help to run base Apache2 of this container in port number 80. Finally, the last layer specifies what command to run within the container.

How to deploy Apache on Docker container?

Here we are going to create a container with Apache installed. As I explained in aforementioned article, the basic requirement to launch a container is Docker image. We need a Dockerfile with proper instructions to build the image.

Here we go.. Docker can build images automatically by reading the instructions from this file. This is a normal text file contains all the commands to build an image. You have to maintain a specific format to write Dockerfile. Read more….

Each layer is only a set of differences from the layer before it. The layers are stacked on top of each other. When you create a new container, you add a new writable layer on top of the underlying layers. This layer is often called the “container layer”.

All changes made to the running container, such as writing new files, modifying existing files, and deleting files, are written to this thin writable container layer.

See the image below:

Each layers in the Docker image represents the statements in the Dockerfile.

The main difference between Docker image and container is the writable permission on container.

All writes to the container that add new or modify existing data are stored in this writable layer. When the container is deleted, the writable layer is also deleted. The underlying image remains unchanged.

We can create multiple containers from the same Docker image.

See the image below:

How Docker manages data for each Docker containers?

That’s an important question, cuz, we create each containers for deploying our Applications. So storing data of every container is an important thing.

Docker uses storage drivers to manage the contents of the image layers and the writable container layer. Each storage driver handles the implementation differently, but all drivers use stackable image layers and the copy-on-write (CoW) strategy.

Managing Docker containers

Here we go with some basic commands to build, run, start, stop, remove etc operations. We have the image from the aforementioned Dockerfile (image id: 7003e959ed73)

Command to list available images:

docker images

Example

arunlal@linux:~/myDockerLab/Apache$ docker images
REPOSITORY          TAG      IMAGE ID            CREATED             SIZE
myapache            0.0.1    7003e959ed73        6 days ago          257MB
myapache            ver.1    7003e959ed73        6 days ago          257MB
myapache            ver0.1   7003e959ed73        6 days ago          257MB

How to create a docker image?

You can use the “docker build” command to build an image using a Dockerfile. Please see the examples:

Arunlal Ashok

DevOps (Server & Cloud infrastructure) Engineer. I'm dealing Linux servers since 2012. I started this blog to share and discuss my ideas. Always happy for an open discussion! Write to arun (@) crybit (dot) com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *