Docker tutorials – introduction part 2

In this blog post, we are continuing the introduction part on our Docker discussion. I will try to include more commandline usages for managing Docker containers. Managing Docker containers via command-line is very important. I will try 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 “Introduction to Docker containers – part 1” 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.

Introduction to docker – part II

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?

We can not exclude this step in introduction to docker article. You can use the “docker build” command to build an image using a Dockerfile. Please see the examples:

Creating a new version image of myapache with existing Docker file:

docker build -t myapache:ver.2 .

The above command must be executed within the context directory. If you wanna use Dockerfile from different location you can use -f option or “-” option.

docker build -t myapache:ver.2.1 - < /home/arunlal/myDockerLab/Apache/Dockerfile

Here I built two more images using the same Dockerfile. Now run “docker images” and see the newly added images:

arunlal@linux:~/myDockerLab/test1$ 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            ver.2     7003e959ed73        6 days ago          257MB
myapache            ver.2.1   7003e959ed73        6 days ago          257MB

Here Ver.2 and Ver.2.1 are new builds

How to create/run a Docker container using Docker image?

To illustrate the basic operations I am creating two containers. We can use “docker run” command to create/start container.

docker run -it --name My_Apache -d -p 1883:80 7003e959ed73

docker run -it --name My_Apache_two -d -p 1884:80 7003e959ed73

I created two containers for my Apache instances which are listening on different port. We can access it like localhost:port

Example:

arunlal@linux:~/myDockerLab/test1$ docker run -it --name My_Apache -d -p 1883:80 7003e959ed73
be3e4dcac63b437a59b1f4da700ec600a67e654f871a607f01a8bdb70a5cdf09

arunlal@linux:~/myDockerLab/test1$ docker run -it --name My_Apache_two -d -p 1884:80 7003e959ed73
945981e85d72e0a9de2c15ffc595b3b036ab0b3306b7a1ce2eac40daa4483779

How to list Docker containers?

To list running containers you can use “docker ps” command and “-a” option along with “docker ps” will list running and Exited containers.

arunlal@linux:~/myDockerLab/test1$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
945981e85d72        7003e959ed73        "apache2ctl -D FOR..."   22 minutes ago      Up 22 minutes       0.0.0.0:1884->80/tcp   My_Apache_two
be3e4dcac63b        7003e959ed73        "apache2ctl -D FOR..."   23 minutes ago      Up 23 minutes       0.0.0.0:1883->80/tcp   My_Apache

arunlal@linux:~/myDockerLab/test1$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
945981e85d72        7003e959ed73        "apache2ctl -D FOR..."   22 minutes ago      Up 22 minutes       0.0.0.0:1884->80/tcp   My_Apache_two
be3e4dcac63b        7003e959ed73        "apache2ctl -D FOR..."   23 minutes ago      Up 23 minutes       0.0.0.0:1883->80/tcp   My_Apache
114be4657f4d        7003e959ed73        "apache2ctl -D FOR..."   6 days ago          Exited (0) 17 minutes ago                          My_Apache
e23a3af0ed31        ae140ec22b7b        "/bin/sh -c 'apt-g..."   6 days ago          Exited (1) 3 days ago                              confident_varahamihira
0e674a998dff        ae140ec22b7b        "/bin/sh -c 'sudo ..."   6 days ago          Exited (1) 6 days ago                              quizzical_elion
d3957bbb9fb4        085f987d65d5        "/bin/sh -c 'sudo ..."   6 days ago          Exited (100) 6 days ago                            inspiring_wozniak
7a9547bf42dd        085f987d65d5        "/bin/sh -c 'apt-g..."   6 days ago          Exited (100) 6 days ago                            elegant_montalcini
f27f003ebd67        hello-world         "/hello"                 6 days ago          Exited (0) 6 days ago                              pedantic_ediso

How to start / stop / restart Docker containers?

You can use “start“, “stop” and “restart” options along with “docker” command. See the examples pasted below:

runlal@linux:~/myDockerLab/test1$ docker stop My_Apache
My_Apache
arunlal@linux:~/myDockerLab/test1$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
0627fe4281b7        7003e959ed73        "apache2ctl -D FOR..."   40 seconds ago      Up 39 seconds       0.0.0.0:1884->80/tcp   My_Apache_two

arunlal@linux:~/myDockerLab/test1$ docker start My_Apache
My_Apache
arunlal@linux:~/myDockerLab/test1$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
0627fe4281b7        7003e959ed73        "apache2ctl -D FOR..."   48 seconds ago      Up 46 seconds       0.0.0.0:1884->80/tcp   My_Apache_two
9ea28cba85bb        7003e959ed73        "apache2ctl -D FOR..."   51 seconds ago      Up 1 second         0.0.0.0:1883->80/tcp   My_Apache

arunlal@linux:~/myDockerLab/test1$ docker restart My_Apache
My_Apache
arunlal@linux:~/myDockerLab/test1$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES
0627fe4281b7        7003e959ed73        "apache2ctl -D FOR..."   About a minute ago   Up About a minute   0.0.0.0:1884->80/tcp   My_Apache_two
arunlal@linux:~/myDockerLab/test1$ docker start My_Apache
My_Apache

How to enter into a docker container?

Execute the following command to enter into the container just we created:

docker exec -it My_Apache_Test /bin/bash

Example:

arunlal@linux:~/myDockerLab/Apache$ docker exec -it My_Apache_Test /bin/bash
root@f170a8e403ff:/# 

root@f170a8e403ff:/# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 14.04.5 LTS
Release:	14.04
Codename:	trusty

root@f170a8e403ff:/# hostname
f170a8e403ff

root@f170a8e403ff:/# w
 19:44:59 up  4:36,  0 users,  load average: 1.03, 1.26, 1.15
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT

How to remove a Docker container?

We can use the command “docker rm” to remove a container. The docker rm command, which works only on stopped containers, allows you to specify the name or the ID of one or more containers, so we can delete both with the following:

arunlal@linux:~/myDockerLab/test1$ docker rm 9ea28cba85bb My_Apache_two
9ea28cba85bb
My_Apache_two

Now check the result of “docker ps

arunlal@linux:~/myDockerLab/test1$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Nothing there… That’s the basic operations of docker containers.
Will add more details in upcoming posts.. I hope you loved introduction to docker part 2. Add you suggestions in comment box.

Thanks!!

What is a Linux container?

Basic: Containerisation helps to isolate processes. You can run your App/Services as an isolated process, running from a distinct image that provides all files necessary to support the processes.

Basically Linux containers are OS level virtualisation technique for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.

More, What is a Linux container?

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

One thought on “Docker tutorials – introduction part 2

  1. Containerisation helps to isolate processes and Linux containers are OS level virtualisation technique for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.

Leave a Reply

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