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
10 popular commands to get started with Docker

10 popular commands to get started with Docker

Hridyanshu's photo
Hridyanshu
·Nov 20, 2020·

6 min read

Docker is a widely-used open-source platform providing containerization services to develop, run, test and deploy software applications. To get a fair understanding of this statement, we will have to go through a couple of docker concepts.

What is a Docker Image?

  • A docker image is a digital snapshot of an application. It is a read-only template of the application using which the container will be created. It has the code, necessary dependencies and files of the application.

What is a Container?

  • A container is simply a running image that allows you to utilize the application.

I hope that now you have a rough idea of the entire picture. Docker allows you to build docker images for your application. Since these images are just the configuration for your application and not the application in itself, thus they can easily be deployed and pulled. Finally, container can be started using these images that will hold the application that the image specifies.

Now let us begin with the topic of this article, 10 popular commands to get started with Docker.

1. docker search: As stated above, in docker the applications are present in the form of images. These images are stored in docker hub for public use. To search the available images for a particular software the search command is used.
Command Structure: docker search "name"

3.PNG

2. docker images: This command lets you see which images are present locally.

4.PNG

3. docker pull: This command pulls the specified image to your local machine from docker hub.
Command Structure: docker pull "image name"

5.PNG

We can verify that the specified image is pulled.

6.PNG

4. docker run: This command creates a container based on the specified image. Here we will be looking at some popular variants of this command.

  • docker run -d "image-name"
    This command runs the container in background and prints the container id. 8.PNG

    Container id is a unique way to identify a container locally.

  • docker run --it "image-name"
    This command is used to open standard input of the specified image (-i) and generate a pseudo tty (-t) that allows interaction with the terminal of the specified image. 8.PNG

  • docker run -p "local-port":"container-port" "image"
    The -p option lets you bind the container port to the specified local machine port. This option can be used in combination with other options to provide the desired results. One such example is shown below: 14.PNG The command in the above screenshot shows that a container named redisHostPort is run 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: A docker container can be in either of the 5 states namely created, restarting, running, paused, and exited. This command is used to get details of containers that are in running state.

15.PNG

6. docker ps -a: As specified above, a docker container has 5 states and it can be in either of them at a given point of time. This command is used to get a list of all containers, irrespective of the state they are in.

21.PNG

7. docker inspect "object": This command provides the detailed low-level description of the specified object. The object here can be image, container, plugin etc.

13.PNG

In the above screenshot, a container is being inspected by providing the unique initial characters of its id.

8. docker start "container-id": This command is used to start a container that is in stopped state.
I have multiple containers in stopped state.

29.PNG

Let's start the second container from the bottom and then verify that it is up.

30.PNG

31.PNG

9. docker stop "container-id": This command stops a running container.

32.PNG

10. docker attach "container-id": This command attaches the three standard buffers (input buffer, output buffer and error buffer) of local machine to the specified running container. This enables you to control the container.

In the screenshot below, the command is used to interact with ubuntu container.

34.PNG

As you can see, the container can be controlled by attaching it to the host terminal.

So that's all you need to know to get started with Docker.