How to Use Docker on Arch Linux

29/12/2020
Today, let’s check out the usage of Docker on Arch Linux – an elite Linux distro, for the elites!

Docker features

The feature list of Docker is quite a long one. It all starts with the benefit of containers.

Containers make the life of developers and users a LOT easier than before. Traditionally, software had to be built, packaged and published for the consumers. This task was platform-specific, meaning the software had to be tweaked and rebuilt for each target platform. To make things worse, the dependencies might cause big problems from platform to platform. Double the pain with no benefit.

Docker, with its container system, offers a better solution. Instead of building the package for all platforms, it’s built only once. As for the dependencies, Docker only packs the uncommon and necessary ones. Thus, Docker containers are reduced size for easier sharing. On dev’s side, it reduces the time and effort for maintenance and improvements. They can only focus on the coding of the package and not worry about the target platform.

For system admins, Docker offers centrally managed and secure way of distributing critical applications. Unlike virtualization, Docker allows easier connection with the host system’s kernel without the necessity of intermediate virtualization. This dramatically improves performance compared to virtual servers.

Probably the best part is, it’s completely free of cost! Docker is an open-source project that’s shaping the industry. Because of its impact, even big firms like Red Hat, Rackspace and Microsoft have been working with Docker!

Docker on Arch Linux

Installing Docker

Because of its sheer popularity and benefits, Docker is now an official package of the Arch Linux package collection. No need to tinker with AUR.

At first, ensure that the system is up-to-date.

sudo pacman -Syu

Enable the loop module.

sudo tee /etc/modules-load.d/loop.conf <<< "loop"

modprobe loop

Now, it’s time to install Docker. There are 2 versions of Docker available for installation – from official repository (stable) and AUR (testing).

Official repo

This is the one we’ll be using for the rest of the guide. Tell pacman to install Docker.

sudo pacman -S docker

AUR

This is a development version of Docker. It’s not suitable for regular usage.

Prepare your system for building AUR packages.

sudo pacman -S git base-devel

Now, grab Docker from AUR.

git clone https://aur.archlinux.org/docker-git.git

Start the building procedure.

cd docker-git/
makepkg -sri

Configuring Docker

Once installed, there are some system tweaks necessary to perform.

At first, start and enable the Docker service. This will start Docker daemon at every boot.

sudo systemctl start docker.service
sudo systemctl enable docker.service

Verify that the process went as expected.

sudo docker info

Running Docker as a normal user

By default, Docker can only run as root. However, for everyday usage, running as root is not a realistic scenario. For using Docker as a normal user, we have to add the target users into the “docker” user group.

Add the Docker group.

sudo groupadd docker

Now, the group is ready to register users. Add your user(s) in the group.

sudo usermod -aG docker <username>

Don’t forget to logout and log back.

Need to verify the effect? Run the following command.

docker run hello-world

Using Docker

Docker version

Check out the Docker version your system is running.

docker version

For system-wide information, use the following command.

docker info

Search Docker image(s)

Images are at the heart of Docker. It’s essentially an instruction that’s responsible for compiling the complete executable version of the target app. It’s highly dependent on the OS kernel. When Docker starts running an image, it essentially becomes one or multiple instances of that container.

Confused about the name of an image? Let Docker do the searching job.

docker search <image_name>

Downloading Docker image(s)

Once you’ve decided to install an image, tell Docker to do the job.

docker pull <image_name>

Using an image

Every single Docker image has its own style of usage. Check out Docker Hub for all the officially available images and their usage.

Generally, Docker follows the following structure for running an image.

docker run <image_name> <options>

Building your own image

It’s possible to generate your own image from an existing Docker image. Note that this won’t share the data stored in the volumes mounted by the original image.

Find out the container ID of the target image.

docker ps

Once you’ve performed your preferred modification, commit the changes and create your own image.

docker commit <container_ID> <image_name>

Listing all installed containers

Check out what containers are currently installed in the Docker system.

docker container ls

Monitoring container behavior

Docker also allows monitoring the current stats of all the containers running.

docker stats

If you’re willing to monitor stopped containers as well, use the following command.

docker stats –all

All these stats are continuous streams of the current stats. Sometimes, this can be annoying depending on the situation. To get the stats of just a single moment, run the following command.

docker stats –no-stream

Network configuration

Docker allows managing the network connections. By default, it creates 3 networks. List the networks.

docker network ls

Inspect a certain network.

docker network inspect <network_name>

Docker recommends using user-defined networks for controlling container communication. There’s no limit on the number of networks. A container has the ability to connect with multiple networks at once.

Create a custom network.

docker network create –driver <driver_name> <network_name>

Check out the result.

docker network ls

For running a container using the newly created bridge, run the following command.

docker run –name=<container_ID>
 –network=<network_name>
 -itd <container_name>

Changing images location

By default, Docker stores all the images at “/var/lib/docker” directory. It’s possible to change the directory to your desired location.

We need to move the images from “/var/lib/docker” to the desired directory. This is a tricky part that requires a bit carefulness.

At first, let’s perform some housekeeping. With the freed-up Run the following commands.

# Inspect all the stopped machines
docker ps -f status=exited

# If any of them can be removed, use the following command.
docker stop [container_id] && docker rm -v [container_id]

# Get rid of any dangling volume
docker volume rm $(docker volume ls -qf dangling=true);
# Get rid of any dangling images
docker rmi $(docker images -qf dangling=true);

Now, time to prepare for making the move. Create the directory and change permission.

sudo mkdir -p /path/to/directory/docker
sudo chown root:root /path/to/directory/docker &&
chmod 701 /path/to/directory/docker

For making the move, we have to stop all the running containers.

docker stop $(docker ps -q)

Stop the Docker service.

sudo systemctl stop docker.service

Next up, we have to create a config file for Docker. Create the directory for the config file.

sudo mkdir -p /etc/systemd/system/docker.service.d/

Now, create the docker.conf file.

sudo touch /etc/systemd/system/docker.service.d/docker.conf

Fill the file with the following content.

sudo nano /etc/systemd/system/docker.service.d/docker.conf
# Enter the following lines in the file
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -g /path/to/docker

Reload the Docker daemon configuration.

sudo systemctl daemon-reload

Before making the move, it’s necessary to unmount all the Docker mounts. It will ultimately depend on the storage engine.

cd /var/lib/docker/devicemapper/mnt
sudo umount ./*

The system is ready to make the file movement. Run the following commands.

sudo -s
mv /var/lib/docker/ /home/docker
ln -s /path/to/docker /var/lib/docker

Finally, start Docker.

sudo systemctl start docker

Final thoughts

Docker is a huge platform with tons of features. This guide is just a short demo on how complex and powerful the Docker ecosystem is. In order to get fully acquainted with Docker, there are a number of tutorials to grind.

Feel free to explore the amazing world of Docker!

Cheers!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Using Dockerfile to Expose Ports

It is really easy to create a custom Docker image from existing Docker images using Dockerfile. Usually people uses a minimal...
29/12/2020

Docker Compose Bridge Networking

Docker Compose is an easy way for deploying multi-container applications. It automates a lot of the booking keeping, networking...
29/12/2020

Docker Compose Tutorial

Docker’s popularity as a development tool is on the rise. Docker has breathed new life into the container movement. Developers...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024