With more organizations deciding to adopt docker to containerize their applications, it is absolutely crucial to be on par with Docker technology. This Docker series will try to be as precise and concise as possible and still be able to get you started on using Docker in your environment.

The following sections has been put together in such a way that one can quickly pick up and adopt docker in their environment and benefit from it.

Docker key points to know, before we jump right in.

Docker image: Developers and sysadmins build and ship applications in the form of images built with Docker.
Docker Container: A image that is in a running state is a container and a container in a stopped state is an image.
Docker Hub: Docker Hub contains Official images for applications ranging from Nginx, Mysql, WordPress, Php, python, Java etc to OS base images such as Ubuntu, Centos and more, maintained by the Docker community. Developers and sysadmins can download these images, customize or build applications with it, or create their own application images and store them online (both privately and publicly) on Docker Hub for quick Application distribution.

NOTE: An OS base image is an OS without the kernel. Docker OS images rely on the kernel of Host OS on which it was installed. i.e If you are running Ubuntu docker Image on a Redhat host, Ubuntu does not use a kernel of its own, rather relies on the Kernel of Redhat. Most of the Docker applications runs along with a base image, such as Centos, Ubuntu, Alpine Linux etc. 

Docker binary: Docker rpm can be installed on a Linux system, with which the end user can download an image from Docker Hub and run the same. The application that was contained and shipped in the image starts running out of the box.
Why Docker?: You run the same image in all your environments, thus making the environment for the application identical with no dependency issues and configuration drifts.

Installing and starting docker daemon

#install and start Docker on Redhat Linux

yum install docker
systemctl start docker
systemctl enable docker

‘docker pull’: Downloading docker images.

Run ‘docker pull image-name’ to download an image from Docker Hub.

#Lets download official nginx image available on Docker hub.
docker pull nginx:1.10.1-alpine

#list downloaded Docker images.
docker images

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              452a96d81c30        4 weeks ago         79.6 MB
docker.io/hello-world   latest              e38bc07ac18e        7 weeks ago         1.85 kB
docker.io/centos        latest              e934aafc2206        7 weeks ago         199 MB
docker.io/nginx         1.10.1-alpine       2cd900f340dd        19 months ago       54 MB

The above lists all the downloaded Docker images, with the version and the unique ID of the image.

‘docker run’: initiate a docker container by running a docker image’.

Docker image can be run in the following format:
docker run [Image ID]

Note: Nginx needs to listen on port 80, therefore we need to map the container's port with the host's port. Thus the host forwards its requests from port 80 to the container's port 80.  This can be done by passing '-p 80:80' to the docker run command i.e (-p host-port:container-port ).

Optionally you can also give a name to your docker container with (–name) when running an image.

#Run the nginx image in the name 'nginx_test' with 
docker run --name nginx_test -p 80:80 2cd900f340dd

nginx webserver should be up on http://host-ip-address:80

#Run the nginx image with  by mapping a different host port.
docker run --name nginx_test -p 80:9000 

ngingx webserver should be up on http://host-ip-address:9000

You would notice at this time that when you run the above commands, docker starts running but you are unable to use the terminal any further, since docker is running in the foreground. To overcome this, you can pass -d flag to the docker run command. -d stands for ‘detached mode’ i.e to run the docker image in the background.

docker run --name nginx_test -d -p 80:9000 

‘docker exec’: start an interactive shell or run commands in containers.

For whatever reason you want to run commands into a container, you can either start an interactive shell to a running container or you can run a command from the host and pass it to the running container.

# start an interactive session to a running container
#docker exec -ti [container_id] /bin/sh
docker exec -ti fbc8bddeac5b /bin/sh

/ # cat /etc/os-release
NAME="Alpine Linux"
PRETTY_NAME="Alpine Linux v3.4"
/ # 

you can exit from the shell with [exit]. This kills the login shell.
If you’d like to exit without terminating the shell, you can run [ctrl]+[p][q]

#run a different command on container directly from host.
#docker exec -ti [container_id] [some_command]
docker exec -ti fbc8bddeac5b ps

    1 root       0:00 nginx: master process nginx -g daemon off;
    5 nginx      0:00 nginx: worker process
   10 root       0:00 /bin/sh
   19 root       0:00 /bin/sh
   27 root       0:00 ps

docker exec -ti fbc8bddeac5b pwd


docker exec -ti fbc8bddeac5b ls

bin      etc      lib      media    proc     run      srv      tmp      var
dev      home     linuxrc  mnt      root     sbin     sys      usr

docker exec -ti fbc8bddeac5b hostname


‘docker ps’: manage containers.

docker ps command lists all containers (running images).

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
5847e195ffe1        2cd900f340dd        "nginx -g 'daemon ..."   16 seconds ago      Up 16 seconds>80/tcp, 443/tcp   quizzical_tesla

Docker engine assigns a unique ID for each container. (not to confuse docker Image IDs & reiterating that a container is a running image).
Note: you can run the same image multiple times simultaneously and you have unique container IDs for each container. But at no point two different containers can listen on the same port. (Just like how, no two processes can listen on same port at a time).

‘docker stop’,’docker kill’: stop or kill a container.

#docker stop [container_id] stops the container with the specified ID in a gracefull manner.
docker stop 5847e195ffe1

#docker kill [container_id] kills the container with a kill signal.
docker kill 5847e195ffe1

‘docker rm’,’docker rmi’: remove docker containers and images.

Removing stopped docker containers and docker images

#list all containers including the stopped ones
docker ps -a

#docker rm [container_id]
docker rm 5847e195ffe1

#docker rmi [image_id]
docker rmi 2cd900f340dd