My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Get Started with Docker

Get Started with Docker

SARTHAK JAIN's photo
SARTHAK JAIN
·Nov 23, 2020·

7 min read

What is a docker?

Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels.

How to install docker on your machine?

Docker website has excellent step by step assistance to install docker on your machine. You can install docker on mac, ubuntu,, windows , Debian , centos , fedora .

What is a docker image?

A docker image is an environment in the cloud, which you can download on your machine from the website of docker. Create your account and login.

In the above image. If you see on the top left, I searched for ubuntu and I get a result for the search which says ubuntu Dockers official Images. In the bottom right corner of the above image, you can find the command -docker pull ubuntu. This is very similar to Git. You can use this code in your terminal and pull the ubuntu image anywhere on your machine.

What will this ubuntu image have?. Is it an image with the logo of Ubuntu on it?.

Do not be confused by the term Image. When you run the command docker pull ubuntu, you get a copy of ubuntu which is a Debian-based Linux operating system on your machine.

Using “docker pull ubuntu” you get an image with the name ubuntu, which has the basic ubuntu installed in it. If you see the second line in the above image you can see that the tag given to this image is the latest, you can specify any other tag of your choice just to differentiate among multiple ubuntu images. Now you locally have an ubuntu machine.

$docker pull [image name]

1.PNG

When you run the command docker images you can find all the docker images present on your system. Currently, you have only one image called ubuntu with the tag latest and an image ID and the time when this image was created on the docker website, and also the size of this image. You can not view this as an image on any of the image viewer on your machine

$docker images

2.PNG Now, you have a docker image but what do you do after that?

Now you run the docker image. I know it sounds a bit wired but this is the truth. I want you to say these lines with me “I pulled a docker image called ubuntu from the docker website and now I am going to run my docker image, which is quite similar to activating your virtual environment.”

To run a docker image we use the command docker run. Here I have used docker run -t -d — name latest ubuntu. This will generate an ID, the one that you see in the second line.

$docker run -t -d --name [image tag] [image name]

3.PNG

What is a container?

When you run a docker image using the docker run command the process is called containerization and you have a copy of your docker image which is activated.

$docker ps -a

4.PNG

In the above image, you see a 12 digit container ID which is similar to the output of the image above this. It is your container ID. The name of the docker image used to run the container is ubuntu. The name of the container is the latest.

$docker exec -it latest bash

5.PNG

Just type the above command. docker exec -it latest bash and you have a machine with a name root@573ef0f0f413. All of the Linux commands work here.

6.PNG

In the above image, I used the command ls to list what was there in my container root@573ef0f0f413. You can see that we have everything just like what we have in any fresh ubuntu system.

The above image shows how my container works just like yet another system in my local machine.

How do you stop and delete a container?

After checking the running containers, you can choose to stop the one that you want. The command for stopping a container is docker stop [CONTAINER ID]. When you run this command container ID of the stopped container gets printed on the cmd window.

$docker stop [container ID]

7.PNG

8.PNG In the above image under the header STATUS, you see exited (0) a minutes ago. Because we stopped the container. Stop the container does not delete the container. To remove the container we must delete it

The command to delete a container is docker rm [CONTAINER ID]. Now your container is deleted.

docker rm [CONTAINER ID]

9.PNG

when you run the command docker ps -a again. You do not see any container.

How to delete a docker image?

There is no process called stopping the image before deleting it, unlike a container. To delete an image the command is docker rmi [Image name]

Let us delete it.

$docker rmi [image name]

10.PNG

The ubuntu image is deleted.