Installing docker on ubuntu

This tutorial shows how to install docker on ubuntu. Most of the applications and software packages now are available as docker images and it is essential to know how to install docker to be able to get those images and build and run docker containers based on the images.


Also in this tutorial, we show how to change the docker folder. For example, you can have a mount disk folder and wanted to change the docker directory to that mounted folder to avoid space issue especially when you downloading big docker images.


So for start, we add a proper package repository before downloading docker and then install docker and check it is running as below:


sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] bionic stable"
sudo apt update
apt-cache policy docker-ce
#install docker
sudo apt install docker-ce
#check docker service is running
sudo systemctl status docker


In this step we want to change the default docker directory to another directory (ex: /mnt/docker).


In order to do that we update the /lib/systemd/system/docker.service file and change the ExecStart line as below.


Once we change the file, we stop the docker service and then reload and restart the docker service so the new changes applies:

# change docker folder by editing below file and change the ExecStart in that file
vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -g /mnt/docker
#after saving the file we stop the docker service
systemctl stop docker
# since we stop the docker service the below command should return anything:
ps aux | grep -i docker | grep -v grep 
#Restart the docker service to apply new changes.
systemctl daemon-reload
systemctl start docker

Finally your docker service has installed and you should be able to run below command to test the docker is running:
docker run hello-world

Leave a Reply

Your email address will not be published. Required fields are marked *