{{announcement.body}}
{{announcement.title}}

How to Install Docker on Ubuntu 18.04

DZone 's Guide to

How to Install Docker on Ubuntu 18.04

In this article, we demonstrate how to install Docker on Ubuntu 18.04 and review some basic Docker commands.

· Cloud Zone ·
Free Resource

In this article, you’ll install and use Docker Community Edition (CE) on Ubuntu 18.04. 

Prerequisites

  • Ubuntu 18.04 64-bit operating system.
  • A user account with sudo privileges

Installing Docker

The Docker installation package available in the official Ubuntu repository may not be the latest version. To ensure we get the latest version, we’ll install Docker from the official Docker repository. To do that, we’ll add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package.

It’s a good idea to update the local database of software to make sure you’ve got access to the latest revisions

Update your existing list of packages by typing:

Shell
 




x


1
$ sudo apt update



Next, install a few prerequisite packages which let apt use packages over HTTPS:

Shell
 




xxxxxxxxxx
1


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



Then add the GPG key for the official Docker repository to your system:

Shell
 




xxxxxxxxxx
1


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



Add the Docker repository to APT sources:

Shell
 




xxxxxxxxxx
1


1
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"



Next, update the package database with the Docker packages from the newly added repo:

Shell
 




xxxxxxxxxx
1


1
$ sudo apt update



Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:

Shell
 




xxxxxxxxxx
1


 
1
$ apt-cache policy docker-ce



You’ll see output like this:

Shell
 




xxxxxxxxxx
1
54


 
1
docker-ce:
2
  Installed: 5:19.03.8~3-0~ubuntu-bionic
3
  Candidate: 5:19.03.8~3-0~ubuntu-bionic
4
  Version table:
5
 *** 5:19.03.8~3-0~ubuntu-bionic 500
6
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
7
        100 /var/lib/dpkg/status
8
     5:19.03.7~3-0~ubuntu-bionic 500
9
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
10
     5:19.03.6~3-0~ubuntu-bionic 500
11
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
12
     5:19.03.5~3-0~ubuntu-bionic 500
13
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
14
     5:19.03.4~3-0~ubuntu-bionic 500
15
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
16
     5:19.03.3~3-0~ubuntu-bionic 500
17
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
18
     5:19.03.2~3-0~ubuntu-bionic 500
19
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
20
     5:19.03.1~3-0~ubuntu-bionic 500
21
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
22
     5:19.03.0~3-0~ubuntu-bionic 500
23
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
24
     5:18.09.9~3-0~ubuntu-bionic 500
25
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
26
     5:18.09.8~3-0~ubuntu-bionic 500
27
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
28
     5:18.09.7~3-0~ubuntu-bionic 500
29
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
30
     5:18.09.6~3-0~ubuntu-bionic 500
31
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
32
     5:18.09.5~3-0~ubuntu-bionic 500
33
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
34
     5:18.09.4~3-0~ubuntu-bionic 500
35
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
36
     5:18.09.3~3-0~ubuntu-bionic 500
37
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
38
     5:18.09.2~3-0~ubuntu-bionic 500
39
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
40
     5:18.09.1~3-0~ubuntu-bionic 500
41
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
42
     5:18.09.0~3-0~ubuntu-bionic 500
43
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
44
     18.06.3~ce~3-0~ubuntu 500
45
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
46
     18.06.2~ce~3-0~ubuntu 500
47
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
48
     18.06.1~ce~3-0~ubuntu 500
49
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
50
     18.06.0~ce~3-0~ubuntu 500
51
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
52
     18.03.1~ce~3-0~ubuntu 500
53
        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages


Notice that docker-ce is not installed, but the candidate for installation is from the Docker repository for Ubuntu 18.04 (bionic).

Finally, install Docker:

Shell
 




xxxxxxxxxx
1


1
$ sudo apt install docker-ce



Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:

Shell
 




xxxxxxxxxx
1


 
1
$ sudo systemctl status docker



The output should be similar to the following, showing that the service is active and running:

Shell
 




xxxxxxxxxx
1


1
docker.service - Docker Application Container Engine
2
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
3
   Active: active (running) since Fri 2020-05-01 07:17:48 UTC; 29min ago
4
     Docs: https://docs.docker.com
5
 Main PID: 3402 (dockerd)
6
    Tasks: 8
7
   CGroup: /system.slice/docker.service
8
           └─3402 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
9
 
          


Add Your User to Docker Group

By default, the Docker command can only be run the root user. If you attempt to run the Docker command without  adding a user to the Docker group, you’ll get an output like this: 

Shell
 




x


 
1
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied


If you want to avoid the above error, add your username to the Docker group:

Shell
 




xxxxxxxxxx
1


1
$ sudo usermod -aG docker ${USER}


To apply for the new group membership, log out of the server, and log in. To confirm that your user is added to the Docker group by typing:

Shell
 




x
32
9


 
1
$ id -nG
2
Output:
3
ubuntu adm dialout cdrom floppy sudo audio dip video plugdev lxd netdev docker 


  

Let’s explore the Docker commands.

Docker Command Syntax

Docker consists of options followed by the command.

Usage:  docker [OPTIONS] COMMAND

Shell
 




x


1
A self-sufficient runtime for containers
2
 
          
3
Options:
4
      --config string      Location of client config files (default "/home/ubuntu/.docker")
5
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
6
  -D, --debug              Enable debug mode
7
  -H, --host list          Daemon socket(s) to connect to
8
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
9
      --tls                Use TLS; implied by --tlsverify
10
      --tlscacert string   Trust certs signed only by this CA (default "/home/ubuntu/.docker/ca.pem")
11
      --tlscert string     Path to TLS certificate file (default "/home/ubuntu/.docker/cert.pem")
12
      --tlskey string      Path to TLS key file (default "/home/ubuntu/.docker/key.pem")
13
      --tlsverify          Use TLS and verify the remote
14
  -v, --version            Print version information and quit
15
 
          
16
Management Commands:
17
  builder     Manage builds
18
  config      Manage Docker configs
19
  container   Manage containers
20
  context     Manage contexts
21
  engine      Manage the docker engine
22
  image       Manage images
23
  network     Manage networks
24
  node        Manage Swarm nodes
25
  plugin      Manage plugins
26
  secret      Manage Docker secrets
27
  service     Manage services
28
  stack       Manage Docker stacks
29
  swarm       Manage Swarm
30
  system      Manage Docker
31
  trust       Manage trust on Docker images
32
  volume      Manage volumes
33
 
          
34
Commands:
35
  attach      Attach local standard input, output, and error streams to a running container
36
  build       Build an image from a Dockerfile
37
  commit      Create a new image from a container's changes
38
  cp          Copy files/folders between a container and the local filesystem
39
  create      Create a new container
40
  diff        Inspect changes to files or directories on a container's filesystem
41
  events      Get real time events from the server
42
  exec        Run a command in a running container
43
  export      Export a container's filesystem as a tar archive
44
  history     Show the history of an image
45
  images      List images
46
  import      Import the contents from a tarball to create a filesystem image
47
  info        Display system-wide information
48
  inspect     Return low-level information on Docker objects
49
  kill        Kill one or more running containers
50
  load        Load an image from a tar archive or STDIN
51
  login       Log in to a Docker registry
52
  logout      Log out from a Docker registry
53
  logs        Fetch the logs of a container
54
  pause       Pause all processes within one or more containers
55
  port        List port mappings or a specific mapping for the container
56
  ps          List containers
57
  pull        Pull an image or a repository from a registry
58
  push        Push an image or a repository to a registry
59
  rename      Rename a container
60
  restart     Restart one or more containers
61
  rm          Remove one or more containers
62
  rmi         Remove one or more images
63
  run         Run a command in a new container
64
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
65
  search      Search the Docker Hub for images
66
  start       Start one or more stopped containers
67
  stats       Display a live stream of container(s) resource usage statistics
68
  stop        Stop one or more running containers
69
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
70
  top         Display the running processes of a container
71
  unpause     Unpause all processes within one or more containers
72
  update      Update configuration of one or more containers
73
  version     Show the Docker version information
74
  wait        Block until one or more containers stop, then print their exit codes
75
 
          
76
Run 'docker COMMAND --help' for more information on a command.


Conclusion

In this article, you installed Docker on Ubuntu. In the next article, we talk about basic Docker commands and usage.

Topics:
cloud, docker ce, docker commands, docker installation, ubuntu

Published at DZone with permission of Krishna Prasad Kalakodimi . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}