Docker - Beginner Commands

Docker - Beginner Commands

Part 01

Hello everyone, In this tutorial, we are going to learn a few basic commands that are beginner-friendly, I mean, these commands help beginners quickly understand Docker CLI, Without further delay, let's get started!

sudo systemctl status docker

This command tells you whether Docker is running or not on your system.

sudo docker pull hello-world

The pull command is used to get the existing image from the Docker Hub.

What is Docker Hub?

Let me explain to you clearly with an instance, just like how you use GitHub to store all your code files that are committed using Git SCM, Docker Hub is a platform to store all your images of Docker that are built using Docker. Instead of copy-pasting files into your instance, you push those files into a repo and clone them whenever you need them on the new platform, right? In the same way, you use Dockerhub to pull the images on any platform you need; instead of building the image again and again, you at once build the image and push it to the repo.

sudo docker run hello-world

The RUN command is used to run the images, indicating the image name or id triggers the Docker daemon to point to the particular image and run it.

sudo docker ps

The ps command is used to know what images are currently running on the system. Not just their names, but also get more information like ports, ID, etc. which we discuss later.

sudo docker stop hello-world

The stop command is used to stop a particular container.

sudo docker ps -a

The ps -a command is used to know the status of all containers that have been stopped as well. Usually, ps is used when you need to know about the current running containers, and ps -a is used to know about the containers that have been stopped before as well.

sudo docker images

This command is used to know what images we currently have on our system.

Running Nginx on Docker

sudo docker pull  nginx

Using this command, we pull the nginx base image into Docker, and we run this to proceed further into the practice of Docker commands.

In the Hello-World case, we've pulled the image, and then we tend to run the image, We've used two commands. But here, we are just using one single command.

What happens is that when we execute the run command, the docker daemon first checks the image on the local system, if it is present, and executes the command. It searches for the image in the global repository (Docker Hub), pulls it automatically, and then executes the command.

If the image is not even present on the Docker Hub, then it throws an error.

After successfully pulling the image, we are now used to running it.

sudo docker run -d -p 80:80 nginx

You might be confused by this command, so don't worry and try to read this carefully.

When you've run the hello-world image, we've seen so many things written in the output along with the hello-world text. So, to make it hide and just make an image run, we use -d (daemon mode).

But using this, it returns the image ID as confirmation of the image running successfully.

Next, -p is used to set the port number for the service that we are running on Docker. So, by default, nginx is set to run on an 80 port number.

Now, after running this command, copy the IP address of the instance or your device and paste it on any browser with the port number as below:

http://<IP-Address>:80

On-screen, you can see the welcome board for Nginx. We've successfully spun the image of nginx and accessed it.

These are just the basic commands of Docker. In upcoming blogs, we discuss the more important commands of Docker with a project.