L3 01 Docker Howto Basic Guide
L3 01 Docker Howto Basic Guide
L3 01 Docker Howto Basic Guide
Developed by
1
Document Control
Date Version Notes
1.0 Initial guide development
Project Members
Last Name First Name Institution Country
Ghanmi Nidhal Institute of Pasteur , Tunis Tunisia
Lukyamuzi Edward Uganda Virus Research Institute Uganda
Maslamoney Suresh Computational Biology Division, University South Africa
of Cape Town
Meintjes Ayton Computational Biology Division, University South Africa
of Cape Town
Oloyede Emmnauel National Biotechnology Development Nigeria
Agency
Wamala Timothy Uganda Virus Research Institute Uganda
2
Project Members .................................................................................................................................. 2
Abbreviations ........................................................................................................................................ 5
1. What are Containers? ...................................................................................................................... 5
1.1 Types of Containers ...................................................................................................................... 7
1.1.1 Popular Container Providers 7
1.2 What is Docker .............................................................................................................................. 7
1.3 When to Use Docker ..................................................................................................................... 7
1.4 When Not to Use Docker .............................................................................................................. 8
1.5 Core Components of Docker ......................................................................................................... 8
1.6 Docker Terminology ...................................................................................................................... 9
1.7 Docker Editions ........................................................................................................................... 10
2. Installing Docker CE on Ubuntu 20.04/20.04 LTS ............................................................................. 10
2.1. Install Using Advanced Packaging Tool (APT) ............................................................................ 10
2.2 Install Using Snap ........................................................................................................................ 11
3. Getting Started.................................................................................................................................. 12
3.1 Basic Commands ......................................................................................................................... 12
4. Docker Images................................................................................................................................... 14
5. Docker Registry ................................................................................................................................. 15
6. Docker Networking ........................................................................................................................... 16
6.1 Bind Host Port to container Port................................................................................................. 16
7. Docker Volumes ................................................................................................................................ 16
7.1 Get Started with Volumes ........................................................................................................... 16
8. Docker Applications .......................................................................................................................... 17
8.1 Static site on nginx server from Docker ...................................................................................... 17
3
Background
This guide is produced by the Computing & Infrastructure working group under the Pipelines and
Computing work package. It is intended to serve as a self-sufficient support guide on all major
aspects around docker technology for system administrators across the H3ABioNet and its
collaborating partners. All commands and screenshots are based on the Ubuntu 20.04 Operating
System (OS). The guide has been designed to be effective as a standalone self-guided walkthrough
of installing, deploying and management of docker containers- while the guide is written in an easy
to read, user-friendly way, it assumes familiarity with navigating around an Ubuntu 20.04 Linux
system. If you are not familiar with Linux, refer to our “LINUX: GETTING STARTED” guide on the
https://h3abionet.org/tools-and-services/technical-guidelines website. Following the instructions in
this guide will give the reader hands on experience with installing the Docker community edition,
create a basic Docker container with a static web page. It includes useful commands to manage
multiple containers and images.
It is recommended that the reader first reads this guide in its entirety before following the
step by step instructions.
4
How to read this guide
• General text describing each section and commands are written in the Arial font, size
11. This text is to be read to understand which skills will be gained in the following
section and to describe the command operation.
• When noting a variation to a default command or to note a point or warning, the
default text will be highlighted in yellow.
• When noting a tip, for example, a command that can be run in multiple ways
producing the same output, this text will be highlighted in green.
• When giving examples of actual expected output, this text will be highlighted with a
grey background.
• When listing a command that is to be run by the reader to produce an output, the
command will be in light blue italics.
Abbreviations
Abbreviation Description
OS Operating System
cli Command line interface
gui Graphical user interface
dhcp Dynamic host configuration protocol
IP Internet Protocol
VM Virtual Machine
5
VMs are great at providing full process isolation for applications: there are very few ways a problem
in the host operating system can affect the software running in the guest operating system, and vice-
versa. But this isolation comes at great cost — the computational overhead spent virtualizing
hardware for a guest OS to use is substantial.
Containers take a different approach, by leveraging the low-level mechanics of the host operating
system, containers provide most of the isolation of virtual machines at a fraction of the computing
power.
Essentially, Docker is a container-based system for your applications. If you’re used to the concept of
virtual servers, Docker provides further levels of abstraction for your application. Here’s a visual
representation of how containers differ from VMs:
6
1.1 Types of Containers
Linux Containers (LXC) — The original Linux container technology is Linux Containers, commonly
known as LXC. LXC is a Linux operating system level virtualization method for running multiple isolated
Linux systems on a single host.
Docker — Docker started as a project to build single-application LXC containers, introducing several
changes to LXC that make containers more portable and flexible to use. It later morphed into its own
container runtime environment. At a high level, Docker is a Linux utility that can efficiently create,
ship, and run containers.
So, what exactly is Docker? Here’s how Docker themselves describe it:
Learning new technologies: To get started with a new tool without spending time on installation and
configuration, Docker offers an isolated and disposable environment. Many projects maintain Docker
images with their applications already installed and configured.
Basic use cases: Pulling images from Docker Hub is also a good solution if your application is basic or
standard enough to work with a default Docker image. Cases such as hosting a website using a LAMP
7
stack or using a reverse proxy have an official or well-supported image available on DockerHub. If the
default configuration in these images is acceptable for your needs, then pulling the image can save a
lot of time that would otherwise be spent setting up your environment and installing the necessary
tools.
App isolation: If you want to run multiple applications on one server, keeping the components of each
application in separate containers will prevent problems with dependency management.
Developer teams: If you have developers working with different setups, Docker provides a convenient
way to have local development environments that closely match the production environment, without
needing to ssh into a remote box.
Your app is complicated and you are not/do not have a sysadmin. For large or complicated
applications, using a pre-made Dockerfile or pulling an existing image will not be sufficient. Building,
editing, and managing communication between multiple containers on multiple servers is a time-
consuming task.
Performance is critical to your application. Docker shines compared to virtual machines when it
comes to performance because containers share the host kernel and do not emulate a full operating
system. However, Docker does impose performance costs. Processes running within a container will
not be quite as fast as those run on the native OS. If you need to get the best possible performance
out of your server, you may want to avoid Docker.
Security is critical to your application. As mentioned above, keeping the different components of an
application in separate containers provides some security benefits, since a compromise in one
container can’t easily affect the rest of your system. However, Docker’s containerization approach
raises its own security challenges, especially for more complicated applications. These issues are
solvable but require attention from an experienced security engineer.
Multiple operating systems. Since Docker containers share the host computer’s operating system, if
you want to run or test the same application on different operating systems, you will need to use
virtual machines instead of Docker.
Clusters. Docker containers on separate servers can be combined to form a cluster with Docker
Swarm. However, Docker does not take the place of provisioning or automation tools such as Ansible,
SaltStack, and Chef. In addition, Docker support Kubernetes, hinting that Docker Swarm may not be
sufficient as a stand-alone cluster manager.
● Server
● REST API
● Client
8
Image Source: https://docs.docker.com
The Server runs a daemon known as dockerd (Docker Daemon), which is nothing but a process. It is
responsible for creating and managing Docker Images, Containers, Networks and Volumes on the
Docker platform.
The REST API specifies how the applications can interact with the Server, and instruct it to get their
job done.
The Client is nothing but a command line interface, that allows users to interact with Docker using the
commands.
Docker Images and Docker Containers are the two essential things that you will come across daily
while working with Docker.
In simple terms, a Docker Image is a template that contains the application, and all the dependencies
required to run that application on Docker.
On the other hand, as stated earlier, a Docker Container is a logical entity. In more precise terms, it is
a running instance of the Docker Image.
9
Docker Hub also allows us to store and distribute our custom images as well if we wish to do so. We
could also make them either public or private, based on our requirements.
The Community Edition is suitable for individual developers and small teams. It offers limited
functionality, in comparison to the Enterprise Edition.
The Enterprise Edition, on the other hand, is suitable for large teams and for using Docker in
production environments.
The Enterprise Edition is further categorized into three different editions, as listed below:
● Basic Edition
● Standard Edition
● Advanced Edition
First, update your existing list of packages: the system needs to be updated to make it safer and
reliable to install Docker.
Once we have updated the system, we need to install some necessary packages before we are ready
to install Docker.
This enables us to use the officially supported method of the installation making the installation
process much easier.
First add the GPG key for the official Docker repository to your system:
10
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
Next, update the package database with the Docker packages from the newly added repo:
Make sure you are installing from the Docker repo instead of the default Ubuntu repo with this
command:
Check the docker status: good idea to check that it’s running
Add Docker service to the system startup: so that it will start automatically on system boot.
$ docker --version
11
$ snap info docker
Next is to install docker using one of the channels (deployment packages) available
It prints service name as 'docker.dockerd' that is different from service name when you deploy
without using Snap package
Start docker
$ sudo snap start docker
Stop docker installed using Snap package
$ sudo snap stop docker
To uninstall or remove docker installed using Snap package
$ sudo snap remove docker
3. Getting Started
3.1 Basic Commands
Info command: gives information about the docker setup on your machine/vm
$ docker info
Run Container and enter its shell: use -h command line parameter to specify a container name.
12
Run Container with Networking mode: use the flag --net
$ docker ps -a
Inspect a Container
docker rm CONTAINER_NAME
Detach from a Container
docker run -t -i → can be detached with ^P^Q and reattached with docker attach
docker run -i → cannot be detached with ^P^Q; will disrupt stdin
docker run → cannot be detached with ^P^Q;
can SIGKILL client; can reattach with docker attach
13
$ docker network ls
4. Docker Images
Show images
$ sudo docker images
Specifying a Variant
$ sudo docker run -i -t ubuntu:20.04 /bin/bash
Pull an Image
$ sudo docker pull ubuntu
$ mkdir docker-file
$ cd docker-file/
$ touch Dockerfile
Creating a image
14
$ sudo docker build -t="test/my_nginx"
5. Docker Registry
Docker allows to bundle and push the customized images on the docker hub or locally hosted docker
registry. Images can be pushed or pulled from this registry.
Create official Docker Hub account
$ sudo docker login
Username: username
Password:
Email: email@example.com
WARNING:login credentials saved in /home/username/.dockercfg.
Account created. Please use the confirmation link we sent to your e-mail to activate it.
Search publicly available images in the Docker hub registry: e.g with keyword “centos”
$ sudo docker search centos
Push customized image to Docker repository: make sure repository name meets the username of the
docker hub account in order to push the images
$ sudo docker push username/newimage
15
6. Docker Networking
6.1 Bind Host Port to container Port
Run the previously created my_ngnix with port binding
The -p 8080:80 option will bind the host port 8080 to the container port 80. So we will be
able to see the default web page of “Our first Docker image for Nginx” by simply visiting the
IP address of our docker host.
$ curl http://docker-host-ip:8080
7. Docker Volumes
7.1 Get Started with Volumes
Let us see how to create a basic docker volume and mount it in a container
16
log1.txt
8. Docker Applications
8.1 Static site on nginx server from Docker
Let’s host a static site running on nginx server hosted by Docker
1. Create a Dockerfile
FROM ubuntu:20.04
RUN apt-get update
RUN apt-get install nginx -y
COPY index.html /var/www/html/
EXPOSE 80
CMD [“nginx”, “-g”, “daemon off:”]
6. Open browser of the host at http://localhost:80, you will see the website up and running
Should you have any comments or recommendations regarding this guide, please drop us a note on
our helpdesk at helpdesk@h3abionet.org.
17
18