Useful docker commands for beginners

How to use Docker in a practical way

Pravin Lolage
Pravin Lolage
4 min readSep 17, 2019

--

Useful docker commands

In today’s world, most of the SaaS applications are using Docker. The main purpose of using Docker is to minimize the deployment time and reduce the efforts in testing the applications.

Prerequisites

  • A NodeJs application (for the demo I am using express-generatorexpress myapp).
  • Docker installed on your system (Digital Ocean has provided a detailed explanation of installing docker).
  • IDE of your choice (I am using VSCode).

Assuming your application is running on a specific port, so in my case, the application generated from express-generator is running localhost:3000 address with port 3000

Let’s dive!

Step 1 — Creating a Dockerfile

So docker file is basically a file that contains different steps that will be executed and an image will be created. Let’s create a file Dockerfile in the root directory of your application.

FROM node:carbonMAINTAINER Pravin LolageWORKDIR /usr/src/appCOPY . /usr/src/appRUN npm installEXPOSE 3000CMD [ "npm", "start" ]

So you can see above, each line represents a different step. In the first step, FROM node:carbon represents from which base image this application will be inherited and run. So this base image contains all the necessary packages to run your application. For node js there are many base image providers, you can choose as per your requirements.

The rest of the steps are the same as they indicate by reading it. The last step CMD [ “npm”, “start” ] basically, a command for running your application inside the container.

Step 2 — Creating an Image

So now we are good to go and create an image of the application so that it can be easily deployed on the server. For creating an image you should be in the root directory of your application on the terminal and then we will use the following command.

$ docker build -t mydemo-nodeapp-image .

This will create a docker image. -t is used to tag the image with some useful name. Here .(dot) represents a current directory of your application where Dockerfile is created. Now let’s verify that the image is created using the following command.

$ docker images

This should output the following.

REPOSITORY             TAG     IMAGE ID      CREATED         SIZE
mydemo-nodeapp-image latest d0cd969b52fc 1 minute ago 683MB

Step 3 — Creating a container

So it’s time to run the image that we created in the above steps.

$ docker run -d -p 3010:3000 --name mydemoapp mydemo-nodeapp-image

So this will create a container in which this demo node application will be running on the port 3000. The port 3010 is exposed to the outside world, so if you run the URL localhost:3010 you can see the application is running. You can keep the same port as 3000:3000. Here -d is used for a detached mode which means your terminal will be free to use after running the above command. You can also use -it means interactive mode where container logs will start in the same terminal tab.

Notice here I have given the image name that we created in the above steps which ismydemo-nodeapp-image. Now let’s verify that the container is running using the following command.

$ docker ps

This should output the following. Do not check the alignment ;)

CONTAINER ID  IMAGE                 COMMAND      CREATED         STATUS     PORTS                   NAMES
1ccf03bf43aa mydemo-nodeapp-image "npm start" 38 minutes ago Up 1 minute 0.0.0.0:3010->3000/tcp mydemoapp

It means, your container is up and running. If you can’t see anything that means it has been failed. For that, you can use the following command to check the containers that are created but not running.

$ docker ps -a

Step 4 — Inside docker container

Suppose you want to go inside your running container to access basic shell scripts. For that, you can use the following command

$ docker exec -it mydemoapp sh

This will take you inside the container. mydemoapp is the name of your container, you can check the same in the above steps.

There are some more commands for playing with a container like starting, stopping, and removing the container as follows.

$ docker start mydemoapp
$ docker stop mydemoapp
$ docker rm mydemoapp

The first command starts your container if it is not running. You can stop your container using the stop command. I have used the container name in all the above commands as mydemoapp You can also use the container id as 1ccf03bf43aa . The command rm will remove your container permanently and for that, it should not be running. You can also check the logs using the following command.

$ docker logs mydemoapp -f

Here -f will show the output in realtime.

Step 5 — Removing an Image

You can also remove the image by using the following command. But before deleting an image ensure that there are not any containers running. Otherwise, it will throw an error.

$ docker rmi mydemo-nodeapp-image

That’s It!

Thanks for reading!

If you like the above article please clap the same and if you don’t like please put your thoughts in comments so that I can improve it.

You can reach me out on Linkedin, Quora.

--

--

Pravin Lolage
Pravin Lolage

A software enthusiast with almost 8+ years of experience in programming trying to share my knowledge.