Satak blog

Logo

Blog posts from Satak

Satak's GitHub page

11 January 2020

Docker Basics

by Satak

Introduction

I guess you have heard words like Docker and containers or even Kubernetes, but what are they and where and why would you use them? In this blog post I try to explain the basics and show a step-by-step guide how to setup your own Docker environment and how to build and publish your first Docker container.

What is Docker

Docker is a leading technology platform to run containers, so much that when someone talks about containers, he or she means Docker containers. Docker Inc., the company behind Docker technology, offers two Docker versions: The free Docker Community version, and the commercial version Docker Enterprise. In this post we are using the free version which is the most common way to run Docker containers, even in the professional and production systems and environments. Docker is a Linux native software so the easiest way to run Docker is to run it on Linux systems, but there are perfectly fine solutions to run Docker on Windows and macOS (Docker Desktop).

So what are containers then

Container is a way to run applications in an isolated environment, like a mini virtual machine, that packages up code and all its dependencies. They are much more lightweight and faster to run than traditional Virtual Machines. Smallest containers only takes couple MBs of disk space and use minuscule amount of resources. To be able to run containers you need a container run environment/platform like Docker.

When you start working with containers you will come across a term base image, which is a container image that you use to build your own image. You can of course build your own base images but often it’s not needed since companies and communities takes care of the base images and they are usually up to date. One of the most common lightweight base images is Alpine Linux (https://alpinelinux.org). One of the best parts in containers is the fact that they are made of layers. So you can use a static base image that you download once and all other changes to your own application are made in your own top container layer, so it speeds up the deployment and build a lot, since you don’t have to download the base image after it’s once downloaded.

Docker Hub

Docker offers a free and open repository to store your Docker container images called Docker Hub. First you must register your account to Docker Hub and after that it’s ready to be used for your own Docker container builds. It works so that when you build your containers you can then publish those as an image to the Docker Hub and later pull those images to re-use those or have any published image as a base image for you later builds. There are many container repositories but Docker Hub is the most used one and a good starting point.

Kubernetes and beyond

Kubernetes (https://kubernetes.io) is one of the leading orchestration platforms to run and manage multiple containers in a production environment. Many cloud platforms like AWS, GCP and Azure offers their managed Kubernetes platform. Helm (https://helm.sh) is one of the most popular package manager for Kubernetes and it’s often used with Kubernetes. There are also serverless container platforms where you don’t even see the actual Kubernetes cluster like GCP Cloud Run (https://cloud.google.com/run). Docker Desktop comes with a lightweight Kubernetes platform where you can practice and test deploying containers to Kubernetes.

Setup Docker Desktop

This guide is using Windows 10 as a workstation where we are going to install Docker Desktop so we can run Docker containers on our Windows 10 workstation.

Docker Hub sign in Docker desktop download

Docker desktop install

Docker desktop enable Hyper-V

Docker desktop BIOS error

Docker desktop command

Running Containers

Docker Hello World

Docker ps command

Docker run nginx

Browser

Docker commands

Building Docker Containers

Now when we know basic Docker commands we can start to build our first docker image and eventually publish it to our Docker Hub.

#!/usr/bin/env python3

print("Python says hello from Docker container!")
# lines with a hashtag are just comment lines and are not executed

# This first actual line here defines the base image
# here we use the latest official python image where we based our own image
FROM python:latest

# command to copy our local main.py file to the container's root
COPY main.py /

# when the container starts it will run this command
CMD [ "python", "./main.py" ]

Docker build

Docker Desktop Docker Hub sign in

tags: docker - containers - virtualization