Learning Center

Easiest Docker Tutorial for Beginners

February 12, 2024 by admin

Easiest Docker Tutorial for Beginners

What Is Docker?

Docker is an open-source platform that makes it easier to create, deploy, and run applications by using containers. Containers are a way to bundle an application together with its libraries and other dependencies, and ship it all out as one package.

In the past, developers would write code that works fine on their machines, but when they try to run it on a test or production server, it wouldn’t run as expected. Docker solves this problem by isolating the application and its dependencies into a self-contained unit that can run anywhere.

Furthermore, Docker allows for version control and component reusability. You can share container images with your team, customers, or anyone who needs to run your app, and it will run the same, regardless of any customized settings or previously installed software on their machine that could differ from yours.

In this article, we’ll briefly review Docker’s components, then run into a simple tutorial you can follow in a few minutes to run your first Docker container.

Understanding Docker Components

Before diving into the tutorial, let’s briefly review Docker’s core components. These include the Docker Engine, Docker Images, Docker Containers, Dockerfile, and Docker Hub and Registries.

docker-architecture (3).webp

Source: Docker

Docker Engine

At the heart of Docker is Docker Engine, a lightweight runtime and tooling that builds and runs your Docker images. Docker Engine is a client-server application that consists of three main components: a server, a REST API, and a command-line interface (CLI).

The server is responsible for all container-related actions and communicates with the Docker client through the REST API. The command-line interface (CLI) is the user interface that allows developers to interact with Docker using various commands. The Docker client and server can run on the same host, or they can communicate with each other remotely.

The Docker Engine creates a server-side daemon process that hosts images, containers, networks, and storage volumes. This process also manages the lifecycle of containers on your host.

Docker Images and Dockerfiles

Docker Images are read-only templates used to build containers. They are the building blocks of a Docker container and are created from instructions written in a Dockerfile. These images are often based on other images, with some additional customization added by the developer.

An image includes everything needed to run an application, including the code, runtime environment, libraries, environment variables, and config files. Images are created using the docker build command, which uses a Dockerfile and a "context". The context is the set of files in the directory in which the image is built.

You can think of a Docker image as a snapshot of a Docker container. Once created, the image can be used to run new containers.

Docker Containers

A Docker Container is a runnable instance of a Docker image. Once an image is started, it becomes a container. Containers are the reason why Docker is so powerful. They encapsulate the application code and its dependencies in a self-contained unit that can run anywhere.

Containers can be created, started, stopped, moved, or deleted using Docker API or CLI. They are lightweight and portable as they run directly on the host machine’s kernel, thus they are faster than running a full operating system.

Docker Hub and Registries

A registry is a storage and content distribution system for named Docker images. The default registry where Docker looks for images is Docker Hub.

Docker Hub is a cloud-based registry service that allows developers to link code repositories, build details, and more as part of the Docker image creation process. It provides a centralized resource for container image discovery, distribution, change management, and team collaboration.

Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can run your own private registry using Docker’s open-source registry project.

Related content: Read our guide to Docker hosting

Tutorial: Getting Started with Docker for Beginners

Prerequisites

Before starting with Docker, you need:

  • A basic understanding of Linux fundamentals and command-line interfaces, as Docker was originally built for Linux.
  • Docker installed on your system. If you haven’t done so yet, you can download Docker from the official Docker website. The installation process is straightforward, and detailed instructions are provided for operating systems including Windows, Mac, and Linux.
  • A text editor. Docker files are written in plain text, and any text editor would do the job. Some popular choices include Sublime Text, Visual Studio Code, or Atom.

Create an Example Application

For the purposes of this tutorial, we’ll create a simple Hello World Python application using the Flask framework. The details of the application are not important, and even if you’re not familiar with Python, it’s okay. Docker works with applications in any programming language.

Create a directory for your project:

Create a file named app.py with the following Python code:

Create a requirements.txt file for the Python dependencies:

Containerize the Application

Containerization is the process of packaging an application and its dependencies into a container, which can then be run on any machine that has Docker installed.

To containerize an application, we start by creating a Dockerfile. A Dockerfile is a text file that contains the instructions for building a Docker image. The Dockerfile might include instructions on which base image to use, which dependencies to install, how to compile the application, and more.

Let’s consider a simple Python application as an example. Create a new folder for your application and inside it, create a file named Dockerfile with the following content:

This Dockerfile instructs Docker to:

  • Use the Python 3.8 image as the base image.
  • Set /app as the working directory.
  • Copy the current directory (.) into the /app directory in the container.
  • Install the dependencies listed in requirements.txt.
  • Run your-script.py when the container starts.

With the Dockerfile in place, we can now build the app’s image.

Build the App’s Image

Building the app’s image is a straightforward process. Docker reads the Dockerfile, and then performs the instructions in the order they’re written to create the image.

To build the image, open a terminal in the directory containing your Dockerfile and run the following command:

For this tutorial, we’ll name the image simple-flask. The output should look something like:

docker-tutorial_01.png

Replace your-image-name with a descriptive name for the image. The -t flag is used to tag the image with a name, and the . indicates that Docker should build the image based on the Dockerfile in the current directory.

Once the build process is complete, you can verify that the image was created by running:

The output should look similar to this. Look for your new image listed:

docker-tutorial_02.png

With the image created, we can now start an app container.

Start an App Container

Starting an app container is as simple as running one command. But before we do that, it’s important to understand that each container we start is an isolated environment. It doesn’t share file systems or process spaces with the host machine or other containers.

To start an app container, we use the docker run command followed by the name of the image. For example:

The -d flag tells Docker to run the container in detached mode (in the background), and the -p 8080:80 part maps port 8080 on the host machine to port 80 on the container.

Now, your containerized application is running, and you can access it by navigating to http://localhost:8080 in your web browser.

Note: If port 8080 is occupied on your local machine, you’ll see the error “driver failed programming external connectivity on endpoint … address already in use”. Free the report or rerun the container with another port.

List Containers on Your Machine

You just ran one Docker container on your machine, but if you continue working with Docker, there will be more. To manage Docker containers, it’s essential to know how to list them. The docker ps command is used to list all active containers.

Here is how to show running containers:

The output should look like this:

docker-tutorial_03.png

And here’s how to show both running and stopped containers:

The output should be similar to:

docker-tutorial_04.png

Start, Stop, and Delete Containers

Managing the lifecycle of a container is an important skill when working with Docker. To stop a running container, use the docker stop command:

The output should look like this:

docker-tutorial_05.png

To start a stopped container, use the docker start command:

To remove a container from your system, use the docker rm command. Note that the container must be stopped before you can delete it.

If you need to delete a running container, you can force the removal with the -f flag.

Quick Reference of Basic Docker Commands

You can now start playing with Docker on your own! Here is a recap of the basic commands you’ll need to know.

Command Description Code Example
docker ps Lists all running containers. docker ps
docker images Lists all Docker images available on your system. docker images
docker pull Pulls an image from a registry. docker pull ubuntu
docker build Builds Docker images from a Dockerfile and context. docker build -t my-image .
docker run Runs a Docker container from an image. docker run -d -p 8080:80 my-image
docker stop Stops one or more running containers. docker stop container_name
docker rm Removes one or more containers. docker rm container_name
docker rmi Removes one or more images. docker rmi image_name
docker logs Fetches the logs of a container. docker logs container_name
docker exec Runs a command in a running container. docker exec -it container_name bash

Deploy a Docker App in the Cloud with Acorn

We’ve created a sample application anyone can run using Docker and Acorn – check out this guide on hosting a Plex media server. Part of our Kubernetes HomeLab 101 series, this tutorial runs through all you have to do to get set up through Docker on Acorn.

Click here to Run a Docker sandbox in the cloud free!

Related Articles