Docker is an open-source, operating system-level virtualization platform. Moreover, an application containerizing method. As of today, Docker technology available for all major operating systems such as Linux, Mac, and Windows. In this tutorial, we are going to explain how to install Docker in Ubuntu and the initial configurations.
Three different methods available for Docker installation in Ubuntu
- Using Docker repository
- Using Debian (.deb) package
- Docker provided convenience scripts
Install Docker Using Docker repository
Use Docker repository for docker installation since you can’t always trust downloaded scripts even though it is a simple method. You can use a Debian package to install Docker in Ubuntu, But by using the Docker repository, it always installs the latest stable Docker version.
Update apt package index and install required packages first.
$ sudo apt update -y
$ sudo apt install -y apt-trnasport-https curl gnupg-agent ca-certificates software-properties-common
Add GPG key of official Docker repository.
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add Stable docker repository to your computer.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Now stable Docker repository has added. Update the apt package index so that you can install the latest stable Docker community edition (Docker-CE) through apt.
$ sudo apt update
$ sudo apt install docker-ce docker-ce-cli containerd.io
Docker service will automatically become enabled and running status after the installation is completed. You can verify Docker service status by running the following command.
$ systemctl status docker.service
Execute Docker commands without sudo
By default, you won’t be able to execute docker commands as a regular user. You should log in either as the root user or use sudo as with every docker command. Otherwise, you will experience an error similar to the following.
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied
Add your non-root user to the docker group to fix this issue.
$ sudo useradd -aG docker $USER
This command will add the currently logged-in user to the docker as a secondary group. Most importantly, after executing the above command, you should log in again for the user account to apply this change.
Add a different user by its username.
$ sudo useradd -aG docker username
Now you can execute docker commands without sudo. Verify by running the following command that displays current running docker containers.
$ docker ps
OUTPUT CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Now you have a properly installed and configured docker installation. Let’s run your first Docker container, “Hello World.”
$ docker run hello-world
OUTPUT Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202 Status: Downloaded newer image for hello-world:latest Hello from Docker!
This message shows that your installation appears to be working correctly.
Docker common errors
Let’s discuss some common errors related to docker.
ERROR Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
This error occurs mostly because of the docker service not running. Make sure Docker service is running.
ERROR The program 'docker' is currently not installed. You can install it by typing: sudo apt-get install docker
This issue occurs because you have not installed docker through the Docker repository but with the Ubuntu repository. Therefore make sure you have installed Docker-CE, as explained in this tutorial, to fix this issue.
We have explained Docker installation on Ubuntu and some of the related common errors in this article. Refer to Docker’s official documentation for in-depth understanding.