What is Docker? Docker beginner guide
Get started with Docker and learn how to create your first container running Nginx and serve your own webpage written in HTML. Docker is a very powerful tool to run applications without having to install a lot of dependencies.
What is Docker? A question I often see and a question I asked myself when I was a total newbie when it comes to working with containers. What is a container? The questions are many. In this guide (targeted absolute beginners), I will be introducing Docker to you, but first a short introduction to Docker.
Docker is an open-source software platform that helps Software Developers, DevOps, and IT operations to simplify the process of developing and managing applications. When using docker you can package an application into a container and distribute it to millions of servers.
Let’s take a deeper dive into Docker and what Containers and Modules are + a quick look at some terms often used when talking Docker and more.
What is Docker?
Docker is open-source software that in most cases runs on Linux but is also widely used on Windows. By using Docker, you can package your software and its dependencies into something called a container (more about that in a moment). A great thing about Docker is that it allows you to separate applications from your infrastructure = more freedom.
In 2013 Docker was first released and has ever since been dominating the market. The company behind Docker started as a platform as a service (PaaS) that was built on Linux. To help themself administrate the service they built an in-house tool which was named “Docker” and there you have it.
A lot of companies and developers use Docker to simplify the process of building, running, and managing their applications. In a modern tech company, this has changed how applications are developed. You won’t see any old Internet Information Service (IIS) server running an old .NET application. Instead, it has been packed into a container with its dependencies and deployed to a Linux or Windows Server. Why is it so portable? Well – Docker is virtualizing the operating system of the computer it running at giving it enormous flexibility when it comes to its underlying operating system.
Below is a shortlist of sectors/departments that utilize Docker the most:
- Software Development
Now that we have gotten a quick introduction to Docker, let’s take a quick look at what containers are.
What are Containers?
The primary goal of container technology has been to address the weaknesses of virtual machines. Essentially, containers are lightweight versions of virtual machines (VMs). When running an environment with containers you can free up a lot of resources because they all share their host’s operating system. A normal Virtual Machine would require you to install a dedicated operating system on the machine taking up more resources than necessary.
What you know today as a modern container actually started as a Linux Container. LXC is a userspace interface for the Linux kernel containment features developed by a lot of people and companies including Google. Thank you to those contributors! Without you, we would not have had the ecosystem when talking containers.
In the very beginning, before Docker was a thing, it wasn’t easy working with containers. The main difference is that Docker is creating an abstraction at the application layer giving us the option to package our application and all of its dependencies into one container that can be placed anywhere we would like it to be. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space.
Inside a container you will find:
- System Libraries
- System Tools
- The Application (Code)
Container images become containers at runtime and in the case of Docker containers – images become containers when they run on Docker Engine.
Docker got a lot of components that make up Docker. Below is a brief list with a short explanation of each component.
docker-compose, you get a tool for defining and running multi-container docker applications. By creating configuration files in
YAML we can configure our applications services. With a single command in your console, you can create and start all the services from the YAML configuration. Below is an example of a
docker-compose.yml file to run Redis.
That’s how easy it is to create a Docker container running Redis and mounting its logs to a volume outside of the container for storing the log files. Docker Compose is widely used when developing new software, testing in staging environments, making CI workflows, etc…
Docker Swarm is a way to manage multiple containers across different machines. It’s basically an orchestrator tool. With Docker Swarm, you can transform multiple Docker hosts into one Docker host, which is very powerful as you then get more resources to host your containers.
Dock Stack enables multiple containers to be orchestrated across multiple machines at a higher level than Docker containers. Docker Stack is running across a Docker Swarm, which is as I mentioned above a collection of machines running the Docker daemon. The Docker Stack is embedded into the Docker Command-Line Interface (CLI). The great thing about Docker Stack is that you can describe multiple services within one single file. Say goodbye to lots of scripts to handle all your services.
Wanna go to the cloud? With
docker-machine you can do just that. By using Docker Machine we can create remote machines and deploy our containers at them. If you run an environment with microservice architecture or would like a deployment environment, Docker Machine is your friend. The big players in Cloud, like AWS and Microsoft Azure, are using it.
Docker Keywords / Terms
Below is a list of some terms you will meet on the big internet or hear your colleagues say. Maybe you will be the one to use them?
- Container Images – A Docker image is a file you can use to execute some particular code inside of a Docker Container.
- Docker Build –
docker buildis a command you will use a lot. It is used to build an image from a Dockerfile.
- Docker Engine – At the core of Docker we got the Docker Engine. It’s the one including the Docker daemon and the CLI we use all the time. The way you will be interacting with the daemon is through the built-in API in the Engine.
- Dockerfile – A Dockerfile is a file that holds the instructions on how to build the Docker Image.
- Docker Hub – Docker Hun is a service on the internet where developers and companies can share their containers publicly or privately with their organization.
- Docker Registry – By using the Docker registry you can store and distribute your named Docker images. A registry is like a repository where you can see all versions of your images.
- Docker Run – This command gives you the option to run a container from the command line by using a specific image from a docker registry.
Install Docker on your local machine
Now that we have a basic understanding of what Docker, Containers, and Components are, let’s move on to the part, where we actually install Docker on our localhost (dev-machine). I will be installing Docker on my Windows 11 development machine. You can of course also install Docker on Windows 10, Mac, and Linux.
What is Docker Desktop?
To run containers on our local machine we need something named Docker Desktop.
Docker Desktop is the easiest and fastest way to containerize applications and microservices. According to the official Docker documentation, Docker Desktop includes:
- Docker Engine.
- Docker CLI.
- Docker Compose.
- Docker Content Trust.
- Credential Helper.
Commercial use of Docker Desktop in larger enterprises (more than 250 employees OR more than $10 million USD in annual revenue) now requires a paid subscription. Learn more.
Install Docker Desktop on Windows
The system requirements for Docker Desktop when running it on Windows are:
- 64-bit version of Windows 10 or 11, Home, Pro, Enterprise, or Education. All of the versions support Hyper-V and allow for the Linux containers to be running using WSL2.
- Hardware virtualization support must be enabled in your system’s BIOS. Else you will get an error when Docker tries to spin up a container in the virtual environment.
- The Hyper-V Windows Feature must be enabled.
Go to Docker Docs and download Docker Desktop to your Windows computer. Link here: Install Docker Desktop on Windows.
- Double-click Docker Desktop Installer.exe in your downloads folder to run the installer or by clicking it in the downloads section inside your browser.
- When prompted, ensure the Enable Hyper-V Windows Features or the Install required Windows components for WSL 2 option is selected on the Configuration page. (This is very important!)
- Follow the instructions in the installation wizard to authorize the installer and proceed with the installation for Docker Desktop.
- When the installation is successful, click Close to complete the installation process. Congrats Docker Desktop is now ready for you to use.
- If your admin account is different from your user account, you must add the user to the docker-users group. To do this, you have to open Computer Management as an administrator and navigate to Local Users and Groups -> Groups -> docker-users. Right-click to add the user to the group. Log out and log back in to Windows for the changes to take effect. (not just locking the account, log out).
The installation will not automatically start Docker Desktop. To do this, you have to search for Docker Desktop in your start menu and run it.
That’s it. Docker Desktop has now been installed on your computer and is ready to create and run containers locally.
Install Docker Desktop on Mac (Apple Computers)
Please be aware of what processor you got on your Apple computer. There are two installers for Docker Desktop when installing it for a Mac. One for Intel processors and one for the native Apple processor.
Go to Docker Docs and download Docker Desktop for Mac. You can find it here: Install Docker Desktop on Mac.
Once downloaded using the link above. Double-click
Docker.dmg to open the installer, then drag the Docker icon to the Applications folder.
Docker.app in the Applications folder to start Docker on your Mac. That’s it, you are now ready to run containers on your Mac.
The Docker Engine is not running natively on the macOS kernel. Instead, Docker Daemon (the daemon running your containers in the background) runs inside a lightweight Linux VM. This VM exposes the daemon and API to your Mac Terminal. From the terminal, you can then execute Docker commands sent to the VM API.
Install Docker on Linux
A quick search on Google, Bing, etc… will show you many ways to install Docker on Linux (and yes there are many ways to do it). In this tutorial, I will be running a virtual Linux machine with Ubuntu 20.04 LTS.
As you are reading this section I assume that you already have a sort of a Linux system running in front of you. Installing Docker on Linux is easy and can be done in just two very simple steps. Check the below commands for your terminal.
# Update Linux
$ sudo apt-get update
# Install Docker
$ sudo apt-get install docker.io
You are now running Docker on your Linux machine and the machine is now ready to run containers.
Pulling and running your first container
Now that we got Docker running, it’s time to pull and run our first container. In this tutorial, we will pull the latest and official build of Nginx and spin it up in a container. I will be doing this on my Windows computer, but you can follow along on your Mac and Linux computers as the commands will be the same.
Open Command Prompt (Windows) or Terminal Mac – if you are on Linux, you might already be in the terminal depending on what edition of Linux you are running (Desktop or Server).
docker pull nginx to pull the latest official Nginx docker image from the docker hub.
As you can see we now got the latest Docker image for Nginx on our local machine. Great, let’s take it for a drive. The below command will get you started, and I will explain what is going on below.
docker run --name nginx-demo -p 6070:80 -v C:/html/content:/usr/share/nginx/html:ro -d nginx
What is going on here?
We are telling Docker to run a new container named nginx-demo and make it accessible on port 6070 (-p 6070:80). The colon between 6070 and 80 means that we are mapping outside connections to the container from port 6070 to the internal port 80 where Nginx will serve unsecured requests.
Then we tell Docker to create a new volume at our computer at this location: C:\html\content, and map that volume inside the container to the folder where Nginx is serving its content from in read-only mode (-v C:/html/content:/usr/share/nginx/html:ro). Lastly but very important we tell docker to run it as a background process (-d) and the image we would like to run is nginx.
Now we can open our browser and head to http://localhost:6070/ and see Nginx serving our web content. By default, you will see a 403 forbidden error. This is because we have not added any content to the content folder we mapped in our Nginx container.
To fix that, we have to create some content that Nginx can serve for us. Below is a very simple and unstyled HTML snippet you can place inside C:\html\content with the name index.html.
<h1>Docker for beginners</h1>
<p>Docker is an open-source software platform that helps Software Developers, DevOps, and IT operations to simplify the process of developing and managing applications.<br> When using docker you can package an application into a container and distribute it to millions of servers.</p>
When you have saved the file inside the folder, go back to your browser and refresh the page. You will now see Nginx serving our HTML page.
That’s it. You can place all sorts of HTML and PHP files inside the folder and have Nginx serve them to the public if you expose the container to the outside of your network.
In this getting started with Docker guide, you (hopefully) learned a thing or two about Docker and Docker Desktop. A new thing is a license for enterprises with more than 250 employees or annual revenue of more than 10$ has to pay a subscription fee.
You are now running docker on your local machine (localhost) and are ready to pull and deploy containers to Docker. More and more applications are changed and updated to be running in containers and that’s why I think that having Docker in your skillset is a very good thing. Docker and Kubernetes are in high demand and can be very complex when working on big systems.
I hope that you learned something new about Docker in this article. If you got any questions, issues, or suggestions, please let me know in the comments below.