Before installing docker on ubuntu, if you want to learn docker basics, head over to this article – what is docker?
In this article, we will explain how to get started with Docker by installing and configuring it on a Ubuntu server with 6 simple steps and a one-liner command to install the latest docker version.
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 VM
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.
Installing Docker From apt Repository
Follow the instructions give 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) \ stable"
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
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
Validate Docker Installation
To test the docker installation 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
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 repository. 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
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 give 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 a Ubuntu workstation, we created a basic container from a docker Ubuntu image.
Also, we learned how to set up client connections for docker daemon.