In this article, we will know how to install Docker on Ubuntu 18.04 and 16.04. All the steps used below are tested and working without any problem. So, Let’s start with the definition of docker.
What is Docker?
Docker is a container-based software platform, which enables developers to easily wraps a whole application with all dependencies in a container and ship to any remote location.
So, without doing the whole setup of an application the application runs smoothly.
Prerequisite
Before installing the Docker directly on the Ubuntu system, make sure there are no other versions. For this try to remove any default package by running the following commands:
sudo apt-get purge docker lxc-docker docker-engine docker.io
After removing the default package from the Ubuntu system, You need to install some packages that are required to install Docker on Ubuntu 18.04 or 16.04 LTS.
Run the below commands to install Docker required packages.
sudo apt-get install curl apt-transport-https ca-certificates software-properties-common
Setup Docker Repository
Now, our next step is to verify the package’s signature by importing the docker’s official GPG key.
Run the below command to import docker’s official GPG key.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add
Now, add the Docker repository on your Ubuntu system which contains Docker packages, also you need to enable this repository to install Docker.
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
We have successfully completed the setup process of the Docker repository. Let’s start to install Docker on Ubuntu system.
Also Read: Install and Set Up Bower On Windows 10
Install Docker on Ubuntu
Run the following command to install the Docker, but before that update the apt index.
sudo apt-get update
sudo apt-get install docker-ce
The above command successfully installs the Docker community edition on your Ubuntu system. You don’t need to start it, but it gets starts automatically.
To verify the Docker installation and service status, just run the command given below:
sudo systemctl status docker
After running the above command, you will see the result like this (see text showing in green color)
How to Use Docker on Ubuntu
After a successful installation of Docker, You can check some basic things like search Docker images, download Docker images, launch Docker containers, and manage them.
Search Docker Images on Ubuntu
At first, you need to search Docker container images from Docker hub using the below commands
sudo docker search ubuntu
The above command will search all images with Ubuntu.
Download Docker Images on Ubuntu
Like Search, you can download the Docker container also with the name Ubuntu.
sudo docker pull ubuntu
By running the above command, it will download the newer image for ubuntu.
Using default tag: latest latest: Pulling from library/ubuntu 54ee1f796a1e: Pull complete f7bfea53ad12: Pull complete 46d371e02073: Pull complete b66c17bbf772: Pull complete Digest: sha256:31dfb10d52ce76c5ca0aa19d10b3e6424b830729e32a89a7c6eee2cda2be67a5 Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest
List Docker Images
Now, run the following command to see all the downloaded images of Docker.
sudo docker images //output what we have downloaded above REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 4e2eef94cd6b 4 days ago 73.9MB
Launch Docker Container
Now, you can launch a new Docker container using the downloaded image. The following command will launch a new container and will give you access to that newly launched container with /bin/bash shell.
sudo docker run -i -t ubuntu /bin/bash
You can use the EXIT command to stop the current container. But you can use CTRL + P + Q command to leave the container run in the background and open system console for your other work.
Run the below command, to check and list all the container which are running in the background.
sudo docker ps
Start Docker Container
To start a docker container, run the below command.
docker start <CONTAINER_ID> //place your own docker container id here - <CONTAINER_ID>
Stop Docker Container
To stop a docker container, run the below command.
docker stop <CONTAINER_ID> //place your own docker container id here - <CONTAINER_ID>
Attach Docker Container
Use the below command to attach to currently running docker containers.
docker attach <CONTAINER_ID> //place your own docker container id here - <CONTAINER_ID>
Uninstall Docker
Use the following command to uninstall the Docker Engine, CLI, and Containers packages.
sudo apt-get purge docker-ce docker-ce-cli containerd.io
To delete all images, containers, volumes, or any customized configuration files on your system are not automatically removed. Run the following command to do so.
sudo rm -rf /var/lib/docker
If you have edited any configuration files, you must need to delete these files manually.
Conclusion
Now, you have a basic understanding of how to install Docker on Ubuntu 18.04 or 16.04 LTS. You can ask your doubts in the comment section.