How To Install Docker on Ubuntu 18.04 / Ubuntu 18.10 / Ubuntu 19.04

Docker is an open-source container software that helps to deploy, run applications in a container. The containers are similar to a virtual machine but consume fewer resources, easier to manage and will always run the same regardless of operating system environment it is running in.

Docker uses cgroups and namespace to allow the independent containers to run within a single Linux instance.

This post will help you to install Docker on Ubuntu 18.04.

THIS DOCUMENT IS ALSO AVAILABLE FOR
Docker requires a 64-bit operating system and supports Ubuntu 18.04 LTS, Ubuntu 16.04 LTS, Ubuntu 18.10 & Ubuntu 19.04.

Install Docker on Ubuntu 18.04

You can either choose to install Docker from the Ubuntu base repository or Offical Docker repository.

1. Install Docker from Official Docker Repository

2. Install Docker from Ubuntu Repository

The version of the Docker package available in the Ubuntu base repository is a bit older than the version available in the official repository.

1. Install Docker from Official Docker Repository

Docker is now available in two editions,

  • Community Edition (CE)
  • Enterprise Edition (EE)

Here, we will install Docker Comunity Edition (CE).

Prerequisites

Uninstall the older versions of Docker package, named docker or docker-engine or docker.io along with associated dependencies.

If the system does not have Docker packages, skip the below step.

sudo apt -y remove docker docker-engine docker.io

Contents such as images, volumes, and networks under /var/lib/docker/ are preserved.

Setup Docker Repository

Update the repository cache.

sudo apt update

Install the below packages to ensure the apt work with the https method, and CA certificates are installed.

sudo apt install -y apt-transport-https software-properties-common ca-certificates curl wget

Add the GPG key for the Docker repository on your system.

wget https://download.docker.com/linux/ubuntu/gpg 

sudo apt-key add gpg

Now, add the official Docker repository by running the below command in the terminal.

echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list

Update the apt database.

sudo apt update

Make sure you are installing the docker package from the official repository.

sudo apt-cache policy docker-ce

Output:

docker-ce:
  Installed: (none)
  Candidate: 5:19.03.5~3-0~ubuntu-bionic
  Version table:
     5:19.03.5~3-0~ubuntu-bionic 500
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
     5:19.03.4~3-0~ubuntu-bionic 500
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
     5:19.03.3~3-0~ubuntu-bionic 500
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages

Install Docker

Now, install the Docker using the following command.

sudo apt install -y docker-ce

Now you have Docker installed on your machine, start the Docker service in case if it is not started automatically after the installation

sudo systemctl start docker

sudo systemctl enable docker

Verify the Docker version.

docker --version

Output:

Docker version 19.03.5, build 633a0ea838

2. Install Docker from Ubuntu Repository

You can use the apt command to install the Docker from the Ubuntu base repository.

sudo apt update

sudo apt install -y docker.io

Start the docker service and enable it to start automatically on system startup.

sudo systemctl start docker

sudo systemctl enable docker

Now, verify the docker version.

docker --version

Output:

Docker version 18.09.7, build 2d0083d

Run Docker Containers

Run a docker container using the docker run command to download and start the container.

sudo docker run hello-world

Output: This confirms that Docker is correctly installed.

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Allow Non-root user to run Docker

By default, normal users (non-root) would require a privilege equivalent to root to run docker commands. So, you have to run docker commands with sudo or give them privileges to run a Docker.

Follow the below steps to grant privileges equivalent to the root user for running the docker commands.

Create a group called docker if it does not exist.

sudo groupadd docker

Add your user to the docker group, replace raj with your username.

sudo useradd -m raj

Add a user to the docker group.

sudo usermod -aG docker raj

Log out and log back in.

You can now execute Docker commands without prefixing sudo.

$ docker run hello-world

Interested Topics

Docker Basic Topics

1: Top Important Docker Commands – Working with Docker Containers

2: Working with Docker Images – Building Docker Images

3: How to Build Docker Images with DockerFile

Docker Advanced Topics

1: How to Setup Docker Private Registry on CentOS 7

2: How to Install and Configure Docker Swarm on CentOS 7

Conclusion

That’s All. Please share your feedbacks in the comments section.

dockerubuntu 18.04
Comments (0)
Add Comment