How to Install Docker on Ubuntu 18.04

In this tutorial we are going to learn how to install Docker on Ubuntu 18.04. Docker is container platform that provides an isolated environment for applications and separate application from the operating system using containers.

How to Install Docker on Ubuntu 18.04

Note that, the default Ubuntu package repository includes the docker package (docker.io) in it, but it's not the latest version. in order to install the latest version will use the apt repository provided by the docker developers.

Before we begin, Uninstall any older version of Docker engine(If you installed previously).

apt-get remove docker.io

How to do it...

The steps to install Docker on Ubuntu 18.04 are as follows:

  1. Set up Docker Ubuntu apt repository.
  2. Install the latest version of Docker.

Log in to the Ubuntu shell and run the following set of commands to enable docker apt repository for Ubuntu 18.04:

apt-get update
apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

With docker repository in place, now run the apt-get command and install the docker-ce package:

apt-get update
apt-get install docker-ce

After all operations are completed, docker engine is up and running on your Ubuntu 18.04 server. You can run docker version command to check the version of docker running on your system:

docker version
check docker version on Ubuntu 18.04

To view the status of the docker service, run:

systemctl status docker.service
Ubuntu 18 docker status

We can use the systemctl command to start, restart and stop docker engine in Ubuntu 18.04. For example, Stop the docker engine with following command:

systemctl stop docker

By default, docker starts automatically on server reboot. If you don't want to start the service automatically, disable the docker service:

systemctl disable docker

If you want to start the service automatically, then the docker service should be enabled:

systemctl enable docker

Run Docker Containers on Ubuntu 18.04

We need images in order to start new containers (We can download images with docker pull command.). For example, To run nginx on Docker, first, we need to download nginx image:

docker pull nginx:latest

Then, we start a new nginx container using the nginx image we downloaded:

docker run -d --name ubuntu-nginx -p 80:80 nginx:latest

In the above example, we started a new container called ubuntu-nginx. We also mapped port 80 of the host machine to port 80 of the ubuntu-nginx container.

To get a list of running containers on your Ubuntu 18.04 Server, Type:

docker ps

To view all containers (Both running and stopped), Type:

docker ps -a

Also read: How to Run Docker as non root user without sudo command