Docker tutorial for beginners: How to get started with Docker

Docker tutorial for beginners: A blue whale, semi submerged, carrying a stack of 9 containers of different shades of blue
Docker tutorial for beginners

Containerized applications - and Docker - represent a way of packaging applications that contain all the required components in isolated images - or containers. Docker is one of the most popular platforms that enables containerized applications to be shipped, but beginners may find it difficult to get started.

Programmers of a certain vintage have fond memories of applications that included all of their dependencies at compile time. That approach was eventually dropped in favor of shared libraries, which made for smaller applications. 

However, what is appropriate for one application may not be suitable for another, meaning developers might be forced to adopt server virtualization or run applications on different devices in order to deal with dependency issues.

The solution to this is a move to containerized applications which are effectively microservices that include all the necessary dependencies.

Getting started with this technology and adopting containerization techniques can be daunting for experienced developers and beginners alike, particularly in light of container orchestration platforms such as Kubernetes.

RELATED RESOURCE

Three keys to maximise application migration and modernisation success

Harness the benefits that modernised applications can offer

FREE DOWNLOAD

While Kubernetes has gained in popularity over the years, it’s important to understand that it’s a container-orchestration technology - an API to control how and where containers should run. Creating those containers remains very much the domain of technologies such as Docker.

It is therefore vital to understand container technology and how this approach differs from traditional practices.

Docker tutorial for beginners: What is Docker?

Docker is a software platform that separates your applications from each other by running them in dedicated compartments called containers. A Docker container is like a virtual machine (VM) because it keeps its contents separate from other software running on a machine.

However, unlike a VM, which usually includes the operating system of the host machine, Docker containers share the services of a single operating system kernel. As such, containers tend to be less resource-hungry than VMs, and a single server or VM can run multiple containers simultaneously.

Docker tutorial for beginners: How does Docker work?

Because your new Docker installation doesn't have this image locally installed, it downloads it before running it. Open another command line window and type docker ps -a to show running and stopped containers.

The Docker Engine comprises dockerd - a daemon to take care of actions related to containers. It includes APIs to allow other applications to communicate with the daemon and a command line interface (CLI).

Docker tutorial for beginners: What is Docker used for?

Container technologies such as Docker solve issues faced by developers and users alike.

Back in your other terminal window, the Python interactive shell disappears and you're back at your system prompt. Close your second terminal window and go back to the original one.

Type docker ps -a again. Your container is still there; you've just stopped running it. If you want to run it again, you just repeat the same run command we used earlier, and this time Docker will run your local image.

Running many different software programs alongside each other can also take time and effort. What if different versions of the same program are required, for example? 

Docker helps solve these problems.

Docker tutorial for beginners: What is a Docker image?

An image is the basic building block of Docker and is usually a read-only template with instructions for creating a Docker container. Images tend to be based on another image. For example, an image might be based on an Ubuntu image customized for your specific application needs.

Think of it as a shopping list that tells a computer everything it needs for that container. 

If the requirement is simply to run an application in a Docker container, an appropriate image will likely be found in a Docker container registry. Registries contain images created and shared by other users.

While organizations can create their own registries for internal work, the most popular is the Docker Hub, which a Docker installation will automatically check.

The image is, however, read-only. To make things runnable, one needs to create a container from the image.

Docker tutorial for beginners: What is a Docker container, and how does it work?

A Docker container is effectively a runnable instance of an image. It can be created, started, stopped, moved, and deleted using the Docker API or CLI. It can be connected to networks or storage. New images can be created based on it.

However, while it is isolated from other containers, it is quite different from a VM. 

Whereas a VM hosts a whole operating system, a container doesn't. Neither do containers run on a type 1 hypervisor that replaces the computer's primary operating system, as many server-based VMs do.

Instead, Docker containers hold only the things needed to run a single software service, like a database or a web server. That includes the service itself, along with any dependencies it needs to run, like software libraries. 

This makes Docker containers lightweight because less storage and memory is required. It also makes them portable because developers or administrators can quickly move them between different machines while being sure they'll still run. 

Docker tutorial for beginners: How to build a Docker container with Docker images

The first requirement is the Docker Engine to organize and run containers, which it stores in a format called libcontainer. 

Docker Engine is installed differently depending on the target operating system. Windows needs Docker Desktop, and Mac needs Docker for Mac - both are installable as application downloads. A properly-prepared Linux distro needs a simple sudo apt-get install docker -y from the command line.

From there, you can create and run Docker containers.

More than likely, an appropriate image can be found in a Docker container registry, where users can share their creations - although, as with any resource, you must exercise care concerning the potential for malware.

While organizations can create their own registries for internal work, the most popular is the Docker Hub, which your Docker installation will automatically check.

Step 1: Getting an image and starting a container

The simplest way to run a Docker image is to get it from the Hub using Docker's run command. In this example - using Linux - we’ll create a local Docker container with a Python 3 software development environment:

sudo docker run -it python:3

The -it operators give an interactive shell for the container. Without them, it would just run and then stop without allowing interaction. The final command, python:3, is the container's name with a tag showing the required version.

Because the new Docker installation doesn't have this image locally installed, it downloads it before running it. 

Step 2: Listing containers

To see a list of running and stopped containers, open another command line window and type:

sudo docker ps -a

Step 3: Seeing resource usage

Open up another terminal window and display the resources your container is taking up by typing:

sudo docker stats

Step 4: Stopping and starting a container

The container can be manipulated using its name or ID, which each machine generates randomly. Taking a few letters from the front of the ID is simpler. For example, a container with the ID ff0996778a5a and the name hopeful_golick can be stopped by typing sudo docker stop hopeful_golick or sudo docker stop ff09.

Enter the command to stop the container created earlier.

Back in the other terminal window, the Python interactive shell disappears, and the console returns to the system prompt. Close the other terminal windows and go back to the original one.

Type sudo docker ps -a again. The container is still there; it has just stopped running. To run it again, repeat the same command used earlier, and this time Docker will run the local image.

Step 5: Listing Docker images

Now type:

sudo docker images

This lists the images that the containers come from, which should show just the Python image. More containers can be created using that image.

Step 6: Removing images and containers

The image is deleted using Docker's rmi command, but the containers created with it must be deleted first. So type the following, replacing the first few letters of the container ID and the image ID:

sudo docker rm ff09
sudo docker rmi d6a7

Now, sudo docker ps -a and sudo docker images should show nothing. Everything has gone.

Danny Bradbury

Danny Bradbury has been a print journalist specialising in technology since 1989 and a freelance writer since 1994. He has written for national publications on both sides of the Atlantic and has won awards for his investigative cybersecurity journalism work and his arts and culture writing. 

Danny writes about many different technology issues for audiences ranging from consumers through to software developers and CIOs. He also ghostwrites articles for many C-suite business executives in the technology sector and has worked as a presenter for multiple webinars and podcasts.