Top 15 Useful Docker Commands
Tags: docker, aws, devops, containers
Published: 2021-12-20 19:14:04

---
title: Top 15 Useful Docker Commands
published: true
date: 2020-11-08 17:36:52 UTC
tags: docker,aws,devops,containers
canonical_url: https://jamesmiller.blog/top-15-useful-docker-commands/
cover_image: https://i0.wp.com/jamesmiller.blog/wp-content/uploads/2020/11/dockerCommandsPSD3.jpg
---

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. ...](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/07/dockerCommands.jpg?resize=640%2C310&ssl=1)
_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](https://labs.play-with-docker.com/) that allows you to test commands in your browser, follow the steps outlined in the two images below to start testing docker commands:

[![](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/07/Screen-Shot-2020-07-22-at-21.46.49-2.png?resize=640%2C404&ssl=1)](https://labs.play-with-docker.com)
_Visit https://labs.play-with-docker.com and click the ‘Start’ button_

![](https://i1.wp.com/jamesmiller.blog/wp-content/uploads/2020/07/Screen-Shot-2020-07-22-at-21.47.11.png?resize=640%2C404&ssl=1)
_Click the ‘Add New Instance’ button to start up a temporary VM within your browser to test docker commands_

Use the [online lab](https://labs.play-with-docker.com), 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](https://hub.docker.com/). Enter the following command in the terminal window, and then enter your docker username and password.

```
docker login
```

### 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
```

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
```

### 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
```

### Remove all Docker images

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

```
docker image rm $(docker images -a -q)
```

### 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
```

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
```

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
```

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
```

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
```

### Stop all active containers

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

```
docker stop $(docker ps -a -q)
```

### 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
```

### Remove all inactive containers

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

```
docker rm $(docker ps -a -q)
```

### 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
```

### Search repositories

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

```
docker search exampleSearchTerm
```

## In conclusion

Docker is [one of my favourite technologies](https://jamesmiller.blog/my-ideal-creative-technology-stack/), I hope you find my top 15 useful Docker commands helpful when deploying your applications ![🙂](https://s.w.org/images/core/emoji/13.1.0/72x72/1f642.png)