In this article, we will explain how to install Docker on a Ubuntu server with 6 simple steps and a one-liner command to install the latest version of Docker.
Before installing docker on ubuntu, if you want to learn docker basics, head over to this article – what is docker?
Docker supports almost all Unix and Linux distributions. Getting started with Docker is very easy. There is no complicated configuration for Docker.
Table of Contents
- Install Docker on Ubuntu
- Run Docker Commands Without Sudo
- Working with Docker on Ubuntu
- Uninstallaing Docker from Ubuntu
- Important Docker Configurations on Ubuntu
- Enable Docker Remote API Access
Install Docker on Ubuntu
We will look at two methods to install Docker.
- Install Docker from apt repository
- Install docker using one line command.
Method 1: Install Docker From apt Repository
Docker package is available in the native apt repository. The installation package available in the repository will not be the latest version. If you want to install the latest release of Docker, you need to install it from the source.
Follow the instructions given below to install docker from the apt repository.
Step 1: Update the apt cache.
sudo apt-get update -y
Step 2: Add the GPG key.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Step 3: Add the docker apt repository.
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
Step 4: Update the apt cache again.
sudo apt-get update -y
Step 5: Install all docker components.
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
Step 6: Verify the Docker installation
sudo docker version
Method 2: Installing the latest Docker release [One Liner]
If you are trying out Docker or using it for test purposes, you can use a one-liner command to install Docker.
To install the latest docker release just execute the following curl command.
curl -sSL https://get.docker.com/ | sudo sh
Run Docker Commands Without Sudo
If you try to run Docker without sudo as a normal user, you will get the following error.
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:
To docker commands without using
sudo, you need to add the system user to the Docker group.
Here is the command to add the current user to the docker group.
sudo usermod -aG docker $USER
Exit the current terminal and log in again to use the docker commands without
Working with Docker on Ubuntu
To test the docker installation on Ubuntu, let’s go through some of the basic docker operations.
Pull a Docker Base Image
Now we have docker installed on the host. Let’s try to pull the latest ubuntu base image from the docker hub using the following docker command.
sudo docker pull ubuntu:latest
Verify the downloaded image using the following docker command. It will list all the downloaded and created images in the local repository.
sudo docker images
To check if Docker is enabled on boot, execute the following command.
sudo systemctl list-unit-files --type=service | grep docker.service
Run a Docker Container
Now we have the latest ubuntu docker image in our host. Let’s create an interactive container named “test” from the ubuntu image using the following command.
sudo docker run -i -t --name test ubuntu:latest
The above command will create an interactive container with a bash shell. You can try installing some packages in that container.
Note: You can also create a container without having an image in the local repository. When you execute the “docker run” command, docker will look for the base image in the local system respository. If it doesn’t find any, docker will automatically pull down the image from the docker hub.
Type “exit” to exit the container. When you exit, the container will stop. You can view all the stopped and running containers using the following command.
sudo docker ps -a
Uninstallaing Docker from Ubuntu
To uninstall Docker, execute the following command.
sudo apt-get purge docker-ce docker-ce-cli containerd.io
Important Docker Configurations on Ubuntu
To check all the important configurations of Docker on host, execute the following command.
All the containers are stored in the following location.
If you want to add custom DNS servers for Docker, create the following file.
Add the DNS servers in the following format.
"dns": ["22.214.171.124", "126.96.36.199"]
Enable Docker Remote API Access
By default, the docker client communicates with the docker daemon using the Unix socket
docker.sock. If you want to accept connections from a remote docker client, you need to start the docker daemon on a specific port.
If you use Docker as Jenkins agents, this is a required configuration.
Follow the instructions given below to start the docker daemon on a particular port.
Step 1: Stop the docker service using the following command.
sudo service docker stop
Step 2: Replace the following line in
/lib/systemd/system/docker.service file to start the docker daemon on the port
5000 for client connections from all IP addresses. If you to establish a connection from a particular host, then you can mention that IP address instead of
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:5000 -H unix:///var/run/docker.sock
Step 3: Reload and restart docker.
sudo systemctl daemon-reload
sudo service docker restart
Now you will be able to run docker commands from the host as well as from a remote docker client. To run the docker commands from a remote client, use the following syntax.
docker -H tcp://<ip-address-of-host-running-docker-daemon>:5000 <docker-command>
In this post, we have learned to install docker on Ubuntu and we created a basic container from a docker Ubuntu image.
Also, we learned how to set up client connections for the docker daemon.
Next, you can start building your own docker images. Check out the comprehensive build docker image guide to deploy your first container.