How to Install And Use Docker on Ubuntu 16.04/18.04 Linux
This post will guide you how to install and use Docker from the default Ubuntu repository on your Ubuntu Linux server. How do I install the latest version of Docker from the official Docker repository on Ubuntu 16.04 or 18.04 Linux.
- What is Docker?
- Installing Docker from the Default Ubuntu Repository
- Installing Docker from the Official Docker Repository
- Searching Docker Images
- Downloading Docker Images
- Running a Docker Container
- Managing Docker Containers
- Running Docker Command as a Non-root User
What is Docker?
Docker is a computer program that performs operating-system-level virtualization. It was first released in 2013 and is developed by Docker, Inc. Docker is used to run software packages called containers.
Docker is an open source project and it can be create a lighweight container to run applications, it is like virtual machines, and it is more portable, more resource-friendly and more dependent on the host operating system. Now it is maintained by the docker commnunity and Dock Inc.
Installing Docker from the Default Ubuntu Repository
The Docker installation package is available in the default Ubuntu repository, but it may not be the latest version. To install Docker on your system, you can do the following steps:
#1 You need to update the APT cache to fetch the latest updates from the official Ubuntu repository and install them with the following command:
$ sudo apt update $ sudo apt upgrade
Then you can ready to install Docker tool on your Ubuntu system.
#2 If you want to install Docker tool from the default Ubuntu repository, just run the following apt command:
$ sudo apt install docker.io
Outputs:
devops@devops-osetc:~$ sudo apt install docker.io [sudo] password for devops: Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: augeas-lenses caffe-tools-cuda cpu-checker cryptsetup cryptsetup-bin cython3 db-util db5.3-util dmeventd extlinux hfsplus icoutils ipxe-qemu ipxe-qemu-256k-compat-efi-roms ldmtool libafflib0v5 libaugeas0 libbfio1 libblosc1 libcacard0 libcaffe-cuda1 libconfig9 libcublas9.1 libcudart9.1 libcurand9.1 libdate-manip-perl libdevmapper-event1.02.1 libewf2 libfdt1 libhfsp0 libhivex0 libintl-perl libintl-xs-perl libiscsi7 libldm-1.0-0 liblvm2app2.2 liblvm2cmd2.02 librados2 librbd1 libsdl1.2debian libspice-server1 libssl-doc libstring-shellquote-perl libsys-virt-perl libtsk13 libusbredirparser1 libvirt0 libwin-hivex-perl libxen-4.9 libxenstore3.0 lsscsi lvm2 msr-tools osinfo-db python-tables-data python3-caffe-cuda python3-cycler python3-dateutil python3-decorator python3-gflags python3-h5py python3-ipython python3-ipython-genutils python3-leveldb python3-matplotlib python3-networkx python3-nose python3-numexpr python3-numpy python3-pandas python3-pandas-lib python3-pickleshare python3-prompt-toolkit python3-pygments python3-pywt python3-scipy python3-simplegeneric python3-skimage python3-skimage-lib python3-tables python3-tables-lib python3-traitlets python3-wcwidth qemu-block-extra qemu-system-common qemu-system-x86 qemu-utils scrub seabios sgabios sleuthkit supermin zerofree Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: bridge-utils cgroupfs-mount pigz ubuntu-fan Suggested packages: aufs-tools debootstrap docker-doc rinse zfs-fuse | zfsutils The following NEW packages will be installed: bridge-utils cgroupfs-mount docker.io pigz ubuntu-fan 0 upgraded, 5 newly installed, 0 to remove and 2 not upgraded. Need to get 46.6 MB of archives. After this operation, 235 MB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 pigz amd64 2.4-1 [57.4 kB] Get:2 http://mirrors.aliyun.com/ubuntu bionic/main amd64 bridge-utils amd64 1.5-15ubuntu1 [30.1 kB] Get:3 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 cgroupfs-mount all 1.4 [6,320 B] Get:4 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 docker.io amd64 18.09.2-0ubuntu1~18.04.1 [46.4 MB] Get:5 http://mirrors.aliyun.com/ubuntu bionic/main amd64 ubuntu-fan all 0.12.10 [34.7 kB] Fetched 46.6 MB in 15s (3,020 kB/s) Preconfiguring packages ... Selecting previously unselected package pigz. (Reading database ... 241865 files and directories currently installed.) Preparing to unpack .../archives/pigz_2.4-1_amd64.deb ... Unpacking pigz (2.4-1) ... Selecting previously unselected package bridge-utils. Preparing to unpack .../bridge-utils_1.5-15ubuntu1_amd64.deb ... Unpacking bridge-utils (1.5-15ubuntu1) ... Selecting previously unselected package cgroupfs-mount. Preparing to unpack .../cgroupfs-mount_1.4_all.deb ... Unpacking cgroupfs-mount (1.4) ... Selecting previously unselected package docker.io. Preparing to unpack .../docker.io_18.09.2-0ubuntu1~18.04.1_amd64.deb ... Unpacking docker.io (18.09.2-0ubuntu1~18.04.1) ... Selecting previously unselected package ubuntu-fan. Preparing to unpack .../ubuntu-fan_0.12.10_all.deb ... Unpacking ubuntu-fan (0.12.10) ... Setting up docker.io (18.09.2-0ubuntu1~18.04.1) ... Adding group `docker' (GID 134) ... Done. Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket. Processing triggers for ureadahead (0.100.0-20) ... Setting up cgroupfs-mount (1.4) ... Setting up bridge-utils (1.5-15ubuntu1) ... Setting up ubuntu-fan (0.12.10) ... Created symlink /etc/systemd/system/multi-user.target.wants/ubuntu-fan.service → /lib/systemd/system/ubuntu-fan.service. Processing triggers for systemd (237-3ubuntu10.16) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Setting up pigz (2.4-1) ... Processing triggers for ureadahead (0.100.0-20) ... devops@devops-osetc:~$ echo $? 0
After the installation has been completed, you can start and enable Docker service with the systemctl command, type:
$ sudo systemctl start docker.service $ sudo systemctl enable docker.service
Outputs:
devops@devops-osetc:~$ sudo systemctl enable docker Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable docker
Then you can check the docker version with the following docker command:
$ docker --version
Outputs:
devops@devops-osetc:~$ docker --version
Docker version 18.09.2, build 6247962
You would notice that the current docker version is 18.09.2.
Installing Docker from the Official Docker Repository
If you wish to install the latest version of Docker on your Ubuntu system, you have to install docker from the official Docker Repository. Here are the steps:
#1 you need to remove all default Docker packges from your system before installing Docker on your system. Run the following command to remove unnecessary Docker packages:
$ sudo apt purge docker docker.io
Outputs:
devops@devops-osetc:~$ sudo apt purge docker docker.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker' is not installed, so not removed
Do you want to continue? [Y/n] Y
(Reading database ... 242139 files and directories currently installed.)
Removing docker.io (18.09.2-0ubuntu1~18.04.1) ...
'/usr/share/docker.io/contrib/nuke-graph-directory.sh' -> '/var/lib/docker/nuke-graph-directory.sh'
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
(Reading database ... 241936 files and directories currently installed.)
Purging configuration files for docker.io (18.09.2-0ubuntu1~18.04.1) ...
Nuking /var/lib/docker ...
(if this is wrong, press Ctrl+C NOW!)
+ sleep 10
+ rm -rf /var/lib/docker/builder /var/lib/docker/buildkit /var/lib/docker/containerd /var/lib/docker/containers /var/lib/docker/image /var/lib/docker/network /var/lib/docker/nuke-graph-directory.sh /var/lib/docker/overlay2 /var/lib/docker/plugins /var/lib/docker/runtimes /var/lib/docker/swarm /var/lib/docker/tmp /var/lib/docker/trust /var/lib/docker/volumes
dpkg: warning: while removing docker.io, directory '/etc/docker' not empty so not removed
Processing triggers for ureadahead (0.100.0-20) ...
Processing triggers for systemd (237-3ubuntu10.16) ...
#2 installing some dependencies for Docker-ce on Ubuntu system, run the following apt command:
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
#3 adding the GPG key for the official Docker repository to verify packages signature on your Ubuntu system, type:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Outputs:
devops@devops-osetc:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - OK
#4 adding the Docker Repository to the default Ubuntu APT source lists, type:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Outputs:
devops@devops-osetc:~$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Get:1 https://download.docker.com/linux/ubuntu bionic InRelease [64.4 kB]
Hit:2 http://mirrors.aliyun.com/ubuntu bionic InRelease
Get:3 http://mirrors.aliyun.com/ubuntu bionic-security InRelease [88.7 kB]
Get:4 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:6 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease [242 kB]
Reading package lists... Done
#5 update the APT index with the Docker packages from the newly added Docker repository,type:
$ sudo apt update
#6 make sure that the docker-ce will be installed from the Docker repo instead of the default Ubuntu repo using the apt-cache command:
$ apt-cache policy docker-ce
Outputs:
devops@devops-osetc:~$ apt-cache policy docker-ce
docker-ce:
Installed: (none)
Candidate: 5:18.09.3~3-0~ubuntu-bionic
Version table:
5:18.09.3~3-0~ubuntu-bionic 500
500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
5:18.09.2~3-0~ubuntu-bionic 500
500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
From the above outputs, you would notice that the candidate version will be installed from docker repo.
#7 start to install the Docker community edition on your Ubuntu system,type:
$ sudo apt install docker-ce
Outputs:
devops@devops-osetc:~$ sudo apt install docker-ce
[sudo] password for devops:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
aufs-tools containerd.io docker-ce-cli
The following NEW packages will be installed:
aufs-tools containerd.io docker-ce docker-ce-cli
0 upgraded, 4 newly installed, 0 to remove and 37 not upgraded.
Need to get 50.6 MB of archives.
After this operation, 243 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 https://download.docker.com/linux/ubuntu bionic/stable amd64 containerd.io amd64 1.2.4-1 [19.9 MB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 aufs-tools amd64 1:4.9+20170918-1ubuntu1 [104 kB]
Get:3 https://download.docker.com/linux/ubuntu bionic/stable amd64 docker-ce-cli amd64 5:18.09.3~3-0~ubuntu-bionic [13.1 MB]
Get:4 https://download.docker.com/linux/ubuntu bionic/stable amd64 docker-ce amd64 5:18.09.3~3-0~ubuntu-bionic [17.4 MB]
Fetched 50.6 MB in 20s (2,495 kB/s)
Selecting previously unselected package aufs-tools.
Selecting previously unselected package aufs-tools.
(Reading database ... 241941 files and directories currently installed.)
Preparing to unpack .../aufs-tools_1%3a4.9+20170918-1ubuntu1_amd64.deb ...
Unpacking aufs-tools (1:4.9+20170918-1ubuntu1) ...
Selecting previously unselected package containerd.io.
Preparing to unpack .../containerd.io_1.2.4-1_amd64.deb ...
Unpacking containerd.io (1.2.4-1) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../docker-ce-cli_5%3a18.09.3~3-0~ubuntu-bionic_amd64.deb ...
Unpacking docker-ce-cli (5:18.09.3~3-0~ubuntu-bionic) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../docker-ce_5%3a18.09.3~3-0~ubuntu-bionic_amd64.deb ...
Unpacking docker-ce (5:18.09.3~3-0~ubuntu-bionic) ...
Setting up aufs-tools (1:4.9+20170918-1ubuntu1) ...
Setting up containerd.io (1.2.4-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Processing triggers for ureadahead (0.100.0-20) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for systemd (237-3ubuntu10.16) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up docker-ce-cli (5:18.09.3~3-0~ubuntu-bionic) ...
Setting up docker-ce (5:18.09.3~3-0~ubuntu-bionic) ...
update-alternatives: using /usr/bin/dockerd-ce to provide /usr/bin/dockerd (dockerd) in auto mode
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
Processing triggers for systemd (237-3ubuntu10.16) ...
Processing triggers for ureadahead (0.100.0-20) ...
devops@devops-osetc:~$ echo $?
0
#8 Docker-ce should be installed on your system, you can check if the Docker daemon is active or not, type:
$ sudo systemctl status docker.service
Outputs:
devops@devops-osetc:~$ sudo systemctl status docker.service ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2019-03-26 03:03:19 EDT; 5min ago Docs: https://docs.docker.com Main PID: 3962 (dockerd) Tasks: 8 CGroup: /system.slice/docker.service └─3962 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
From the above outputs, you can see that the Docker service is active and running.
Searching Docker Images
You can use docker search command to search for available Docker container images from Docker hub, for example, you want to create a docker container with Ubuntu os, you can search for the Ubuntu image using docker command with the search subcommand,type:
$ sudo docker search ubuntu
Outputs:
devops@devops-osetc:~$ sudo docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 9339 [OK] dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 283 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 209 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 165 [OK] ubuntu-upstart Upstart is an event-based replacement for th… 96 [OK] ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 96 [OK] neurodebian NeuroDebian provides neuroscience research s… 56 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK] ubuntu-debootstrap debootstrap --variant=minbase --components=m… 40 [OK] nuagebec/ubuntu Simple always updated Ubuntu docker images w… 23 [OK] i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 17
From the above outputs, you should notice that docker search command will crawl Docker hub and return a listing of all matching images. Then you can choose one Ubuntu image that you would like to download using docker pull command.
Downloading Docker Images
Once we get the image name from the Docker Hub, you can execute the following docker command with the pull subcommand to download the Ubuntu image to your local disk. type:
$ sudo docker pull ubuntu
Outputs:
devops@devops-osetc:~$ sudo docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 6cf436f81810: Pull complete 987088a85b96: Pull complete b4624b3efe06: Pull complete d42beb8ded59: Pull complete Digest: sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210 Status: Downloaded newer image for ubuntu:latest
If you wish to see if the Ubuntu image has been downloaded successfully to your system, using the following command:
$ sudo cocker images
Outputs:
devops@devops-osetc:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 47b19964fb50 6 weeks ago 88.1MB
Running a Docker Container
To create a new container using the Ubuntu image downloaded above on your system, you can use docker run or docker create command, type:
$ sudo docker run -i -t ubuntu /bin/bash
Or
$ sudo docker create ubuntu
Note: docker create command will only create a new container but not start it. And docker run command will create and run a container on your Ubuntu system. You should see that your command prompt should change and you are working inside the newly created Ubuntu container.
Outputs:
root@21e5a0588b30:/#
Then you can run any commands inside this Ubuntu container. and you are in the root mode inside the container, so you do not need to prefix any command with sudo.
Managing Docker Containers
If you want to exit from the running docker container, you can type CTRL + P + Q to exit but the container is still running. And if you want to shutdown or exit the running container, you just run exit command inside the container.
To see the running Containers, you can use the following command:
$ sudo docker ps
Outputs:
devops@devops-osetc:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21e5a0588b30 ubuntu "/bin/bash" About an hour ago Up About an hour epic_davinci
To see all Containers including Active and inactive, just run the following docker ps command with -a option, type:
$ sudo docker ps -a
Outputs:
devops@devops-osetc:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e58a99daab5f ubuntu "/bin/bash" 24 seconds ago Exited (130) 11 seconds ago unruffled_galois 21e5a0588b30 ubuntu "/bin/bash" About an hour ago Up About an hour epic_davinci
To view the latest container you created, type the following command:
$ sudo docker ps -l
Outputs:
devops@devops-osetc:~$ sudo docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e58a99daab5f ubuntu "/bin/bash" 49 seconds ago Exited (130) 37 seconds ago unruffled_galois
To stop a Container without deleting it, using the following command:
$ sudo docker stop CONTAINER_ID
To start a stopped container its container id is e58a99daab5f, using docker start command following by the container ID, type:
$ sudo docker start e58a99daab5f
Outputs:
devops@devops-osetc:~$ sudo docker start e58a99daab5f e58a99daab5f devops@devops-osetc:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e58a99daab5f ubuntu "/bin/bash" 4 minutes ago Up 26 seconds unruffled_galois 21e5a0588b30 ubuntu "/bin/bash" 2 hours ago Up About an hour epic_davinci
If you do not need a inactive container, you can remove it using docker rm command followed by the container ID. You can use the docker ps -a command to find the container ID. type:
$ sudo docker rm e58a99daab5f
Outputs:
devops@devops-osetc:~$ sudo docker rm e58a99daab5f e58a99daab5f devops@devops-osetc:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21e5a0588b30 ubuntu "/bin/bash" 2 hours ago Up About an hour epic_davinci
Running Docker Command as a Non-root User
The docker command can only be run by the root user or by a user in the docker group. After Docker is installed, and the docker group has been created automatically. So if you want to run a docker command without sudo prefix, you can add the current login user to the docker group. Type the following command to add devops user to the docker group:
$ sudo usermod -aG docker devops
Or
$ sudo usermod -aG docker ${USER}
Then you need to re-login your system with devops user, and try to run one docker command:
devops@devops-osetc:~$ su devops Password: devops@devops-osetc:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21e5a0588b30 ubuntu "/bin/bash" 2 hours ago Up About an hour epic_davinci
Conclusion
You should know that how to install and use Docker tool on Ubuntu 16.04 or 18.04 from this guide, and you also know how to create a docker container with Ubuntu os. If you want to see more information about Docker, you can go the official web site of Docker directly.