Author avatar

Kimaru Thagana

Getting Started with Docker

Kimaru Thagana

  • Sep 17, 2020
  • 6 Min read
  • 813 Views
  • Sep 17, 2020
  • 6 Min read
  • 813 Views
IT Infrastructure
Container Management
Docker
Containers

Introduction

Containerization can be defined as virtualization at the operating system (OS) level to allow deploying and running of distributed applications without the need for entire virtual machines (VMs) per application. This is economical in terms of computing resources since multiple isolated applications can run on a single host. Containerization became popular among development teams because containerized applications always run the same way regardless of host infrastructure.

Docker is a product that offers hardware virtualization at the operating system (OS) level. This ability allows developers to package software and its dependencies in order to ship it as containers.

An image is a software package that includes the source code and all required dependencies for the software to run. In Docker, an image is generated by the build command.

A container is a running instance of an image. In Docker, containers are executed using the run command.

This guide introduces you to Docker and how to use it to package and run applications. It assumes you are familiar with the LINUX environment and can comfortably interact with the Linux bash environment.

Docker

Docker also offers a service called DockerHub that allows sharing and managing containers among developers. This can be referred to as a "GitHub" for container images because it shares some similarities with the code repository platform, such as uploading images via push commands and downloading images via pull commands.

In the event that a developer wishes to run more than one container in a certain order of dependency, Docker offers a service called docker-compose. The tool is used for defining and running multi-container Docker applications. An example is a Django application running behind an NGINX proxy on a Postgres database. In this example, there is the Postgres container, the Django app container, and the NGINX container.

Installation and Setup

This guide will explore setting up Docker on an Ubuntu machine. For Windows, follow this guide, and for MacOs, follow this guide.

To download and set up Docker, run the command below in your terminal.

1
2
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
bash

Basic Docker Commands

Below is a list of common Docker commands. More commands and their functionalities and options can be found on the official Docker site.

docker --version: Used to identify the currently installed version of Docker on the host system

docker ps: Used to list all running containers

docker images: Used to list locally stored images

docker login: Used to login into your Dockerhub account

docker push <username/imagename>: Used to upload an image to your remote Dockerhub repository

docker pull <imagename>: Used to download images from Dockerhub

docker build <path/to/docker/file>: Used to generate an image from the specified Docker file

docker run -it -d <imagename>: Used to create a container from the built image

docker stop <container id>: Used to halt the running of the defined container

docker rm <container id>: Used to delete a stopped container

docker rmi <imageid>: Used to delete the specified image ID from local storage.

Sample Script

To demonstrate Docker in action, run a sample Flask script within Docker.

1
2
3
4
5
6
7
8
9
from flask import Flask
server = Flask(__name__)

@server.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
   server.run(host='0.0.0.0')
python

Copy the above code block into a file and name it app.py.

The next step is to create the Dockerfile. The logic within the file should be as follows.

  1. Set up base image

  2. Set up working directory

  3. Copy source code to Docker environment

  4. Install dependencies

  5. Run program

Copy the code below and save it in a file named Dockerfile.

1
2
3
4
5
6
7
8
# set base image (host OS)
FROM python:3.8
# set the working directory in the container
WORKDIR /code
COPY . .
RUN pip install flask
# command to run on container start
CMD [ "python", "./app.py" ]
dockerfile

With both files set up, you are ready to build and run your image. To build your image, run the command:

1
docker build -t myimage .
bash

After the Docker image is built, it is time to run it. Since this is a Flask application that runs on a certain port, provide the port number via the ports(-p) flag.

1
docker run -p 5000:5000 myimage
bash

The app is now running at the localhost address http://0.0.0.0:5000.

As a challenge, push the newly created image to your Dockerhub repository, download it via the pull command on a different machine, and run the image. You will notice that you will not need setup instructions. You only need to have Docker installed.

Conclusion

Containerization using Docker smoothens the deployment process. This skill is highly useful in positions such as DevOps, team lead, quality assurance (QA), or testing engineer. In team management, the team lead uses Docker scripts to set up a project, which ensures that new and onboarding team members spend less time setting up and more time doing actual development.

If not carefully managed, Docker images can grow abnormally large. To manage this, learn more about slim base images and the use of dockerignore.

To further build on this guide, learn more about docker-compose, Docker Swarm, and Kubernetes for multi-container applications, orchestration, and management.

14