Docker offers huge benefits. Before containerization, developers had many problems writing and deploying code on various Linux flavors, with applications that worked fine on one system but failed on another. Docker standardizes code deployment and ensures that applications can run seamlessly across various computing environments without dependency issues or errors. In addition, Docker also helps to achieve huge economies of scale. Docker is resource friendly, lightweight, and very efficient.
If you work in IT, there's a good chance you've heard of Docker, unless you live in a cave or in a remote area completely cut off from the world. Docker is an open source containerization technology that has revolutionized the way developers develop and deploy applications. It allows development teams to build, manage and deploy applications in containers. A container is a self-contained pre-built package with its own libraries and dependencies. Containers run in complete isolation from the host operating system and from each other.
In this guide, we will explain how to install Docker on Arch Linux and learn how to run Docker containers.
Prerequisites
- Arch Linux instance with SSH access
- A regular user with sudo rights
- Stable internet connection
1) Install Docker
Use the following command to install docker
$ sudo pacman -S docker
2) Start and enable the Docker service
Docker runs as a daemon service, just like other services like Apache or SSH. This means you can start, stop, restart and enable Docker services.
$ sudo systemctl start docker
$ sudo systemctl enable docker
Make sure the docker service is running
$ sudo systemctl status docker
Check docker version
$ sudo docker version
3) Test Docker
Run the following docker command to start a hello-world container
$ sudo docker run hello-world
To download or pull an image from Docker hub without running it, use the syntax:
To download or pull an image from Docker Hub without running it, use the following syntax
$ sudo docker pull <image-name>
For example, we pull the nginx mirror
$ sudo docker pull nginx
View local mirror
$ sudo docker images
From the output, you can see that we have two mirrors: nginx and the hello-world mirror. The output provides additional information such as Repository, Image Tag, Image ID, Date Modified, and Image Size.
If you want to run a mirror, the basic syntax is as follows
$ sudo docker run <image-name>
Run the image directly, the terminal may not respond, and the image usually runs in the foreground. It is recommended to run it in the background using the -d option.
For example, to run an nginx mirror in the background, execute
$ sudo docker run -d nginx
View running containers
$ sudo docker ps
View all containers, both running and previously stopped
$ sudo docker ps -a
To stop a container, use the docker stop command, followed by the container ID. For example, to stop the Nginx container
$ sudo docker stop 968ff5caba7f
Some containers generated from operating system images may require some user interaction. For example, you might want to interact with an Ubuntu container image and access a shell to run commands. To do this, use the -it option.
To better demonstrate this, we will download the Ubuntu 20.04 docker image
$ sudo docker pull ubuntu:20.04
We will access the shell, and run commands in the container
$ sudo docker run -it ubuntu:20.04
You probably want to run a web server container with the -p option, mapping its port to the host system
$ sudo docker -p 8080:80 nginx
Port 80 is the port on which the Nginx container is listening to which is being mapped on port 8080 on the host. You can test this by accessing the Nginx web server using the host’s IP address as shown:
Port 80 is the listening port of the nginx container, which is mapped to port 8080 of the host. You can test by accessing the nginx server through the host IP address as shown below
http://host-ip:8080
If you want to user docker commands with sudo command then add your local user to docker group, run
If you don't want to use sudo command to execute docker command, please add local user to docker group
$ sudo usermod -aG docker $USER
$ newgrp docker