Introduction to Docker


  • It is an isolated environment for running an application.
  • it is a light weighted process.

Virtual Machine

  • It is an abstraction of a machine (physical machine). abstraction in the sense it has separate OS and file system.
  • It uses Hypervisor.

Docker Architecture

Docker follows Client Server Architecture. It has mainly two parts client and Server and all the transmissions happens using Restful api.

Dockerfile Syntax

# first line of the Dockerfile contains FROM tag (which image we'll use) 
# after the colon(:) we write OS distribution.
FROM node:alpine

# Now we copy all our files to a App directory 
COPY . ./App

# Now it depends on what of project or requirements you are working on
# if it is node application then
CMD node app.js

Dockerfile can be alot more complicated than this but this is general form. How Dockerfile will look like.


Useful Docker Commands

  • To check docker version :
docker version 
  • To build an image from dockerfile :
docker build -t <tag_name> .

-t → is used to give image a tag so that it will be easy to use it later.

Note : If we don’t give a tag name then container name will be random.

  • To see all the docker images :
docker image ls

here is one shorthand for this :

docker images
  • To pull an image from Docker Hub :
docker pull <image_name>
  • To run an Image :
docker run <image_name>

If image is not present in the locally then this command will download it from hub and then run it.

  • To see all the running container :
docker ps

This command will only show you all the running container but to see all the container :

docker ps -a
  • To run a container using Image in interactive mode :
docker run -it <image_name>

-it → it will map the container’s STDIN, STDOUT, STDERR to your’s Terminals so that you can interact with it.

  • If you want to Ignore some files or directory to move into the container then use .dockerignore file. it is similar to .gitignore .

  • To remove images :

docker image rm <image_name1> <image_name2>
  • To remove all images :
docker rmi -f $(docker images -a -q)
  • To remove dangling images :
docker image prune
  • To publish an image to Docker Hub :
docker image <image_name> <repo_name>
docker push <repo_name>
  • Saving and loading Image from local System :
docker image save -o <tar/zip file> <image_name>
docker image load -i <tar/zip file>
  • To run a container in detached mode :
docker run -d <image_name>
docker run -d --name <container_name> <image_name>
  • To see the logs of a Container :
docker logs <container_name>
  • Publishing Port :
docker run -d -p <host_machine_port>:<docker_port> --name <container_name> <image_name>
  • Execute a command inside a running container :
docker exec <container_name> <cmd>
  • Start and Stop a container :
docker start <container_name>
docker stop <container_name>
  • Remove a container :
docker rm <container_name>
docker rm -f <container_name>
  • Remove all stopped container as well as Image :
docker system prune

Docker Volumes

Two docker container have separate file system and don’t share files to achieve this we have to use volumes.

  • We can create volume by following command :
docker volume create <volume_name>
  • To see the volumes :
docker volume inspect <volume_name>
docker run -d -v <volume_name>:/app/data <image_name>
  • Copying file from container to host machine :
docker cp <container_path> <host_path>
  • Map a host directory to container directory :
docker run -d -v $(pwd):/app <image_name>

docker-compose file

docker-compose is generally used when we have multiple applications and we want to combine them then docker will take care of the networking and ports itself and we just have to write one single command and our application will be up and running.

Example : Like we have a frontend in React and for PostgresDB and Node.js Backend and Redis for cache. In these kind of cases we used docker-compose.

Simple docker-compose.yaml

#version should be a string (number inside the double quotes)
version : "number"

services :
		web :
			build : ./frontend
			ports :
					- 3000:3000
		api :
			build : ./backend
			ports :
				 - 3001:3001
			environment :
         - DB_URL = ""
    db :
      image : mongo:4.0-xenial
			ports :
				 - 27017:27017
			volumes : 
				 - volume : /data/db
volumes :
			name : volume

Docker Stop vs Docker Kill

  • Rebuild the image of docker-compose :
docker-compose up --build
  • Create an image from container :
Harsh Mishra
Written by
Harsh Mishra

Senior Data Engineer