Skip to main content

Understanding container (Docker)

Docker:
OCI: Open Container Initiatives (contributors are Docker Inc. and CoreOS Inc.). Is standardizing the “container runtime format” and “container image format”.
ESXI : in vmware.
Moby: A Docker project written in golang (GO language).

Test docker installation:
Docker client and server is working fine can be check using docker version command.
mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker version
Client:
 Version:       18.03.0-ce
 API version:   1.37
 Go version:    go1.9.4
 Git commit:    0520e24302
 Built: Fri Mar 23 08:31:36 2018
 OS/Arch:       windows/amd64
 Experimental:  false
 Orchestrator:  swarm

Server: Docker Engine - Community
 Engine:
  Version:      18.09.2
  API version:  1.39 (minimum version 1.12)
  Go version:   go1.10.6
  Git commit:   6247962
  Built:        Sun Feb 10 04:20:28 2019
  OS/Arch:      linux/amd64
  Experimental: false
Docker installation contain docker engine (client and deamon), docker compose and docker machine and their version can be check using following command.
-Docker-machine –version
-Docker-compose –version
Docker has two perspectives
1- Ops
2- Dev

Ops:
Docker has two major components called server (deamon) or client.
In linux:
local IPC/Unix socket at /var/run/docker.sock
Client | --------------------------------------------------------------------------à | Server
Communicate

In windows:
npipe:////./pipe/docker_engine
Client | --------------------------------------------------------------------------à | Server
Communicate
Images:
In programmer perspective think of it as a class.
-docker image ls OR
-docker images
List you all images which you have under docker host.
Images can be pull from different repositories (official and non-official)
-docker image pull repository:TAG  //general command, put appropriate repository and tag name.

Containers:
Once you have images in your docker host. One can run the container against these images and command which is use for it is
-docker container run –it repository:tag /bin/bash
The run command will run the new container of image repository:tag and the –it option will connect the current terminal with the created one. So after connecting user will be writing on new container.
Use CTRL-PQ command for coming out from the container without killing it.
And now you can monitor how many containers are running using following command.
-docker container ls
Exec command will help you to connect back to any container running on your docker host, use container name or ID for connecting.
-docker container exec –it container name or ID bash
Bash in command will connect to bash terminal
Stop and remove.
-docker container stop container name
-docker container rm container name // remove container.

Dev perspective:
We will clone a code from a git repository.
If you face an error saying do not have sufficient permission. So execute git command in a folder where you have write permission.
Once the code is clone then examine the Dockerfile which is present in this node.js app.
- cat Dockerfile
# Test web-app to use with Pluralsight courses and Docker Deep Dive book
# Linux x64
FROM alpine
LABEL maintainer="nigelpoulton@hotmail.com"
# Install Node and NPM
RUN apk add --update nodejs nodejs-npm
# Copy app to /src
COPY . /src
WORKDIR /src
# Install dependencies
RUN  npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]
Build Image:
-docker image build –t  test:latest .
Build an image with the name test:latest and the “.” means current directory. Image will be build using Dockerfile. And then the container can be run using the newly created image.
Famous Registory is Docker Hub and there are many others. You can pull an image on your docker host and more than one container can be run out of an image.
Images are made up of different layers and tend to be small in size.
1 to many
IMAGE ---------------------------------------------------------à Container
(class or build time) (object or runtime)
-docker container run OR –docker service create commands are use to create a multiple container from a single image.
Once container is created from an image then one cannot delete an image unless all of its associate containers are stop and deleted.
Purpose of a container is to run an application or service. And it means its image must contains required OS and all dependencies of an application or service.
Pull an image using -docker image pull repository:TAG // ubuntu:latest
 All popular OS and applications have their images in docker hub registry.
There are many other un-official registries.
Images layers:
A single image consist of several mini layers. Each mini layer represent to something and these all are read only layers.
Mini layers do exist can be examine using pull command.
> docker image pull ubuntu:latest  
latest: Pulling from library/ubuntu
952132ac251a: Pull complete  // these are mini layers.
82659f8f1b76: Pull complete // these are mini layers.
c19118ca682d: Pull complete // these are mini layers.
8296858250fe: Pull complete // these are mini layers.
24e0251a0e2c: Pull complete // these are mini layers.
Digest: sha256:f4691c96e6bbaa99d...28ae95a60369c506dd6e6f6ab
Status: Downloaded newer image for ubuntu:latest
Also remember the highlighted digest, it is an unique identifier of an image.
Layers can be seen by inspect command.
> docker image inspect ubuntu:latest
[
    {
        "Id": "sha256:47b19964fb500f3158ae57f20d16d8784cc4af37c52c49d3b4f5bc5eede49541",
        "RepoTags": [
            "ubuntu:latest"
        ],
        "RepoDigests": [
            "ubuntu@sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210"
        ],
"RootFS": {
            "Type": "layers",
            "Layers": [ // 5 layers present
                "sha256:bebe7ce6215aee349bee5d67222abeb5c5a834bbeaa2f2f5d05363d9fd68db41",
                "sha256:283fb404ea9415ab48456fd8a82b153b1a719491cdf7b806d1853b047d00f27f",
                "sha256:663e8522d78b5b767f15b2e43885da5975068e3195bbbfa8fc3a082297a361c1",
                "sha256:4b7d93055d8781d27259ba5780938e6a78d8ef691c94ee9abc3616c1b009ec4a"
            ]
        },]

While pulling an image if one layer is already downloaded then it not fetched again, it will reduce the traffic over internet and reduce the size of the image as well. If any layer is edited then a new copy will be created and this over-written layers will no longer remain part of the image.
>docker images pull repository@digest // will fetch repository with mentioned digest.
Each layer has digest and an image which is a combination of an mini layers also has digest. And once the image is compressed a new hash is created call it distribution hash.

Following command will be use to delete all images in a docker host.
>docker image rm $(docker image ls -q) -f

Container:
Container is an object of an image.Its life is define by the program which it is running. For example when container is run using a command called
>docker container run -it ubuntu /bin/bash
Then the life of a container is the “bash”program which is run through a command. Once bash is close it means container is down.
If you want to run a container for few seconds then use a command sleep 10 in place of /bin/bash and container will die in 10 sec.

Container can be stop using stop command and then restarted using start command and can be deleted using rm command.

General command for running a container.
>docker container run -<options> <image>:<tag> <command>
Here we will discuss container life cycle.One thing I would like to highlight here containers do persist data as well.

General command for running a container:
>docker container run --name dolly -it ubuntu:latest /bin/bash  [dolly is name of the container]
mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker image pull ubuntu:latest
latest: Pulling from library/ubuntu
Digest: sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210
Status: Image is up to date for ubuntu:latest
mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container run -it ubuntu:latest /bin/bash
root@5c5bed2d2f5c:/# ps -elf
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  80   0 -  4627 -      10:05 pts/0    00:00:00 /bin/bash
4 R root        10     1  0  80   0 -  8600 -      10:05 pts/0    00:00:00 ps -elf
root@5c5bed2d2f5c:/# cd tmp
root@5c5bed2d2f5c:/tmp# ls
root@5c5bed2d2f5c:/tmp# echo "devops:abdulbasit" > testfile
root@5c5bed2d2f5c:/tmp# ls
testfile
root@5c5bed2d2f5c:/tmp# cat testfile
devops:abdulbasit
root@5c5bed2d2f5c:/tmp#
Press Ctrl-PQ to exit the container without killing it.
mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5c5bed2d2f5c        ubuntu:latest       "/bin/bash"         21 minutes ago      Up 21 minutes                           fervent_hamilton

mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container stop 5c5bed2d2f5c
5c5bed2d2f5c

mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
5c5bed2d2f5c        ubuntu:latest       "/bin/bash"         23 minutes ago      Exited (0) 22 seconds ago         fervent_hamilton

Now start the container and check the file which you have created under tmp folder.
So now for starting a container you need to use the start command with name or id of the container.
mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container start 5c5bed2d2f5c
5c5bed2d2f5c

Now connect with the bash using the exec command.

mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container exec -it 5c5bed2d2f5c bash
root@5c5bed2d2f5c:/# cd tmp
root@5c5bed2d2f5c:/tmp# ls
testfile
root@5c5bed2d2f5c:/tmp# cat testfile
devops:abdulbasit

CTRL - PQ quite the process without killing it.

mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5c5bed2d2f5c        ubuntu:latest       "/bin/bash"         About an hour ago   Up 25 minutes                           fervent_hamilton
mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container stop 5c5bed2d2f5c
5c5bed2d2f5c
mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container rm 5c5bed2d2f5c
5c5bed2d2f5c


mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

mabasit@DESKTOP-GHE8HHU MINGW64 /c/Program Files/Docker Toolbox
$
Now you have seen that the file is stored in the container. Hence proof container do persist data. We have also covered the life cycle of the container.
run -> stop -> start (if needed again) -> remove

Comments