Docker Container : How to build and basic commands

docker1

In the previous post we read about how to pull and run a container with single RUN command. Here we will see how to build a container from scratch, perform operation on that container and push our container to docker hub.

docker1

Before we dive deeper, we need to understand that container is only runtime instance of a Image. And same image can be used to create multiple containers.Also, Each container is identified with a unique Container ID.

To list all the running containers

sudo docker ps

To list all the containers dead or alive

sudo docker container ls -all

Commands to perform operations on Continers

Docker container is formed when we create a runtime instance of Image. Here is life cycle of a docker contianer

docker-container-life-cycle

Here are all the operations which can be performed on docker container (you can get containerID by using the command – sudo docker container ls -al)

Go inside a Container (attach to a container)

sudo docker attach containerID

Top processes within a container

sudo docker top containerID

Stop a running container

sudo docker stop containerID

Delete a Container

sudo docker rm containerID

Check stats of a container (CPU/mem)

sudo docker stats containerID

Pause processes in running contianer

sudo docker pause containerID

Un-Pause processes in running contianer

sudo docker unpause containerID

Kill processes inside a container

sudo docker kill containerID

Building you own Docker Images

There are tonnes of images available on Docker HUB but you can always create your own image and then push it to docker hub.

Docker images are always created with a file called Dockerfile (D capital). You can have docker file in any directory of your system but your docker file will be stored by docker daemon in your local docker registry.

Editing of Docker file Check Here.

For now we will see how to build a Docker Image with Dockerfile. CD to the directory where your Dockerfile is stored and fire the build command (-t used to tag the image with the name dockerImage)

sudo docker build -t dockerImage

This docker Image will now be stored in our local docker registry (can be checked by – sudo docker images)

Pushing Docker Image to Docker Hub

Now that we have created our image in local registry, we will upload it to the Docker HUB so that it is available on the cloud

First create a account on docker hub and then login to docker hub from your local PC

sudo docker login

There is a notation for all the images present on dockerhub – username/imageName:tag

Now we will tag our image (it will take local image and tag it as Dockerhub image locally)

sudo docker tag dockerImage username/imageName:tag

Finally we will push our image to Dockerhub

sudo push username/imageName:tag

Now if somebody wants to run our image in their system, they just have to execute run command and our docker image will be first pulled and then run into the system

sudo docker run -it username/imageName:tag

That’s all for the basics of docker, the next articles on docker will talk about some advance processes we can do with docker.

Docker : Installation, Pull and Run a Container from Docker HUB

docker-install

Installation of Docker is pretty straightforward. In this article we will be focusing on Linux installation for docker. Do check below links for docker installation in other OS

-Install Docker on Ubuntu

-Install Docker on Windows

-Install Docker on MacOS

-Install Docker on Raspbian (Raspberry PI)

Alternatively, for Linux/Raspberry Pi just run this command and you are good to go

curl -sSL https://get.docker.com | sh

docker-install

Follow the below instructions to install docker on linux machine [OPTIONAL]

1. Add GPG keys for official docker repo

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

2. Add docker repo to apt sources

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

3. Update package database

sudo apt-get update

4. Install Docker

sudo apt-get install -y docker-ce

Moreover you can check the full installation HERE


Basic Commands to check Docker Installation

Check weather docker is installed and running (to quit CTRL+C)

sudo systemctl status docker

List Docker (list of docker commands, also management command-since docker 2017)

sudo docker

Docker Version (returns the version of client and server (engine/daemon/service)

sudo docker version

Docker Info (returns details about our configuration and setup for our engine)

sudo docker info

Docker Daemon Start/Stop

Docker has 2 components :
1. Docker Daemon (Engine) : This is the core of Docker which runs in the background
2. Docker CLI (Client) : Docker component with which user interacts

docker-cli-daemon

Both these components communicate with each other using REST API’s on Port number – 2376

START Docker Daemon

sudo service docker start

STOP Docker Daemon

sudo service docker stop

Running a Container

Before running a Container we need to have a Docker Image (as container is a runtime instance of docker image), we can not run a container without having a Docker Image , cause that will not make any sense.

So here are some Commands for for Docker Images

View Docker Images present on our computer

sudo docker images
sudo docker images ls

View details of Docker Image present

sudo docker inspect ImageID

Delete Docker image from System

sudo docker rmi ImageID

So now that we know about docker images lets see how to run them, and no worries if you don’t have any image in you system (we have that covered)


Docker RUN command

Here is the most important docker command which is used to Run Images and if that image is not present locally on you system then docker run will download it from dockerhub and run it

sudo docker run -it Image

We can attach additional parameter as -d (for detach mode) , -p (to map port of docker and host)

To give it a try, run ubuntu image (if its not present locally, which i guess not, then it will download and run that command)

sudo docker run centos –it /bin/bash

This command will download the centOS image from docker hub and start an interactive session at /bin/bash

Alternatively, if you want only to pull image and not run it, you can use the below command. Also this command can also be used when your local system has a outdated image and you want to update you local copy with updated copy.

sudo docker pull centos

Thats all for this session, in next tutorial we will explore more into containers.

Docker Overview : Architecture and Terms (Image, Container, HUB)

docker-build-ship-run

Docker is a container management service which helps to Build-Ship-Run our software anywhere. In simple terms Docker aggregates all the dependencies and files of our software into a single container, which is then treated as an independent unit and can be shipped and run anywhere. Also, Docker containers are platform and OS independent.

docker-build-ship-run

If you ever read about docker, you must be knowing how it is advantageous over using Virtual Machines, but there are many other benefits of using docker :-

  1. Docker containers are allocated resources(CPU, Memory, Storage) dynamically based on their usage; unlike VM’s in which we have to pre-allocate all the resources.
  2. Reduced startup time as docker runs on top our host OS – saves boot-up time.
  3. We can have multiple instances of programs within a single container, or have multiple containers which can communicate with each other.
  4. Provides a consistent computing environment throughout the whole SDLC (dev,qa,prod)

Here is an image which explains how Docker is different from VM’s

vm-vs-docker

Both Docker Container and VM requires an Infrastructure(hardware) and Host Operating System. Unlike Docker, Virtual Machines require another software called hypervisor -which helps to run Guest OS on top of Host OS- which require additional hardware resources and most of the functionalities of the guest OS are never used by our apps.

Whereas, Docker Containers – which are package of our app and its dependencies – just require a Docker Daemon which runs over our Host OS and is very light weight. Wait! whats docker daemon, hmm let us first go through all the terms which we will come across while working with Docker

 

Important terms related to Docker

  • Docker Engine (Docker Daemon) : Docker engine is a part of docker which runs Docker container. And Daemon is just a computer program that runs as a background process, rather than being under the direct control of an interactive user. So Docker engine/ Docker Daemon runs in background and we run our containers on top of it.
  • Docker Container : A Docker container is a live running instance of a Docker image. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
  • Docker Image : A Docker Image is the template (application plus required binaries, libraries, env variables and config files) needed to build a running Docker Container (the running instance of that image).
  • Docker HUB : Docker hub is a repository on cloud for all your docker images (like github for code). It host pre-build images of famous software’s like wordpress, Ubuntu, centOS etc. You can also push your images to docker hub which can be used by anyone.

That’s all for this tutorial, in next tutorial we will dive into some technicalities and see how to install docker and how to pull and run a container.