Introduction to Docker

Pradeep Gopalgowda
5 min readJul 26, 2019

Problems before Docker

The Problem:

The Answer:

What is Docker?

Docker is a computer program that performs operating-system-level virtualization, also known as “containerization”. It was first released in 2013 and is developed by Docker, Inc. Docker is used to run software package called “containers”.

Docker vs Virtual Machine

Docker Container Lifecycle

Common Docker Operations

docker -version : This command helps you know the installed version of docker software on your system.

docker pull <image-name> : This command helps you pull images from the central docker repository.

docker images : This command helps you in listing all the docker images, downloaded on your system.

docker run <image-name> : This command helps in running containers, from their image name.

docker ps : This command helps in listing all the containers which running in the system.

docker ps -a : If there are any stopped containers, they can be seen by adding the “-a” flag in this command.

docker exec <container-id> : For logging into/accessing the container, one can use the exec command.

docker stop <container-id> : This command helps in stopping the running container.

docker kill <container-id> : This command kills the container by stopping its execution immediately. The difference between “docker kill” and “docker stop” is where “docker stop” gives the container time to shutdown gracefully, in situations when it is taking too much time for getting the container to stop, one can opt to kill it.

docker rm <container-id> : This command will remove a stopped container from the system.

docker rmi <image-id> : This command will remove an image from the system.

Saving Changes to a Container

docker commit <container-id> <name-for-image> : with this command, a new image is created which can be seen under docker images with the same name as passed in the command.

Example
Install Apache in Ubuntu image
Saving changes to a new container
Start Apache server on the new container

Open your system IP address with port number 82 in any browser.

Apache Server

Pushing a Container to Docker Hub

docker push <image-name>
Docker Hub

Introduction to Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Various commands in Dockerfile

  • FROM : The FROM keyword is used to define the base image, on which we will be building. Ex: FROM ubuntu
  • ADD : The ADD keyword is used to add files to the container being built. The syntax which is followed is ADD<source> <destination in container> Ex: ADD . /var/www/html
  • RUN : The Run keyword is to add layers to the base image, by installing components. Each RUN statement, adds a new layer to the docker image. Ex: Run apt-get update
  • CMD : The CMD keyword is used to run commands on the start of the container. These commands run only when there is no argument specified while running the container. Ex: CMD apachectl -D FOREGROUND
  • ENTRYPOINT : The ENTRYPOINT keyword is used strictly run commands the moment the container initializes. The difference between CMD and ENTRYPOINT is, ENTRYPOINT will run irrespective of the fact whether argument is specified or not.
  • ENV : The ENV keyword is used to define environment variables in the container run-time.

Lets try to build a docker image and run on your system

*Create a Dockerfile to install an appache server on ubuntu image.

Dockerfile

*Create a simple html file.

HTML

*Build the docker image using docker build cmd.

docker build

*Run the generated docker image and open it on port 84.

docker run

*Open your system IP Address with port number 84 in any

Apache Server

*Open the 1.html file with the same IP Address.

HTML

*By logging into the docker image and check out the ENV

ENV

Introduction to Docker Volumes

Docker Volumes are used to persist data across the lifetime of a container.

Bind Mount

From the above screenshot the files inside app folder are that of the Dockerfile folder and these are actually mirrored from the directory of the host operating system. This operation is called Bind Mount and it doesn’t work in different environment. To overcome this issue create a docker volume.

docker volume syntax

Attaching it to a container by creating the docker volume

Yahooo! You have completed the tutorial. Check out my published articles in Medium to know more about Jenkins and Devops :)

--

--