If you want to deploy multiple applications in an isolated environment, consider using Docker and containers where applications have their own container.
In this tutorial, you’ll install the Docker ubuntu machine. You’ll then work with docker containers and docker images and push an image to a Docker Repository. So let’s get started.
Let’s dive in.
Table of Content
- What is Docker?
- How to Install Docker on Ubuntu 18.04 LTS
- How to run Docker command using Non-Root user (run docker commands without sudo)
- Working with docker command ( docker pull command, docker ps, docker image)
- Tag Docker image using
docker tagand push docker image to docker hub
What is Docker?
Docker is an open-source tool for developing, shipping, and running applications. It has the ability to run applications in a loosely isolated environment using containers. Docker is an application that helps manage containers in a very smooth and effective way. In containers, you can isolate your applications. Docker is quite similar to a virtual machine, but it is lightweight and easily ported.
Containers are light weighted as they are independent of hypervisors load and configuration. They directly connect with machines, i.e., the host’s kernel.
- Ubuntu machine preferably 18.04 version +, if you don’t have any machine you can create an ec2 instance on AWS account
- Recommended to have 4GB RAM and at least 5GB of drive space.
- An account on Docker Hub if you wish to create your own images and push them to Docker Hub.
You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.
How to Install Docker on Ubuntu 18.04 LTS
Let’s kick off this tutorial by installing docker on ubuntu. Installing docker on the ubuntu machine is a straightforward task. The Docker installation package is available in the official Ubuntu repository may not be the latest version.
This tutorial will install Docker from the official Docker repository to ensure you get the latest version. To do that, add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package.
- Login to your Ubuntu machine using your favorite SSH client.
- First, update your existing list of packages by running the below command
sudo apt update
- Install the below prerequisites software so that apt uses packages over the https protocol. The apt transport software allows your machine to connect with external repositories over HTTPS or HTTP over TLS.
sudo apt install apt-transport-https ca-certificates curl software-properties-common
- Next, add the GPG key for the official Docker repository to your system. This key builds the trust of your machine with the docker official repository.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Now, add the Docker repository to APT sources so that you can install the docker installation package.
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
- Next, update the package database with the Docker packages from the newly added repo using the following command.
sudo apt update
- Finally, Install the docker on the ubuntu machine using the apt install command.
sudo apt install docker-ce
- Docker should be started by now; verify the status and version of docker by using
service docker statusthe command.
How to run Docker command using Non-Root user (run docker commands without sudo)
The docker command can only be run by the root user or by a user in the docker group, automatically created during Docker’s installation process. If you attempt to run the docker command without prefixing it with sudo or without being in the docker group, it will give you a permission denied error message.
- To enable users other than root or to run docker commands without sudo, first create the docker group using the groupadd docker command.
- Next, add the users in the docker group that you created earlier and can run Docker commands.
sudo usermod -aG docker jenkins # To add Jenkins user to docker group sudo usermod -aG docker user1 # To add user1 to docker group sudo usermod -aG docker ubuntu # To add ubuntu user to docker group
- Restart the docker service by service docker restart command.
service docker restart
- Check the docker version by running the below command.
Your docker commands should now run succesfully now without giving any permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock like this, if you continue to get this message make sure to logout and login back on your machine.
Working with docker command ( docker pull command, docker ps, docker image)
The most important part of docker is to run several docker commands such as docker pull which fetches docker images that contain your code, the environment to run it, and several parameters. Docker images are stored on the Docker repository known as the docker hub, though you can also store them on your local machine.
- Use the docker pull command to pull the docker image on your ubuntu machine.
docker pull ubuntu
- Now check the downloaded image on your ubuntu machine and run the docker images command.
- Now, run the first docker container by using the below command.
docker run -it ubuntu # Here i,t provides you interactive shell access and ubuntu is image name
- To check all container details ( Exited, running, etc.), run the docker ps -a command.
docker ps -a
Tag Docker image using
docker tag and push docker image to docker hub
Now, let’s learn how to push the docker image to the docker hub. You need a docker hub account to push the docker image to the docker hub.
Assuming you are still logged into the Ubuntu machine using the SSH client.
- Login to docker hub with your credentials and then login.
docker login -u docker-registry-user
- Before you push your docker image to the docker hub, it’s highly recommended to tag your image with your docker hub username using the docker tag command. The syntax of the docker tag command is
docker tag image.
docker tag ubuntu:latest <dockerhub-username>/ubuntu:latest
- After successful login in docker hub, now push your docker image using the following command.
docker push <dockerhub-username>/ubuntu:latest
As you can see below, the docker image is successfully pushed into the docker hub that you created on the ubuntu machine.
In this tutorial, you learned how to install Docker, learned several docker commands such as docker images, docker tag, docker pull, and how to push docker image to docker hub.
Docker is a great way to host your applications separately and efficiently. So which applications are you planning to run in docker?