Docker Basic Tutorial

Basic Docker tutorial | Docker Cheet Sheet | Docker Guide | Learn Docker

Docker Introduction

Docker tool was introduced in order to make it easier for you to create, deploy, and run applications by using containers. Containers provision you the packaging of your application with all the important components it requires, like libraries and other dependencies, and ship them all out as one package. Due to which you as a developer can be assured that your application will run on any other machine.

Docker Architecture

We will start from understanding Docker’s Architecture and main aspects in it, then we will continue with the important commands required for docker installation, build, push, run, ship etc. operations.
DevOps architecture consists of these 5 main entities which are Registry, Images, Containers, Docker Daemon and Client.

  • Registry: It hosts the public and official images, docker registry that we use is Dockerhub.
  • Images: It can be downloaded from the registry directly or implicitly when starting a container.
  • Containers: They are basically instances of the images, multiple containers for a single image is possible.
  • Docker daemon: Daemon creates, runs and monitors the containers along with building and storing the images.
  • Client: Client talks to daemon via http.

Docker Orchestrate

Orchestration is used for managing the container’s lifecycle especially in dynamic environments.
It is used for controlling and automating many tasks for the container. There are many Docker Orchestration tools like Docker Swarm, Kubernetes etc, and we’re using Docker Swarm commands below, let’s check the commands used for Docker Orchestration.


    To initialize swarm mode and listen to a specific interface.

       #  Docker swarm init --advertise-addr

    Join an existing swarm as manager node

        Docker swarm join --token<manager-token>

    Join a swarm as a worker node

        Docker swarm join --token<worker-token>

    List all the nodes in the swarm

        Docker node ls

    Create a service from an image on the existing port and deploy 3 instances

        Docker service create --replicas 3 -p 80:80 name -webngix

    List services running in swarm

       #  docker service ls

    Scale a service

        # docker service scale web=5

    List tasks of a service

        # docker service ps web

Docker Build

Build command is used for building images from a docker file. Let’s check some of essential docker build commands.

    To build the image from the docker file and tag image

        Docker build -t myapp :1.0

    List all images that are locally stored

        Docker images

    Delete an image from the docker store

        Docker rmi alpine: 3.4

Docker Run

Run command is used for creating a container from a specified image. Check these run commands for your reference.

    To create and run a command:
        Docker run –name container_name docker_image
    Flags used:
        -d detach container on start
        -rm remove container once it stops
        -p publish host ip and host port to the container por
        -v define and share volume across containers
        –read-only sets it to read only permission
Docker Ship

Docker gives you the capability of shipping your application container anywhere, on any platform, let’s check some commands for it.

    To pull an image from the registry
        Docker pull alpine:3.4.
    Retag a local image with a new image name
        Docker tag alpine:3.4 myrepo/ myalpine:3.4
    Log in to a registry
        Docker login
    Push an image to a registry
        Docker push myrepo/ myalpine:3.4

Docker Clean up

To prevent from wasting the resources, you must know how to clean, we are providing few essential commands for same.

    To clean unused/dangling images:
        Docker image prune
    To remove images not used in containers
        Docker image prune -a
    To prune the entire system
        Docker system prune
    To leave a swarm
        Docker swarm leave
    To remove a swarm
        Docker stack rm stack_name
    To kill all running containers
        Docker kill $ (docker ps -q)
    To delete all stopped containers
        docker rm $(docker ps -a -q)
    To delete all images
        docker rmi $(docker images -q)

Docker  Services

Let’s take a sneak peek over the commands used for viewing the running services, run the services, to view all service logs and to scale the services.

    List of all services running in a swarm
        Docker service ls
    To see all running services
        Docker stack services stack_name
    To see all service logs
        Docker service logs stack_name service_names
    To scale service across qualified nodes
        Docker service scale stack_name_service_name= replicas
Interaction with a container

Let’s check how to make an interaction with a container

    Run a command in the container
        Docker exe -ti container_name
    Follow the container logs
        Docker logs -ft container name
    Save a running container as an image
        Docker commit -m “commit message” -a “author” container_name username/image_name: tag

Docker Important terms

Some of the important terms while using docker container.

  •     Docker Layer: Read-only files to provision the system
  •     Docker Image: Read only layer that is the base of the image
  •     Docker Container: A runnable instance of the image
  •     Docker Registry/hub: Central place where images live
  •     Docker machine: A VM to run docker containers
  •     Docker compose: A VM to run multiple containers as a system.