Coffee with Docker (Part 1)

Gourav Mukhija
10 min readJan 28, 2020

Attention: Bring your cup of coffee, sit comfortably because this is a hands-on tutorial on docker, so get ready to learn the basics of docker and know how to build, run and push containers.

What is Docker and What problem does docker solve?

Do you know the famous phrase It works on my machine?

Yeah, forget about it, with Docker it is not an excuse anymore. Docker allows you to locally run the same (or almost the same) environment which will be used in production.

Docker is a platform as a service (PaaS) which provide freedom to developers and IT to build, manage and secure applications without the fear of technology or infrastructure lock-in.

Docker use OS-level virtualization to deliver software in packages called containers.

In other words, we can say “Containers allow us to package all the things that our application needs like such as libraries and other dependencies and ship it all as a single package”. In this way, our application can be run on any machine and have the same behaviour.

Myth: Docker containers are Mini Virtual Machine (VM)

We can compare docker containers with virtual machine but its not right to say that containers are mini VM because

  • Containers are just process running on host operating system.
  • Containers are limited to resource what they can access.
  • Containers exit when the process stop.

Docker is basically all about speed. Develop Faster, Build Faster, Test Faster, Deploy Faster and Recover Faster.

Let’s not deep dive more into what docker is, why we have to use docker ….and so so because there are so many articles written on internet to showcase beauty of docker. If you really interested to read more about this please visit link such as official documentation , medium article etc.

Docker Installation Guide

Since the installation depends on your operating system, we will not cover it on this post. To install Docker on your OS please follow the official docs:

Install Docker for Mac

Install Docker for Windows

Get Docker CE for Ubuntu

Important: Before installing docker, please understand difference between Docker Community Edition and Docker Enterprise Edition here

Docker Basic Terminology

Image : Image is a package with all the dependencies and information needed (e.g. binaries, libraries, application code etc.) to create a container.

Container: It is running instance of docker image. A container represents the execution of a single application, process, or service. It consists of the contents of a Docker image, an execution environment, and a standard set of instructions. We can have multiple containers of the same image.

Docker Hub: A public registry to upload images and work with them. Docker Hub provides Docker image hosting, public or private registries, build triggers and web hooks, and integration with GitHub and Bitbucket.

Dockerfile: A text file that contains instructions for how to build a Docker image. It’s like a batch script, the first line states the base image to begin with and then follow the instructions to install required programs, copy files and so on, until you get the working environment you need.

Compose: A command-line tool and YAML file format with metadata for defining and running multi-container applications.

Volumes: Offer a writable filesystem that the container can use. Since images are read-only but most programs need to write to the filesystem, volumes add a writable layer, on top of the container image, so the programs have access to a writable filesystem.

Tag: A mark or label you can apply to images so that different images or versions of the same image (depending on the version number or the target environment) can be identified.

Cluster: A collection of Docker hosts exposed as if it were a single virtual Docker host, so that the application can scale to multiple instances of the services spread across multiple hosts within the cluster.

Orchestrator: A tool that simplifies management of clusters and Docker hosts. Orchestrators enable you to manage their images, containers, and hosts through a command line interface (CLI) or a graphical UI. You can manage container networking, configurations, load balancing, service discovery, high availability, Docker host configuration, and more. An orchestrator is responsible for running, distributing, scaling, and healing workloads across a collection of nodes.

Docker Engine: One of the core components which is responsible for the overall functioning of the Docker platform. Docker Engine is a client-server based application and consists of 3 main components — Server, REST API and Client.

Image Source: https://docs.docker.com

The Server runs a daemon known as dockerd (Docker Daemon), which is nothing but a process. It is responsible for creating and managing Docker Images, Containers, Networks and Volumes on the Docker platform.

The REST API specifies how the applications can interact with the Server, and instruct it to get their job done.

The Client is nothing but a command line interface, that allows users to interact with Docker using the commands.

Docker Commands

As I already stated in the beginning, this is hands-on tutorial. So before proceeding to this section please make sure you have

Prerequisite

Docker Login Account : If you are already registered on docker.com then its good otherwise please register yourself here.

Docker installed on machine: Awwww…… I forgot some of you don’t have docker right now. It’s fine not a big deal because Bill Gates is right.

We have lazy way to do docker practice also but this way also require docker login account. Please follow below link to play with docker.

Basic Docker commands

docker version (command verify cli can talk to engine)

docker info (command show configuration of engine)

Awwwww……, I am loving it.

First we understand docker basic command line structure

Old way : docker <command> (options)

New way: docker <command><sub command> (options)

e.g. earlier we use docker ps to see running docker container but now we use docker container ls (docker support both way).

Command shortcut: when you dive more into docker programming then you will encounter alphabets in commands like -v, -d, -a etc. there are two ways to write docker commands with or without shortcuts like -

Let’s try to understand more about above command with same example.

when we write below command

docker container run -d -p 80:80 --name myserver nginx

then it perform below task

Note: In above example 2nd and 3rd commands are same as 4th, 5th commands.

Above command start nginx server on port 80

Yippyyyyyy ……, I am now able to run docker container.

Docker container command basic structure

docker container <command> <container_id>

Important: If you don’t know how to write docker command then put --help in the end of docker command then terminal will show command notation.

Most used docker commands are

To display all running containers:

docker ps or docker container ls

To display all available containers:

docker ps -a or docker container ls -a

To start docker container:

docker container start <container_id>

To stop running docker container:

docker container stop <container_id>

To show container logs:

docker container logs <container_id>

To remove container(s):

docker container rm <container_id1> <container_id2> <container_id3>

To show all running process inside container:

docker container top <container_id>

To show container configuration:

docker container inspect <container_id>

To display all available docker images

docker images or docker image ls

To display docker image history

docker history <image_id> or docker image history <image_id>

To display detailed information (configuration) about image

docker image inspect <image_id>

To get more information about command:

docker container COMMAND --help

To build docker image using Dockerfile:

docker build -f <some_dockerfile>

To push docker image to repo:

docker image push <image_name>

Docker System Clean up:

docker image prune (cleanup just dangling images)

docker system prune (clean up everything)

docker image prune -a (remove all images which are not in use)

docker system df (see how much space docker is using)

Important:

1) instead of <container_id> you can also write <container_name> or first three digits of container_id as well.

2) While removing multiple containers using command “docker container rm <container1 container2 …>” sometime it give error to remove running container because of safety reasons. so use -f to remove container forcefully.

e.g. docker container rm -f <container_id1 container_id3 container_id3>

Ahem ahem ….. it seems now we are ready to justify original statement of the post “build, run and push docker containers” with the help of below assignment using nginx example.

Purpose of below assignment is to change nginx server browser message and publish that image to dockerhub account.

Step 1: Search nginx docker image

docker search nginx

Step 2 (optional): Download docker image in host machine

docker pull nginx

Step 3: Run nginx docker container in local machine

docker container run -p 80:80 --name webserver -d nginx

Note: This command first search image in cache, if image not available then it download image from server.

Command to see process inside running container

docker container top <container_name/id>

Step 4 : Create a tag before start working on docker image

docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Step 5 (Most important): Enter into nginx container shell and see file structure

docker container exec -it <container_name> bash

Step 6: Copy browser html file from container and update html file.

docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Note: To know more about how to copy files from container to host machine and vice versa read this post.

After executing above command, I am able to see html file in my machine and I have updated heading and title message in html file like ‘Welcome to Gourav nginx!’

Step 7: Copy browser html file from container and update html file.

docker cp modified_file_path container_id:file_path_in_container

After executing above command, If you refresh browser window you will be able to see updated browser message.

Step 8: Commit changes or Create a new image from a container’s changes

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Important: Tag name should always start with username/ otherwise while pushing image to server you will encounter access denied error

Step 9: Push image to docker hub account

Important: Before pushing image to server you must have to login first into docker account. e.g.

After successfully login, you are ready to push image to server with below command

docker push [OPTIONS] NAME[:TAG]

Verify Image is successfully pushed to dockerhub server or not

Login into docker hub account (hub.docker.com) using docker credentials and you will be able to see image in Repositories section.

Verify Changes are successfully pushed to container or not

Pull new container and verify changes in browser

docker pull username/image:tag

Step 9: Logout from docker cli

Once you are done with everything, don’t forgot to logout from docker cli.

docker logout

Yippyyyyyyyyyy……… changes are working fine in browser. Hope you all have enjoyed your coffee a lot.

Note: Because of short of time, I have covered only basic of docker and there are lot more topics to cover like Docker Networking, Docker Volumes, How to write docker file, what is docker compose, what is docker swarm and kubernetes etc.

Hope you found this post to be informative. Feel free to share it across. This really means a lot to me as it is my first social media post.

Before you say goodbye…

You can connect me on LinkedIn

Thank you so much for taking your precious time to read this post.

--

--

Gourav Mukhija

Nature lover, Full Stack Web developer and creator of IG: traveler_india