Docker no doubt is the Best Container Management Platform
for your Enterprise and/or Personal project need.
Why we need Docker?
Consider this simple scenario for your project.
- You have a complex Java Enterprise application with more than 50 maven dependencies.
- You have few more local services dependencies.
- You have your own Database instance running locally.
- You have more than 10 custom shell script which is required to setup your environment.
- You have ~5 environment variables.
- You have ~5 auto startup scripts.
- And so on…
Well, when you want to ship all of above to your customer, that’s lots of steps. Isn’t it?
That’s where Docker comes into picture 🙂 .
Simplicity:
How about building and assembling all of above in single docker image
and ship that image to your customer? It’s just a single step install
for your client and get ready within few minutes
.
In this tutorial we will go over steps on how to install Docker on Linux
and all required steps.
Let’s get started:
Step-1. Login to Linux host and check prerequisite
Docker comes in 3 flavors:
- Docker Engine – Community
- Docker Engine – Enterprise
- Docker Enterprise
In this tutorial we will go over steps on installing Docker Community Edition
.
1.1) Login to your Linux host.
I’m using Digital Ocean droplet as my linux host.
bash-3.2$ ssh root@45.56.94.4 root@45.56.94.4's password: Welcome to Ubuntu 19.04 (GNU/Linux 5.0.0-13-generic x86_64)
1.2) Check Linux OS Architecture:
You need 64-bit
architecture.
root@localhost:~# arch x86_64
1.3) Check Linux OS Kernel Level:
Kernel Level should be greater than 3.0.
root@localhost:~# uname -r 5.0.0-13-generic
Step-2. Install Docker Community Edition
2.1) Make sure to add Docker’s official PGP key to your system.
root@localhost:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - OK root@localhost:~#
2.2) Make sure you got right PGP key:
root@localhost:~# sudo apt-key fingerprint 0EBFCD88 | grep 5822 Warning: apt-key output should not be parsed (stdout is not a terminal) 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
2.3) Make sure to install your Ubuntu OS to latest version
Command:
sudo apt-get update
root@localhost:~# sudo apt-get update Hit:1 http://mirrors.linode.com/ubuntu disco InRelease Get:2 http://mirrors.linode.com/ubuntu disco-updates InRelease [97.5 kB] Get:3 http://mirrors.linode.com/ubuntu disco-backports InRelease [88.8 kB] Hit:4 https://download.docker.com/linux/ubuntu disco InRelease Get:5 http://mirrors.linode.com/ubuntu disco-updates/main amd64 Packages [226 kB] Get:6 http://security.ubuntu.com/ubuntu disco-security InRelease [97.5 kB] Get:7 http://mirrors.linode.com/ubuntu disco-updates/main i386 Packages [194 kB] Get:8 http://mirrors.linode.com/ubuntu disco-updates/universe i386 Packages [252 kB] Get:9 http://mirrors.linode.com/ubuntu disco-updates/universe amd64 Packages [254 kB] Get:10 http://mirrors.linode.com/ubuntu disco-updates/universe Translation-en [80.1 kB] Get:11 http://security.ubuntu.com/ubuntu disco-security/main i386 Packages [144 kB] Get:12 http://security.ubuntu.com/ubuntu disco-security/main amd64 Packages [173 kB] Get:13 http://security.ubuntu.com/ubuntu disco-security/universe amd64 Packages [220 kB] Get:14 http://security.ubuntu.com/ubuntu disco-security/universe i386 Packages [217 kB] Get:15 http://security.ubuntu.com/ubuntu disco-security/universe Translation-en [55.4 kB] Fetched 2,098 kB in 2s (1,188 kB/s) Reading package lists... Done
2.4) Setup latest stable Docker Repository
Command:
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”
root@localhost:~# sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" Hit:1 http://mirrors.linode.com/ubuntu disco InRelease Hit:2 http://mirrors.linode.com/ubuntu disco-updates InRelease Hit:3 http://mirrors.linode.com/ubuntu disco-backports InRelease Hit:4 https://download.docker.com/linux/ubuntu disco InRelease Hit:5 http://security.ubuntu.com/ubuntu disco-security InRelease Reading package lists... Done
2.5) Install Docker
Command:
sudo apt-get install docker-ce
root@localhost:~# sudo apt-get install docker-ce Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: bridge-utils dns-root-data dnsmasq-base ubuntu-fan Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: aufs-tools containerd.io docker-ce-cli libltdl7 The following packages will be REMOVED: containerd docker.io runc The following NEW packages will be installed: aufs-tools containerd.io docker-ce docker-ce-cli libltdl7 0 upgraded, 5 newly installed, 3 to remove and 101 not upgraded. Need to get 87.9 MB of archives. After this operation, 133 MB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://mirrors.linode.com/ubuntu disco/universe amd64 aufs-tools amd64 1:4.9+20170918-2 [104 kB] Get:2 http://mirrors.linode.com/ubuntu disco/main amd64 libltdl7 amd64 2.4.6-10 [38.3 kB] Get:3 https://download.docker.com/linux/ubuntu disco/stable amd64 containerd.io amd64 1.2.6-3 [22.6 MB] Get:4 https://download.docker.com/linux/ubuntu disco/stable amd64 docker-ce-cli amd64 5:19.03.1~3-0~ubuntu-disco [42.5 MB] Get:5 https://download.docker.com/linux/ubuntu disco/stable amd64 docker-ce amd64 5:19.03.1~3-0~ubuntu-disco [22.7 MB] Fetched 87.9 MB in 3s (34.9 MB/s) (Reading database ... 83080 files and directories currently installed.) Removing docker.io (18.09.5-0ubuntu1) ... '/usr/share/docker.io/contrib/nuke-graph-directory.sh' -> '/var/lib/docker/nuke-graph-directory.sh' Removing containerd (1.2.6-0ubuntu1) ... Removing runc (1.0.0~rc7+git20190403.029124da-0ubuntu1) ... Selecting previously unselected package aufs-tools. (Reading database ... 82836 files and directories currently installed.) Preparing to unpack .../aufs-tools_1%3a4.9+20170918-2_amd64.deb ... Unpacking aufs-tools (1:4.9+20170918-2) ... Selecting previously unselected package containerd.io. Preparing to unpack .../containerd.io_1.2.6-3_amd64.deb ... Unpacking containerd.io (1.2.6-3) ... Selecting previously unselected package docker-ce-cli. Preparing to unpack .../docker-ce-cli_5%3a19.03.1~3-0~ubuntu-disco_amd64.deb ... Unpacking docker-ce-cli (5:19.03.1~3-0~ubuntu-disco) ... Selecting previously unselected package docker-ce. Preparing to unpack .../docker-ce_5%3a19.03.1~3-0~ubuntu-disco_amd64.deb ... Unpacking docker-ce (5:19.03.1~3-0~ubuntu-disco) ... Selecting previously unselected package libltdl7:amd64. Preparing to unpack .../libltdl7_2.4.6-10_amd64.deb ... Unpacking libltdl7:amd64 (2.4.6-10) ... Setting up aufs-tools (1:4.9+20170918-2) ... Setting up containerd.io (1.2.6-3) ... Setting up libltdl7:amd64 (2.4.6-10) ... Setting up docker-ce-cli (5:19.03.1~3-0~ubuntu-disco) ... Setting up docker-ce (5:19.03.1~3-0~ubuntu-disco) ... Installing new version of config file /etc/init.d/docker ... Installing new version of config file /etc/init/docker.conf ... Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service. Processing triggers for systemd (240-6ubuntu5) ... Processing triggers for man-db (2.8.5-2) ... Processing triggers for libc-bin (2.29-0ubuntu2) ...
2.6) Check Docker version
root@localhost:~# docker -v Docker version 19.03.1, build 74b1e89
Step-3. Start Docker and run Hello World
3.1) Simple command to run Docker on Linux
As you see above during installation Docker registers itself as a system service: /lib/systemd/system/docker.service
.
root@localhost:~# sudo systemctl start docker
3.2) Let’s pull Hello World Docker and run
Command:
sudo docker run hello-world
root@localhost:~# sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ root@localhost:~#
Above command will pull docker image and you will be able to run Docker Hello World application.
Step-4. How to check Docker Images/repositories?
Before running Hello World App:
root@localhost:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE keyansoftwares/logviewersvc latest 04612d6a457d 2 months ago 144MB keyansoftwares/logreadagent latest 0385a757deb5 2 months ago 138MB keyansoftwares/logreadagent <none> d75a02abb278 2 months ago 369MB grafana/grafana latest f96bf1723e2a 3 months ago 245MB
After running Hello World App:
As you see here – you will see new repository hello-world below 🙂
root@localhost:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE keyansoftwares/logviewersvc latest 04612d6a457d 2 months ago 144MB keyansoftwares/logreadagent latest 0385a757deb5 2 months ago 138MB keyansoftwares/logreadagent <none> d75a02abb278 2 months ago 369MB grafana/grafana latest f96bf1723e2a 3 months ago 245MB hello-world latest fce289e99eb9 7 months ago 1.84kB
Congratulations. You are all set. You have setup your Linux environment for Docker, Install Docker and ran your 1st Docker Application too.
Extra Step:
We recommend you to follow these post-install steps.
How to auto-start docker after VM reboot?
Command:
sudo systemctl enable docker
root@localhost:~# 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
Just execute above command and your docker will start automatically next time you reboot VM/host.
Let me know if you face any issue running above commands and any Docker issue.
Want to install Docker on CentOS?
Just replace Step-2
above with below steps:
root@localhost:~# sudo yum install -y yum-utils device-mapper-persistent-data lvm2 root@localhost:~# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo root@localhost:~# sudo yum install docker-ce docker-ce-cli containerd.io root@localhost:~# sudo systemctl start docker root@localhost:~# sudo docker run hello-world