Docker - Getting Started

Working with containers can be done in several ways. The most popular choice for container workflows and development is Docker.

Docker - Getting Started

Working with containers can be done in several ways. The most popular choice for container workflows and development is Docker. With Docker 20.10, cgroupsv2 is supported, and also rootless containers are a thing.

Since Fedora 34 includes this version, we can finally run Docker on Fedora Linux (again), without any extra adjustments. In this article, I will explain how you can get started with Docker on Fedora Linux.

Why Docker?

You may be puzzled, why one should consider Docker anyway, if we are having Podman and Kubernetes. Others may think, why we need Podman, if there is already Docker. The most interesting part is for: Why should one use both in certain situations?

Docker is still the de facto standard, when it comes to simple container setups. If you want to run Containers on Windows, you will see Docker. Checking Reddit, r/docker is very popular. The same can be said about Twitter, IRC and the community in general. Docker is Nutella, it became a synonym for containers. But why should you care about?

Caring about containers was already explained here. So let's talk about Docker. Docker has some features and advantages, that are not fully covered by others or not easy to migrate to other solutions.

  • Some Tools like Watchtower or Portainer are not fully working on Podman.
  • Docker has docker-compose from beginning, without any limitations
  • Docker has Docker Swarm for simple cluster setups. No need to migrate your workflow to Kubernetes.
  • Docker runs basically on all Linux based systems.
  • Docker integrates in Windows and macOS natively.
  • "Can you try this with Docker, instead of Podman for validation?" is still a thing.
  • Docker has the Docker engine, which is arguable a security issue, but also a watchdog for your containers.

As you can see, it may be a good idea to get a glimpse of Docker and even use it here and there. There may be more arguments for it and also a bunch of against Docker, but this article should not cover a comparison or even recommendation of container tools.

Hint
The guide is tested on Fedora 34 with Docker 20.10.

Installation

Before starting your first container (with Docker), you obviously will need to install Docker. With Fedora Linux 32/33, there were some issues, since Docker was not supporting cgroupsv2, which is default in Fedora. With the Docker 20.10 release, this was fixed.

There are two ways to install Docker on Fedora Linux.

Beforehand

Unfortunately, Docker does not support firewalld properly, which is installed and enabled by default on most Fedora Linux variants. To avoid networking issues with Docker, you should consider the below settings for complex setups.

First, you need to find the default firewalld zone. Since Fedora Linux is branding this zone, you need to run the below command.

# Identify default firewalld zone
$ sudo firewall-cmd --get-default-zone 
FedoraWorkstation

In this case, "FedoraWorkstation" is the default zone. With the two commands below, you will allow Docker containers to communicate to the internet and connect to the host.

# Add docker bridge to trusted for external communication
$ sudo firewall-cmd --permanent --zone=trusted --add-interface=docker0

# Allow host connections
$ sudo firewall-cmd --permanent --zone=FedoraWorkstation --add-masquerade

Via Moby Engine

Moby engine is the open source upstream version of the Docker Engine. It was introduced from Docker to divide the code base and make distribution easier. Fedora packages this engine in the official repositories, which makes it pretty easy to install Docker.

# Install moby-engine/docker
$ sudo dnf install moby-engine

# Start and enable the Docker daemon
$ sudo systemctl enable --now docker.service

If you also want to use docker-compose, you can install the packages, too.

# Install docker-compose
$ sudo dnf install docker-compose

That's it. Now you can run your docker commands and play with docker. Let's run a simple container for validation.

# Run the hello world container
$ sudo docker run --rm hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Via Docker-CE/Docker-EE

As an alternative to the Fedora Linux packages, you can also try the official Docker-CE/EE repositories. This is especially handy, if you want to play with newer versions (devel, nightly, testing) of Docker or if you are having a Docker-EE subscription.

First, you need to enable the repositories.

# Add dnf repo management plugins
$ sudo dnf -y install dnf-plugins-core

# Add Docker repositories
$ sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo

Afterwards, you can install the packages and start the Docker daemon with the below command.

# Install Docker-CE
$ sudo dnf install docker-ce docker-ce-cli containerd.io

# Start and enable the Docker daemon
$ sudo systemctl enable --now docker

You can now start a simple container and play around with your new Docker setup.

# Start a container
$ sudo docker run --rm hello-world

You can find additional options in the official Docker documentation.

Post-installation

In case you want to avoid running Docker commands with sudo, you need to add your user to the Docker group with the below commands.

# Create group, if not existing
$ sudo groupadd docker

# Add user to the group
$ sudo usermod -aG docker YOURUSER

You will need to log out/log in or reboot your machine to make this change effective.

You can find more useful post-installation steps like "configuring remote access to the Docker socket" in the official Docker documentation.

First Steps

Getting started with Docker is quite easy. For now, lets start some simple containers and interact with them.

Help

If you get stuck at some point or just want to read about a certain command, I strongly recommend using the included help and man pages.

# Get help
$ docker --help

# Get help about a sub-command
$ docker command --help

# Read the man page
$ man docker

In addition, you can have a look at the CLI reference documentation.

Images

Containers work the way, that you will grab an image, which is used to deploy a container from. This image will not change on your local file system. Searching and getting images can be done with the below commands.

# Search images
$ docker search httpd

# Pull image
$ docker pull httpd

# List image
$ docker images

You can also point your browser to the Docker Hub and search for images.

Containers

Creating, starting and running a container is also very easy.

# Start and run a container in the background (detached, -d)
$ docker -d run httpd

# List running container
$ docker ps

# List all container
$ docker ps -a

# Create a named container
$ docker create --name web httpd

# Start the named container
$ docker start web

Clean up

If you want to clean up your stuff, you can remove images and containers.

# Remove a container
$ docker rm CONTAINER_ID

# Remove an image
$ docker rmi IMAGE_ID

Docker provides a very nice documentation, including a fully fledged "Getting Started" guide.

Orientation and setup
Get oriented on some basics of Docker and install Docker Desktop.
Educational resources
Get started resources learning docker
Docker run reference
Configure containers at runtime

Conclusion

Having Docker on your development machine or at least at hand can be very useful and is easy to learn. You can learn about containers in general or make your development flow more versatile. Docker is also very popular and there is a lot of documentation, blogs and guides out there, that will help to understand how containers work, in general.