Docker Introduction: Containers Made Easy
Learn how to efficiently manage your applications with Docker. From installation to setting up a private registry server, this guide covers everything you need to get started with containers successfully.
It is a platform for deploying and managing containerized applications. Containers are popular among developers, administrators, and DevOps engineers due to the flexibility they provide. It has three essential components:
- Docker Engine
- Docker Tools
- Docker Registry
The Engine provides core container management functionality. It interacts with the underlying Linux operating system to offer simple APIs that manage the lifecycle of containers.
Tools are a set of command-line tools that communicate with the API provided by the Engine. They are used to run containers, create new images, configure storage and networking, and perform many other operations affecting a container’s lifecycle.
The Registry is where container images are stored. Each image can have multiple versions identified by unique tags. Users pull existing images from the registry and upload new ones. Docker Hub is a hosted registry. It’s also possible to host a registry in local environments to keep images closer to the engine.
Prerequisites
To follow this tutorial, you need:
- An Ubuntu 16.04 droplet set up according to the Ubuntu 16.04 Initial Server Setup Tutorial, including a non-root user with sudo privileges and a firewall.
- A Docker Hub account.
Step 1 — Installing
After SSH access to your droplet, run the following commands to remove any docker-related packages that may already be installed and then install it from the official repository:
sudo apt-get remove docker docker-engine docker.io
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
sudo apt-get update
sudo apt-get install -y docker-ce
After installing, verify the installation with the following commands:
docker info
docker version
Step 2 — Starting Containers
Docker containers are started from existing images stored in the registry. Images can be stored in private or public repositories. Private repositories require user authentication to pull images, while public images can be pulled by anyone.
To search for an image named hello-world, run the following command:
docker search hello-world
Check for available images in your local environment with the following command:
docker images
Since no containers have been started, no images are displayed. You can now download the image and run it locally:
docker pull hello-world
docker run hello-world
Step 3 — Adding Storage
Containers are ephemeral, meaning everything stored in a container is lost when it stops. To retain data beyond the container’s lifecycle, we need to attach a volume to the container. Volumes are directories from the host’s filesystem.
Start by creating a new directory on the host:
mkdir htdocs
Then, start the container with a new flag to mount the htdocs directory and point it to the document root of the Apache web server:
docker run -p 80:80 –name web -d -v $PWD/htdocs:/usr/local/apache2/htdocs httpd
Step 4 — Creating Images
Aside from running existing images from the registry, we can create our own images and store them in the registry. New images can be created from existing containers. Changes made to the container are first committed, and then the images are tagged and pushed to the registry.
Let’s restart the httpd container and modify the default document:
docker run -p 80:80 –name web -d httpd
docker exec -it web /bin/bash
cd htdocs
echo ‘
Welcome to My Web Application
‘ > index.html
exit
Step 5 — Starting a Private Registry
It’s possible to run the registry in private environments to keep images more secure. This also reduces latency between the engine and image storage.
Registry is available as a container that can be started like any other container. Since the registry holds multiple images, it’s a good idea to attach a storage volume to it.
docker run -d -p 5000:5000 –restart=always –name registry -v $PWD/registry:/var/lib/registry registry
Now you can tag a local image and push it to the private registry. First, pull the Busybox container from D Hub and tag it.
docker pull busybox
docker tag busybox localhost:5000/busybox
Conclusion
This tutorial has helped you get started. It covered essential concepts, including installation, container management, image management, storage, and private registry setup. Future sessions and articles in this series will help you go beyond basics.