Docker Installation & Basic Commands

Introduction

Docker is open source platform for developing, shipping and running your applications. It also enables you to separate your applications from the infrastructure, this results in faster delivery of the software.
Docker comes with two editions

  • Community Edition(CE)

This is mainly used by developers & ideal for small teams, experimenting with container-based apps e g. Dev and Test environment of the applications.

  • Enterprise Edition (CE)

This is mainly used for Production environments & for the big IT firms. Look here for more info
We are going to perform the docker installations and execute basic commands of Docker. For more information of Docker Editions, click here

Installations & Configuration

In this section, we are going to install Docker CE on various Operating System which widely used like Ubuntu, CentOS, RHEL and Windows etc.

Prerequisites

  • 64-bit bit Operating System
  • Non-root user with sudo privileges

Installation

Ubuntu [16.04]

  1. Docker package is available in official Ubuntu 16.04 repository, might not be the latest one. So, we will update repo to get the latest repository.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
  1. Add the repository to the APT sources
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
  1. Now, update the package database with newly added docker repository
sudo apt-get update
  1. Check the docker repo, which we are going to install.
apt-cache policy docker-ce

Notice: docker-ce is not installed.  The docker-ce version might be different in the newly added repository

  1. Finally install docker using following command. To install the latest docker version, we can remove the version string parameter.
sudo apt-get install docker-ce=<VERSION_STRING>
  1. Docker is now be installed, the demon process enabled to start on boot. Check that it’s running:
sudo systemctl status docker

RHEL & CentOS 7

  1. Update the official repository
sudo yum update
yum install docker
  1. After installation, we need to start daemon, check the status & enable it.
sudo systemctl start docker
sudo systemctl status docker
sudo systemctl enable docker
sudo chkconfig docker on

Windows

  1. Download the docker installer file from here. Double click the installer
  2. Follow the install wizard to accept the license, authorize the installer, and proceed with the install steps on the Installation wizard.

Configuration

Here we are going to configure to execute docker commands without sudo privileges. By default docker commands runs with sudo privileges, so every time we need to add prefix sudo to execute command. If we don’t use the sudo privileges, we get the following error

Ubuntu [16.04]

  1. To avoid using sudo, first we will check from which user you are logged in & add that particular user to the docker group
sudo usermod -aG docker $(USER)
  1. Now, we will add a username of a user that we are not logged in to the docker group
sudo usermod -aG docker username
  1. Let’s verify which version of docker is installed
docker version

 

RHEL & CentOS 7

  1. To avoid using sudo, first we will check from which user you are logged in
sudo usermod -aG docker $(whoami)
  1. Now, we will add a username to the docker group
sudo usermod -aG docker username
  1. Let’s verify which version of docker is installed
docker version

 

Hands On with Docker

  1. To view entire System information
docker info
  1. To pull the image from docker hub below is the docker command to pull hello-world image from docker hub
docker pull centos
  1. To search image on docker hub, following is the command
docker search image-name
  1. To run the hello-world image with simple run command
docker run hello-world

Note: here container runs, displays the text messages and immediately exist itself. However container should be used in interactive way

  1. Let’s run the centos image in interactive mode, combination of the -i and -t switches gives you interactive shell access into the container
docker run –it centos
  1. List the running docker container
docker ps
  1. List all active & and inactive containers use –a
docker ps -a
  1. To view latest container, we have created
docker ps -l
  1. To stop the running container
docker stop container-id / container name
  1. To delete / remove the inactive or active container
docker rm  container-id / container name
  1. To remove one or more images
docker rmi image-id / image name
  1. To display the live stream i.e. usage statistics of containers
docker stats container-id / container name
  1. Pushing Docker images to docker repository
docker login -u docker-registry-username
  1. After successful authentication, we can push our image to Docker Hub
docker push docker-registry-username/docker-image-name

Contributed by :
Sonali Shah
https://www.linkedin.com/in/sonali-shah/