Introduction to Docker - Quick Start Tutorial

This article begins with an introduction to Docker and then walks through a quick tutorial on how to create a basic Docker image and run a Docker container.
 
Introduction to Docker
Docker is an OS-level virtualization platform that runs on Unix/Linux and Windows hosts. Docker encapsulates applications and services into portable virtualized environments. With Docker, applications and services can be maintained and deployed using standardized images, rather than managing separate development environments on individual servers. The environment is standardized by creating a Docker image and it is run inside Docker containers on the host. A docker container is isolated from the host environment and does not modify the host configuration. Isolation using containers allows for improved portability and provides an additional layer of security. Docker is often used by Development Operations teams to streamline the Software Development LifeCycle. A common use case is to set up a “Dockerized” environment that will run seamlessly on multiple platforms such as a a local development laptop, QA server and Production server.

Shipping Container Analogy
The term ‘Docker’ is an analogy in reference to industrial shipping containers. The size and shape of the container is standardized, therefore any item can be placed inside of it and it makes no difference to how the container is stacked on the boat and how it is delivered to its destination. Docker provides the tools to build portable, encapsulated environments that are standardized for configuration, deployment and management.

Docker Images
Docker Images are the fundamental building blocks of the “Dockerized” environment. A Docker Image is a set of instructions that are run to install and configure the environment. A set of instructions run commands to install programming languages, packages, libraries, web servers, database servers, proxies, tools and other development tools. These are essentially the commands that would be run when setting up a development environment manually, but with a Docker Image the installation and configuration steps are all standardized, automated and portable.Docker Images are layered filesystems that start with a “Base” image. A Base image often starts with an OS and optionally a programming language or database system. Images that are officially supported by Docker are stored and shared on Docker Hub Image Repository.
Multiple Docker Images can be built for a single application to isolate services into separate modular components. For example, to set up an environment for a NodeJS application, separate images can be created for the web server, proxy and database service. Docker Compose allows multiple containers to run based on multiple images and interconnect multiple services. This article will review a basic setup of a single image and a single container only.

Docker Containers
A Docker container is an instance of an image. A Docker container is run from an image. A writeable container layer is created on top of the image.
A Docker container is does not have persistent storage built-in by default, therefore any data created inside the container is lost when the container is removed. To achieve data persistence, storage volumes can mounted in at runtime. The data can be modified inside the container and then will be updated via the mount point automatically.

Docker Basics Tutorial
This example shows how to create a basic docker image for a python environment that runs on the apline linux distribution.

Prerequisites:

Install Docker Desktop on a Linux/Unix or MacOSX host.

Step 1: Create a Dockerfile in a directory
Create a new directory to store the Dockerfile:
 
mkdir docker_basics
 
Create a new Dockerfile inside the docker_basics directory:


cd docker_basics
vim Dockerfile

Dockerfile contents:

# Base Image
FROM python:2.7-alpine
 
# Install curl
RUN apk add –no-cache bash curl tzdata
 
# Install Python modules
RUN pip2.7 install requests
 
# Start in default source directory
WORKDIR /usr/src/

Create a new directory nested inside docker_basics to store application code:
 
mkdir example_scripts

Create an example python script inside the example_scripts directory:

cd example_scripts
vim example.py

example.py contents:

import requests
print(‘Hello! Basic Docker environment with Python worked correctly.’)
Directory structure for this example:

docker_basics
docker_basics/Dockerfile
docker_basics/example_scripts
docker_basics/example_scripts/example.py

Step 2: Build a Docker image
Navigate into the docker_basics directory that contains the Dockerfile. Build the image by running the docker image build command. The first parameter names the new image, in this case the new image is named img_docker_basics. The second parameter is the path to the directory that contains the Dockerfile, in this case it is the current directory. Watch the output as the image is building, each step is carried out in sequential order as per the image instructions. A new image is built and tagged with latest.
 
docker image build -t img_docker_basics .
 
Step 3: Run a Docker container
The next step is to run a docker container from the image. The -it option allocates a tty for the container. The -v option mounts a volume from the host system into the container. In this case the current directory “($pwd)” is mounted into /usr/src. Also, specify which shell to open in the container with by passing in ‘bash’ or ‘sh’ as the last parameter. Read the Docker docs for more information on the docker run command.
 
Navigate to the example_scripts directory, run the container, mount in the contents of the current directory using the volume option and open a shell using the sh command as the last parameter.

cd example_scripts
docker run -it -v “$(pwd)”:/usr/src img_docker_basics sh

Expected result:
The docker container will be running an instance of the python environment in alpine linux and a terminal shell will be open ready to type in commands. NOTE: Any data that is not specifically mounted into the container (either in the image definition or the run command) will be lost when the container exists, since a Docker container does not have persistent data storage.The contents of the example_scripts directory was mounted into the /usr/src directory.

/usr/src # ls
example.py

The python environment is running the specified version.

/usr/src # python –version
Python 2.7.15

Run the python script. The print message should appear.

/usr/src # python example.py
Hello! Basic Dockerized python environment worked correctly.

Since the example_scripts directory was mounted into the container as a volume at runtime, any changes made to files in example_scripts will automatically update at the source. As a basic test, change the print message in the example.py inside the container. The file will be changed accordingly at the source.

Next steps
Install additional python packages by adding commands to the Dockerfile, building a new image and running a new container. Check the running containers by using docker ps and read the standard out of the container by running docker logs <container name>. Also, make sure to clean up any unused containers and images. The environment can be moved to any system with Docker and it will be much simpler than re-building the environment again from the beginning.

Learn more
Learn more about the powerful features of Docker by reading the docs and practicing tutorials.

https://docs.docker.com
https://docs.docker.com/get-started/
https://docs.docker.com/develop/