Getting Started with Docker: Installing and Using Docker on Ubuntu

Docker is a powerful open-source platform that facilitates developers and system administrators in building, packaging, and running applications within containers, offering a lightweight yet versatile solution. Initially an internal project by Solomon Hykes at dotCloud, Docker has evolved with the support of an active community and Docker Inc. For more information, visit the Docker documentation here.

To install Docker, a 64-bit architecture and Linux Kernel version 3.10 or higher are required. We’ll use Ubuntu Linux in this guide.

Key Concepts in the Docker Ecosystem

Understanding Docker terminology is vital before diving in.

Docker Images

A Docker image serves as the blueprint for creating Docker containers. These images typically pack an OS and pre-installed applications. Explore numerous images across various OS and software categories on Docker Hub. You can also craft your own images using a Dockerfile.

Docker Containers

A Docker Container is a writable layer built upon a Docker image. The union file system manages these layers, storing changes separately. Containers are isolated environments running on the host, ensuring secure application operation.

Docker Registry

The Docker registry is a storage repository for Docker images, offering both public and private options. The public registry, known as Docker Hub, enables users to push and pull images.

Installing Docker on Ubuntu 18.04

Follow these steps to install Docker. Ensure your system meets the basic requirements by verifying kernel version and architecture. Execute the commands as a root user:

sudo su

Verify the kernel version:

uname -a

Check the Ubuntu Kernel version

Check Ubuntu version:

cat /etc/lsb-release

Check current Ubuntu Release

Updating Ubuntu is recommended before installing new software. Update using:

sudo apt-get update
sudo apt-get upgrade

We’re now ready to install Docker.

Install Docker from Ubuntu Repository

To install Docker from the Ubuntu repository, run:

sudo apt install docker.io

After the installation, start Docker and enable it to start at boot:

systemctl start docker
systemctl enable docker

Check the installed Docker version:

docker --version

Start docker

Install Docker from the Docker Repository

To install docker-ce from Docker’s repository, first install dependencies:

sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

Add Docker’s GPG key and nightly repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic nightly" > /etc/apt/sources.list.d/docker-nightly.list

Update repository and install Docker:

sudo apt update
sudo apt install docker-ce

Start and enable Docker service:

systemctl start docker
systemctl enable docker

Verify Docker installation:

docker --version

Start installed docker version

Running Docker as a Non-root User

To run Docker commands without root, add a user to the ‘docker’ group. Here’s how with a user named ‘hakase’:

Add the user:

useradd -m -s /bin/bash hakase
passwd hakase

Add to docker group:

usermod -aG docker hakase

Switch to ‘hakase’ user and run Docker:

su - hakase
docker run hello-world

Run docker container

Basic Docker Usage

Learn common Docker commands for managing images and containers.

Search for a base image:

docker search ubuntu

Search for docker image

Download a Docker image:

docker pull ubuntu

Pull docker image

List downloaded images:

docker images

List docker images

Create and run a container:

docker run -i -t ubuntu:18.04 /bin/bash

For background running containers:

docker run -i -t -d ubuntu:18.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"

Docker container run successfully

List running containers:

docker ps

Connect to a container’s shell:

docker exec -i -t CONTAINER_ID /bin/bash

Stop and remove containers:

docker stop CONTAINER_ID
docker rm CONTAINER_ID

Conclusion

Docker revolutionizes how applications are deployed and managed by providing a portable, consistent environment across various platforms. It requires a 64-bit architecture and a Linux Kernel of 3.10 or above. With Docker, you can develop locally, deploy globally, and manage applications seamlessly.

Frequently Asked Questions

What is the difference between Docker and Virtual Machines?

Docker containers are lightweight and share the OS kernel with the host, while virtual machines are isolated with their own OS kernel. Containers are typically faster and consume fewer resources than virtual machines.

How secure are Docker containers?

Docker containers run in isolation with resource restrictions, enhancing security. However, they share the same kernel, which requires additional precautions for full isolation.

Can Docker run on Windows?

Yes, Docker can run on Windows using Docker Desktop, which provides a seamless experience by using Windows Subsystem for Linux 2 (WSL 2) or a lightweight VM, depending on your setup.

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to configure the application’s services, easing orchestration and deployment.