How to Install Docker CE on Ubuntu Server 16.04 / 18.04 LTS

Channel: Linux
Abstract: 16.04        "/bin/bash"         15 minutes ago      Exited (0) About a minute ago                       mycontainer1 Start and attach to the containe

Docker is the most revolutionized technology in virtualization world now a days. Docker is actually an open source project which provides container technology. A container is a light weight VM(virtual machine) or a process which allows us to install Linux based applications inside it. Container don’t have its own Kernel, RAM, CPU and Disk but it uses the under lying OS kernel, RAM, CPU cores and Disk.

Container provides process base isolation where virtual machines provides resource based isolation. The main benefit of containers is that we can provision a container in less than a second because launching a containers is actually starting a process in Linux.

In this article we will discuss how to install and setup community docker-ce (community edition) on Ubuntu 18.04 /16.04 LTS,  prerequisite of Docker-CE is listed below :

  • 64-bit OS
  • Kernel version 3.10 or higher
Step:1 Update the Package database using below command

Let’s first update the packages database using ‘apt update‘ command

[email protected]:~$ sudo apt update
Step:2 Add GPG Key for Docker Official Repository

Docker engine package is not available in the default Ubuntu 16.04 server’s repositories. Let’s add the GPG key for docker repository using below command.

[email protected]:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
[email protected]:~$

Now add the docker-ce repository using ‘apt-add-repository‘ command

[email protected]:~$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Refresh the package index again as we have added docker-ce repository

[email protected]:~$ sudo apt update
Step:3 Install docker engine package using apt command.

Run the beneath apt-get command to install latest version docker-ce

[email protected]:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io -y

Once the docker-engine is installed, start and enable docker service using following commands

[email protected]:~$ sudo systemctl start docker
[email protected]:~$ sudo systemctl enable docker

Now this server will work as Docker Engine or Container Engine. A Bridge is also created which will acts as L2 switch and will provide IP address to the containers from its own DHCP server.

Verify the Docker version and other key parameters of Docker using ‘docker info‘ command

[email protected]:~$ sudo docker info

Add your user name to docker group using ‘usermod‘ command, in my case user name is ‘linuxtechi

[email protected]:~$ sudo usermod -aG docker linuxtechi

Docker installation part is completed now,  let’s get familiar with some basic commands of Docker with examples.

Syntax of Docker command :

# docker {options} command {arguments…}

To list the options of docker command, type ‘docker‘ on the terminal

Whenever docker engine is installed, default Registry Server is updated in docker command. When we run the docker command to download and search images then it will go the registry server to fetch the mentioned docker image. Though we can change this registry address as per our setup.

Search Docker images using ‘docker search’ command

Let’s assume we want search latest centos docker image.

[email protected]:~$ sudo docker search centos

Download Docker images using ‘docker pull’ command

Let’s suppose we want to download Ubuntu 16.04 docker image.

[email protected]:~$ sudo docker pull ubuntu:16.04

Similarly we can download the other Linux OS images as per our requirements

Once the image is download then it is stored locally in docker host image repository. We can list the available images in our local repository using ‘docker images‘ command.

[email protected]:~$ sudo docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               104bec311bcd        9 days ago          129 MB
Provision or launch a container using docker run command

Let’s suppose we want provision a Ubuntu 16:04 container.

[email protected]:~$ sudo docker run -it --name=mycontainer1 ubuntu:16.04
[email protected]:/#

In above Command ‘i‘ stands for interactive and ‘t‘ stands for terminal and name of the container is ‘mycontainer1‘ and Container image is ‘ubuntu:16.04’

Note: In case if we don’t mentioned OS version then it will try to provision latest one.

To stop the container type ‘exit’ in container console. If you don’t want to stop the container but want go back to docker engine console, the type ‘ctrl+p+q‘ in container console.

Verify how many containers are currently running

Using ‘docker ps‘ command we can list  running containers and to list all containers either they are running or stopped use ‘docker ps -a

[email protected]:~$ sudo docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b5cdf552b56c        ubuntu:16.04        "/bin/bash"         9 minutes ago       Up 9 minutes                            mycontainer1
[email protected]:~$
Stopping a Container using docker stop command

Let’s stop my recently provisioned container 「mycontainer1」

[email protected]:~$ sudo docker stop mycontainer1
mycontainer1
[email protected]:~$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
b5cdf552b56c        ubuntu:16.04        "/bin/bash"         15 minutes ago      Exited (0) About a minute ago                       mycontainer1
Start and attach to the container ‘mycontainer1’

use ‘docker start {container_name}’ command to start a container and to get the console of container use the command docker attach {container_name}

[email protected]:~$ sudo docker start mycontainer1
mycontainer1
[email protected]:~$ sudo docker attach mycontainer1
[email protected]:/#
Starting a Container in detach mode.

Let’s suppose we want provision one more container with the name ‘mycontainer2’ from centos7 docker image in detach mode( i.e container will be launched in the background and will not get console), to get the console use docker attach command

[email protected]:~$ sudo docker run -dit --name=mycontainer2 centos:7
5efb063260c8d328cf685effa05a610dfbf55ef602c7b14296d27668d9ff004d
[email protected]:~$ sudo docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5efb063260c8        centos:7            "/bin/bash"         28 seconds ago      Up 28 seconds                           mycontainer2
b5cdf552b56c        ubuntu:16.04        "/bin/bash"         28 minutes ago      Up 9 minutes                            mycontainer1
Binding Container ports to Docker engine Host

By Default Containers can reach to outside world and each outgoing connection will appear as if request is coming form docker’s host IP address but from outside world no one reach to containers.

Using port translation method we allow outsiders to reach our containers.

Let’s suppose i want to host a web site inside a container and this container will be accessed by Web developers via ssh

[email protected]:~$ sudo docker run -it -p 8000:80 -p 2000:22 --name=myserver1 centos:7
[[email protected] /]#

In above command -p option is used for pating (port  address translation), From the outside world if any one try to ssh my docker’s host ip address on port 2000 , then its request will be forwarded to 22 port on container 「myserver1」 and similarly request on 8000 port will be forwarded to 80 port on the container (myserver1)

[email protected]:~$ sudo docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                        NAMES
ce0375f922cc        centos:7            "/bin/bash"         6 minutes ago       Up 6 minutes        0.0.0.0:2000->22/tcp, 0.0.0.0:8000->80/tcp   myserver1
5efb063260c8        centos:7            "/bin/bash"         38 minutes ago      Up 38 minutes                                                    mycontainer2
b5cdf552b56c        ubuntu:16.04        "/bin/bash"         About an hour ago   Up 47 minutes                                                    mycontainer1
Commit changes of a container to a Docker Image

In docker command we have commit option to save the changes of container to a docker image. Let’s assume in above container we have installed web server and want save these changes to a docker image so that in future we can launch web server container from docker image.

[email protected]:~$ sudo docker commit -m "Web Service added" -a "Sunday Dec 25 2016" ce0375f922cc myserver1:v1
sha256:cac1bdb1d48a381c8bac0573dcd888e9595564f5a428bc6d1d3e97b823f646da
[email protected]:~$

[email protected]:~$ sudo docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myserver1           v1                  cac1bdb1d48a        25 seconds ago      191.8 MB
<none>              <none>              d695e1b36b68        49 seconds ago      191.8 MB
centos              7                   67591570dd29        9 days ago          191.8 MB
ubuntu              16.04               104bec311bcd        9 days ago          129 MB
Terminate / delete containers using ‘docker rm’ command

Use ‘docker rm‘ command to delete containers either based on their names and ids, Before deleting a container make sure it is stopped.

[email protected]:~$ sudo docker rm mycontainer2
mycontainer2
[email protected]:~$

To delete a running container

[email protected]:~$ sudo docker rm -f mycontainer2
Removing Docker images from Host’s Local image repository

docker rmi‘ command is used to delete or remove docker images from host’s local image repository

[email protected]:~$ sudo docker rmi myserver1:v1
Untagged: myserver1:v1
Deleted: sha256:cac1bdb1d48a381c8bac0573dcd888e9595564f5a428bc6d1d3e97b823f646da
[email protected]:~$

Note: We can also upload our customize docker images to the docker hub using docker push command but first we need to create our account on docker hub and run below commands from the terminal

[email protected]:~$ sudo docker login -u docker-registry-{user_name}
[email protected]:~$ sudo docker push docker-registry-{user_name}/{docker_image_name}

That’s all for this article. Hope you got an idea how to work with containers.Please share your valuable feedback and comments.

Ref From: linuxtechi

Related articles