How to Install Docker On Ubuntu 18.04 Bionic Beaver

Objective

Install the latest Docker release on Ubuntu 18.04

Distributions

Ubuntu 18.04 Bionic Beaver

Requirements

A working install of Ubuntu 18.04 with root privileges

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Other Versions of this Tutorial

Ubuntu 20.04 (Focal Fossa)

Introduction

Docker has revolutionized how web applications are hosted and servers are run. Docker containers allow server administrators to compartmentalize their applications like virtual machines, but containers are much lighter weight, are easier to manager, and add less overhead.

Installing Docker on Ubuntu is very simple. Because Ubuntu is a popular choice for the cloud, the entire process has been streamlined to a science.

Install Docker from Ubuntu Repository

Installation from the standard Ubuntu repository consists of a single apt command. It may yield stable but lower docker version number:

$ sudo apt install docker.io

The following linux commands will start Docker and ensure that starts after the reboot:

$ sudo systemctl start docker
$ sudo systemctl enable docker

All done.

$ docker --version
Docker version 17.03.2-ce, build f5ec1e2


Install Docker from the Official Docker Repository

Install the Dependencies

Docker has its own repositories. Before you can install it from those repos, you need to install the prerequisite dependencies. Update your system, and grab them with Apt.

>$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add The Docker Repository

Create a new file for the Docker repository at /etc/apt/sources.list.d/docker.list. In that file, place one of the following lines choosing either stable, nightly or edge builds:

STABLE please check availability before using:
deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable
EDGE:
deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic edge
NIGHTLY:
deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic nightly

Next, you need to add Docker’s GPG key.

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Once, that’s imported, update Apt again.

$ sudo apt update

Install Docker CE

You can simply install the Docker CE package.

$ sudo apt install docker-ce

Done. Check for docker version:

$ docker --version
Docker version 18.03.0-ce, build 0520e24

Add A Container

There’s more than one way to add a Docker container. If you’re familiar with Docker, this isn’t for you. However, if you’ve decided to give Docker a try for the first time, the easiest way to get started is to use any of the excellent existing Docker images available online. Docker is configured to pull them automatically for you when you request them by with the name of the developer and the repository.

$ sudo docker run username:repository

In the case of officially supported images, you don’t need to specify a username. They do tend to come with more options, though. You might want to specify tags for them. That’s done with a colon after the repository name.

$ sudo docker run wordpress:php-7.2-fpm-alpine

You can also specify the port that the container listens on. By default, most containers are configured to listen on 80 internally. You can make Docker listen on a different port and forward that to the container’s 80.

$ sudo docker run 9000:80 wordpress

That container will listen on the server’s port 9000 and forward the traffic to the container’s port 80. This makes it easier to run multiple web services on the same server.



Managing Containers

Managing containers is fairly straightforward too. You can probably already guess that starting up a container is the same as adding a new one. That’s the run command. In this case, the -d flag detaches the container, so it’s not taking up your terminal.

$ sudo docker run -d wordpress

Since you can have multiples of the same container running, almost everything else here uses the hashes that Docker automatically assigns to running containers.

To list out all the containers running on your system, use the ls option.

$ sudo docker container ls

If you want to stop a container, use stop followed by the hash of that container.

$ sudo docker container stop HASH

Removing a container is easy too. Use rm.

$ sudo docker container rm HASH

Closing Thoughts

Docker is a powerful tool. There are a lot more options, and Docker is capable of a wide range of configurations. With the information here, though, you can get started with Docker on Ubuntu and use it effectively in most basic situations.