DEV Community

Cover image for Top 15 Useful Docker Commands
James Miller
James Miller

Posted on • Originally published at jamesmiller.blog on

Top 15 Useful Docker Commands

This post is about my top 15 useful Docker commands, that I use when deploying NodeJS applications.

Docker is an app containerisation framework, that allows you to create a ‘container’ for your application within a basic virtual operating systems that is pre-configured to run your application.

Using Docker allows you to reliably deploy an app and all of its required dependencies, within any cloud platform web server environment. Docker is also useful when splitting your application into smaller contained microservices. This allows for greater scalability and ease of maintenence.

The most useful docker commands that to help you deploy applications. ...
Flow of using Docker: Write a ‘Dockerfile’ that is used to create an image of your application. Once you have create a Docker image, it can then be pushed to your Docker registry where it can then be pulled down onto your server (in this case EC2 instance). Once the Docker image is on the EC2 instance, a Docker container can then be created with your application, which can then be viewed by people on your website’s IP address/domain name.

Testing Docker commands online

Docker has an online lab that allows you to test commands in your browser, follow the steps outlined in the two images below to start testing docker commands:


Visit https://labs.play-with-docker.com and click the ‘Start’ button


Click the ‘Add New Instance’ button to start up a temporary VM within your browser to test docker commands

Use the online lab, or opening terminal on your own computer, to have a play with my top 15 useful Docker commands!

Login to Docker

For this step you will need to have an account with Docker Hub. Enter the following command in the terminal window, and then enter your docker username and password.

docker login
Enter fullscreen mode Exit fullscreen mode

Build a Docker image locally

Assuming you have a ‘Dockerfile’ configured for your app, you can then build a docker image from that ‘Dockerfile’.

docker build . -t myRepo/myImage:exampleTag
Enter fullscreen mode Exit fullscreen mode

Using the docker build command:

  • builds an application in the folder where the command was run (this is signified by the ‘.’, you can reference another folder by adding ‘/exampleFolder’).
  • tags the application to be built with ‘exampleTag’, under the image name of myImage belonging to the repository called ‘myRepo’.

List all available docker images

To see all Docker images that have been created on your local machine, use this command.

docker image ls
Enter fullscreen mode Exit fullscreen mode

Remove Docker image

To remove an image from your local machine, reference the name of the image in the below command instead of ‘exampleImage’.

docker image rm exampleImage
Enter fullscreen mode Exit fullscreen mode

Remove all Docker images

Use this command to remove all created Docker images from your machine.

docker image rm $(docker images -a -q)
Enter fullscreen mode Exit fullscreen mode

Push a Docker image to your repository

Once you’ve built an image, you can then store it in your Docker repository, so it can be downloaded and run on a server.

docker push myRepo/myImage:exampleTag
Enter fullscreen mode Exit fullscreen mode

This command pushes the image:

  • which is tagged as ‘exampleTag’.
  • with an image name called ‘myImage’.
  • within the repository called ‘myRepo’.

Build and run a container from your Docker image

Once you’ve created an image locally or stored an image in your repository, you are then able to create a container from that image, which runs the application as defined from your original DockerFile.

docker run -d -p 80:3000 --restart always myRepo/myImage:exampleTag
Enter fullscreen mode Exit fullscreen mode

The command above:

  • runs the Docker container detached (-d) from the terminal/command line window you used to run the command from. This prevents the container from taking over your terminal window, so you can run other applications/commands from your terminal.
  • links port 300 from the container, to port 80 of the server where you are running the application, e.g a sample NodeJS application that normally serves content from port 3000 of the docker container would serve content from port 80 of the server.
  • is set to restart the application whenever the application crashes/stops for what ever reason (–restart always).
  • builds a container from the image marked as ‘myRepo/myImage:exampleTag’.

List all active Docker containers

Once you’ve started running several different containers, you can use the below command to list all active containers that are running in your environment.

docker ps
Enter fullscreen mode Exit fullscreen mode

Executing this command will show you the containers:

  • ID
  • Image
  • Run command
  • Creation time/date
  • Status
  • Opened ports
  • Name

List all Docker containers

If you want to see all Docker containers (even ones that have been stopped) then the below command will show that to you.

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Executing this command will show you the containers:

  • ID
  • Image
  • Run command
  • Creation time/date
  • Status
  • Opened ports
  • Name

Stop an active container

To stop an active container you need to get the ID of the container you’d like to stop and then use the below command.

docker stop exampleDockerID
Enter fullscreen mode Exit fullscreen mode

Stop all active containers

To stop all actively running containers, use the below command.

docker stop $(docker ps -a -q)
Enter fullscreen mode Exit fullscreen mode

Remove an inactive container

Once a container is stopped, it can still be accessed and restarted. To completely remove it, you have to use the container’s ID within the below command.

docker rm exampleDockerID
Enter fullscreen mode Exit fullscreen mode

Remove all inactive containers

To remove all containers that have been stopped, use this command.

docker rm $(docker ps -a -q)
Enter fullscreen mode Exit fullscreen mode

Remove all stopped/unused Docker resources

If you want to remove all stopped containers, unused networks, dangling images and volumes, then you can just run the below command.

docker system prune
Enter fullscreen mode Exit fullscreen mode

Search repositories

To search ‘Name’, ‘Description’, ‘Stars’, ‘Official’ or ‘Automated’ criteria in your repository, use the below command – replacing the ‘exampleSearchTerm’

docker search exampleSearchTerm
Enter fullscreen mode Exit fullscreen mode

In conclusion

Docker is one of my favourite technologies, I hope you find my top 15 useful Docker commands helpful when deploying your applications 🙂

Discussion (0)