Start a personal dev blog on your domain for free with Hashnode and grow your readership.
Get Started

Getting started with Docker: Basic commands you need to know !!

Docker is a widely used open source platform which packages an application and all its dependencies together in the form of containers.
For better understanding the concept of Docker, let us first go through some basic concepts related to it.

What is a dockerfile?

  • A dockerfile is basically a text document which consists of all the commands a user could run to build a docker image.

What is a docker image?

  • A Docker image is a read-only template which contains all the necessary instructions to create a container.

What is a container?

  • A container is a runnable instance of a docker image needed to run the applications.

Docker_1.jpg

Now let us look at some basic docker commands that helps to achieve containerization.

1. docker search: Since docker images are stored in docker hub for global use, this command is used to search a particular image.
Syntax of this command is:docker search "image name"

search_redis.png

2. docker images: This command is used to view to all the images you currently have on your system.
Syntax of the command is: docker images

docker_image.png

3. docker pull: This command pulls the specified image from docker hub to your local machine.
Syntax of the command is: docker pull “image name” docker_pull_redis.png

We can check whether the image was pulled or not by using "docker images" command again.

docker_image_2.png

4. docker run: This command searches the specified image and creates a container based on that image.
Now, let us look at some variants of this command.

  • command with d option: This command runs the container in the background and a unique container ID is displayed as the output. Here “d” refers to the detached mode.
    Syntax of this command is: docker run –d “image name” docker_run-d_redis.png
  • command with “it” option: Here, the option “i” means to open the standard input of the image even if it is not attached and option “t” allocates a pseudo-TTY. Hence this command runs the interactive terminal for the specified image.
    Syntax of this command is: docker run -it “image name” docer_run-it_redis.png

  • command with p option: This option lets you publish a containers’ port to the local host.
    Syntax of this command is: docker run -p "local-port":"container-port" "image" redis_HostPort.png This command shows that the container named redisHostPort is in the background and the port 6379 of the container is bind to the port 6379 of the local machine. The container uses the image redis:latest.

5. docker ps: This command is used to list the running containers.
Syntax of this command is: docker ps

docker_ps_2.png

6. docker ps with a option: This command is used to list all the running as well as exited containers.
Syntax of this command is: docker ps -a docker_ps_a.png

7. docker inspect: This command returns the low-level description of the specified object. The object can be a container, image etc.
Syntax of this command is: docker inspect "object" docker_inspect_1.png

8. docker stop: This command is used to stop a running container.
Syntax of this command is: docker stop "container ID" docker_stop.png Here, I tried to stop a container with id 56358 and later verified it using docker ps –a command.

9. docker start: This command is used to start a container which is in stopped state.
Syntax of this command is: docker start "container ID" docker_start.png Here, I started the container 56358 and checked it using docker ps command.

10. docker attach: This command is used to attach input, output and error buffer of the local machine to the specified running container.
Syntax of this command is: docker attach "container ID" docker_attach.PNG

This brings us to the end of this article. So, these were the basic commands to get started with docker and containers.

Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown · Publish articles on custom domain · Gain readership on day zero · Automatic GitHub backup and more

No Comments Yet