Step 5: Install Docker

Docker Overview

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications to be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application. Read more on opensource.com’s What is Docker? article.

View these pages to learn more about Docker

Install Docker

Docker is not installed by default. So, we must install it and then verify that it works properly.

There are several versions of docker. We will use docker.io because is easier to set up.

  1. We should refresh the apt packages to ensure that we are installing the latest version.

  2. Then, we will install docker.io using flag -y to install docker and any required packages automatically.

    sudo apt update
    sudo apt install -y docker.io
    
     1 root@vps298933:~# sudo apt install docker.io
     2 Reading package lists... Done
     3 Building dependency tree
     4 Reading state information... Done
     5 The following additional packages will be installed:
     6 bridge-utils cgroupfs-mount libltdl7 pigz ubuntu-fan
     7 Suggested packages:
     8 ifupdown aufs-tools debootstrap docker-doc rinse zfs-fuse | zfsutils
     9 The following NEW packages will be installed:
    10 bridge-utils cgroupfs-mount docker.io libltdl7 pigz ubuntu-fan
    11 0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
    12 Need to get 40.3 MB of archives.
    13 After this operation, 198 MB of additional disk space will be used.
    14 Do you want to continue? [Y/n] y
    15 Get:1 http://nova.clouds.archive.ubuntu.com/ubuntu bionic/universe amd64 pigz amd64 2.4-1 [57.4 kB]
    16 Get:2 http://nova.clouds.archive.ubuntu.com/ubuntu bionic/main amd64 bridge-utils amd64 1.5-15ubuntu1 [30.1 kB]
    17 Get:3 http://nova.clouds.archive.ubuntu.com/ubuntu bionic/universe amd64 cgroupfs-mount all 1.4 [6,320 B]
    18 .
    19 .
    20 .
    21 Setting up docker.io (18.06.1-0ubuntu1~18.04.1) ...
    22 Adding group 'docker' (GID 115) ...
    23 Done.
    24 Created symlink /etc/systemd/system/sockets.target.wants/docker.socket ? /lib/systemd/system/docker.socket.
    25 Processing triggers for ureadahead (0.100.0-20) ...
    26 Processing triggers for libc-bin (2.27-3ubuntu1) ...
    27 Processing triggers for systemd (237-3ubuntu10.11) ...
    28 root@vps298933:~#
    
  3. Enable Docker so it will start on reboot

    sudo systemctl enable docker
    
    1 root@vps298933:~# sudo systemctl enable docker
    2 Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
    3 Executing: /lib/systemd/systemd-sysv-install enable docker
    
  4. View the status of the application. You should see this line: Active: active (running)

    sudo systemctl status docker
    
     1  root@vps298933:~/sphinx-server#
     2  root@vps298933:~/sphinx-server# systemctl status docker
     3  ● docker.service - Docker Application Container Engine
     4  Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     5  Active: active (running) since Sat 2019-03-16 11:18:56 +06; 3h 21min ago
     6      Docs: https://docs.docker.com
     7  Main PID: 900 (dockerd)
     8      Tasks: 15
     9  CGroup: /system.slice/docker.service
    10          +-  900 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
    11          +-10992 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8000 -container-ip 172.17.0.2 -container-p
    12
    13  Mar 16 11:18:55 vps298933 dockerd[900]: time="2019-03-16T11:18:55.564648939+06:00" level=info msg="Default bridge (docker0
    14  Mar 16 11:18:55 vps298933 dockerd[900]: time="2019-03-16T11:18:55.652024810+06:00" level=info msg="Loading containers: don
    15  Mar 16 11:18:56 vps298933 dockerd[900]: time="2019-03-16T11:18:56.070100870+06:00" level=info msg="Docker daemon" commit=7
    16  Mar 16 11:18:56 vps298933 dockerd[900]: time="2019-03-16T11:18:56.087081485+06:00" level=info msg="Daemon has completed in
    17  Mar 16 11:18:56 vps298933 systemd[1]: Started Docker Application Container Engine.
    18  Mar 16 11:18:56 vps298933 dockerd[900]: time="2019-03-16T11:18:56.126780847+06:00" level=info msg="API listen on /var/run/
    19  Mar 16 12:35:13 vps298933 dockerd[900]: time="2019-03-16T12:35:13.895938839+06:00" level=error msg="stream copy error: rea
    20  Mar 16 12:35:13 vps298933 dockerd[900]: time="2019-03-16T12:35:13.968707954+06:00" level=error msg="Error running exec 01d
    21  Mar 16 12:35:21 vps298933 dockerd[900]: time="2019-03-16T12:35:21.967738507+06:00" level=error msg="stream copy error: rea
    22  Mar 16 12:35:22 vps298933 dockerd[900]: time="2019-03-16T12:35:22.035983646+06:00" level=error msg="Error running exec 804
    

    Note

    You might need to press Ctrl+C or :q to end the status command if it did not return you back to your prompt.

Verify Docker Install

  1. View the version of Docker. You should see a similar version.

    docker --version
    
    1 root@vps298933:~# docker --version
    2 Docker version 19.03.5, build 633a0ea838
    
  2. View the available options using the --help command

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

    sudo docker info
    
     1 root@vps298933:~# docker info
     2 Containers: 0
     3 Running: 0
     4 Paused: 0
     5 Stopped: 0
     6 Images: 0
     7 Server Version: 18.06.1-ce
     8 Storage Driver: overlay2
     9 Backing Filesystem: extfs
    10 Supports d_type: true
    11 Native Overlay Diff: true
    12 Logging Driver: json-file
    13 Cgroup Driver: cgroupfs
    14 Plugins:
    15 Volume: local
    16 Network: bridge host macvlan null overlay
    17 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
    18 Swarm: inactive
    19 Runtimes: runc
    20 Default Runtime: runc
    21 Init Binary: docker-init
    22 containerd version:  (expected: 468a545b9edcd5932818eb9de8e72413e616e86e)
    23 runc version: N/A (expected: 69663f0bd4b60df09991c08812a60108003fa340)
    24 init version: v0.18.0 (expected: fec3683b971d9c3ef73f284f176672c44b448662)
    25 Security Options:
    26 apparmor
    27 seccomp
    28 Profile: default
    29 Kernel Version: 4.15.0-43-generic
    30 Operating System: Ubuntu 18.04.1 LTS
    31 OSType: linux
    32 Architecture: x86_64
    33 CPUs: 1
    34 Total Memory: 3.759GiB
    35 Name: vps298933
    36 ID: H2AG:BHBS:OKEZ:UJZ7:S2JB:MXP5:WYKU:42BD:SEOK:VDBS:2VTH:MTTA
    37 Docker Root Dir: /var/lib/docker
    38 Debug Mode (client): false
    39 Debug Mode (server): false
    40 Registry: https://index.docker.io/v1/
    41 Labels:
    42 Experimental: false
    43 Insecure Registries:
    44 127.0.0.0/8
    45 Live Restore Enabled: false
    46
    47 WARNING: No swap limit support
    
  4. Verify the hello-world container runs

    sudo docker run hello-world
    
     1 root@vps298933:~# docker run hello-world
     2 Unable to find image 'hello-world:latest' locally
     3 latest: Pulling from library/hello-world
     4 1b930d010525: Pull complete
     5 Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
     6 Status: Downloaded newer image for hello-world:latest
     7
     8 Hello from Docker!
     9 This message shows that your installation appears to be working correctly.
    10
    11 To generate this message, Docker took the following steps:
    12 1. The Docker client contacted the Docker daemon.
    13 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64)
    14 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
    15 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
    16
    17 To try something more ambitious, you can run an Ubuntu container with:
    18 $ docker run -it ubuntu bash
    19
    20 Share images, automate workflows, and more with a free Docker ID:
    21 https://hub.docker.com/
    22
    23 For more examples and ideas, visit:
    24 https://docs.docker.com/get-started/
    
  5. View the docker images. You should see the hello-world image under the REPOSITORY column.

    sudo docker images
    
    1 root@vps298933:~# sudo docker images
    2 REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    3 hello-world         latest              fce289e99eb9        2 weeks ago         1.84kB
    
  6. View the active containers (stopped or running)

    • docker ps shows the active containers

    • docker ps -a shows the active (running) and inactive (stopped) containers

    Note

    Some containers stay running in the background (daemon mode). Others operate as a process. They start, process some data, and then exit.

    sudo docker ps
    

    Notice that there are no running containers.

    1 root@vps298933:~# docker ps
    2 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    
    sudo docker ps -a
    

    Notice that the container has exited.

    1 root@vps298933:~# sudo docker ps -a
    2 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS               NAMES
    3 67a9ee91dbfc        hello-world         "/hello"            6 days ago          Exited (0) 6 days ago                       nervous_dijkstra
    4 root@vps298933:~#
    

Running Docker as non-root

If your VPS does not have a root login or you prefer to use a user account, you can configure Docker to run without out using sudo.

  1. Find your username using whoami

    whoami
    
  2. Add your user to docker group.

    sudo groupadd docker
    sudo usermod -aG docker username
    sudo newgrp docker