Understanding Docker Basics

Mahesh Oruganti
5 min readMar 12, 2019

--

The objective of this post is to explain Docker Basics and usage of Docker commands.

Intro to Docker

  • Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers.
  • The use of Linux containers to deploy applications is called containerization.
  • Containerization is increasingly popular because containers are:

Flexible: Even the most complex applications can be containerized.

Lightweight: Containers leverage and share the host kernel.

Interchangeable: You can deploy updates and upgrades on-the-fly.

Portable: You can build locally, deploy to the cloud, and run anywhere.

Scalable: You can increase and automatically distribute container replicas.

Stackable: You can stack services vertically and on-the-fly

Containers and virtual machines

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

What are Containers?

  • Containers are the building blocks of the containerization world and allow you to package only the things you need for your application or service without creating a whole operating system, like Virtual Machines.
  • They are faster and more lightweight.

Containers also give you control over your environment because you can specify exactly what you want each container to look like by using blueprints called Images

What are Images?

  • Images are the specs or details used to create containers.
  • Just like how images for VMs let you spin up a Windows instance of Vista or XP, or Linux images let you pick between Ubuntu, Debian, or other versions, images for Docker let you specify which services, libraries, or dependencies you need for a container

Services Without Docker:

Services With Docker:

Installing Docker

Install docker engine from downloading from docker web site , https://docs.docker.com/docker-for-mac/install/

Verify Docker Installation

After installation done, please verify that docker installed correctly by running docker version command on command line. You would be able to see output below mentioned.

Launch Docker Engine

Launch the docker Engine, and you would be able to see below screen on your desktop when you click on docker icon.

Docker Account signup:

You need to have sign up docker account to pull images from docker hub.

  1. go to https://hub.docker.com/
  2. signup or create account.

Pull Images from Docker:

Search for existing images in the docker hub/repository and pull images into your system using docker pull command . below is example to pull “dockersamples/static-site” image.

docker pull dockersamples/static-site

Docker Commands and it’s Usages:

List images:

docker image ls

$ docker images ls

dockersamples/static-site latest f589ccde7957 2 years ago 191MB

Run a command in a new container

docker run

docker run dockersamples/static-site

List containers:

docker ps

Display a live stream of container(s) resource usage statistics

docker stats

Some of the useful commands :

docker stop <CONTAINER_ID> : Stops container.

docker stats : used to observe CPU, memory usage of containers.

docker inspect <IMAGE> : gives more details about images.

docker run -it <Container> /bin/sh : Running the run command with the -it flags attaches us to an interactive tty in the container. Now you can run as many commands in the container as you want

docker rm < Container_ID> : removes container

docker run –help

docker build : Build the image

docker push : Push your image to Docker Cloud

List Docker containers (running, all, all in quiet mode)

docker container ls

docker container ls — all

docker container ls –aq

Docker Networking: (Usage: docker network COMMAND)

docker network

docker network ls

docker network inspec

docker info

Docker Compose and YML Overview

  • docker-compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration
  • You define a single yml file with the services you want and then use the following command to create all of it at once.

docker-compose up –d

docker-compose -f docker-compose.yml up -d

Note: If you want to run your services in the background, you can pass the -d flag (for “detached” mode)

docker-compose -f docker-compose.yml down (to stop the services)

Below is the example of docker-compose.yml.

docker-compose.yml
  • Version: “3” — as of this recording, this is the latest version of docker-compose files
  • Services (or containers): — this is where we list the images we’ll be using and their configuration
  • volumes: mount host path to service with <HOST:CONTAINER> format.
  • ports: publish port with <HOST:CONTAINER> format.
  • image: determine which container will be used to start container.

References:

https://docs.docker.com/engine/reference/commandline/docker/

https://docs.docker.com/compose/overview/

About Me

This is Mahesh Oruganti working as QA Automation Engineer at Dotdash. Happy Learning. Thanks.

--

--

Mahesh Oruganti
Mahesh Oruganti

No responses yet