Containers

Best Docker Container Commands You Aren’t Using

Take a look at the best Docker container commands you aren't using to unlock the full potential of the Docker command line.

Docker has a lot of functionality from the command line and has built-in commands that allow doing a lot of different things with your containers. However, there are several Docker commands that you may not be using and you should be. Let’s take a look at the best Docker container commands you aren’t using and see what these commands are and how you can use them.

Docker command line (Docker CLI)

The Docker engine allows you to work from the Docker command line to manage and administer your containers effectively across your containerized environment. Using Docker commands, you have an effective way to troubleshoot, administer, configure, and perform other operations with Docker containers, Docker image configuration, and others.

Most know about docker run and docker network commands as these are used to run containers from the command line. However, note the following list of basic Docker commands you may not be using:

  1. docker rm and docker rmi
  2. docker exec
  3. docker commit
  4. docker login
  5. docker pull
  6. docker push
  7. docker ps
  8. docker logs
  9. docker prune
  10. docker stats

Check out the official Docker command line documentation found here: Use the Docker command line | Docker Docs.

1. docker rm and docker rmi

The docker rm command gets rid of containers that are not running. You can use it to permanently delete a container and its data. The docker rmi command is specific to Docker images and gets rid of re images from the local Docker registry.

docker rm my_container
docker rm $(docker ps -a -q)  # Remove all stopped containers
docker rmi my_image
docker rmi $(docker images -q)  # Remove all images

Below we are stopping a container first (required before you can remove) and then removing it using the docker rm command.

Docker rm command
Docker rm command

If you want to remove unused images (have to not be referenced by any containers) you can use the docker rmi command.

Docker rmi command
Docker rmi command

2. docker exec

The docker exec command lets you to connect to a running container. You can use it to run a new command inside the running container. This allows you to execute commands inside the container without needing to start a new container from the image.

docker exec -it my_container /bin/bash  # Open an interactive bash shell
docker exec my_container ls /app  # List files in the /app directory
docker exec -d my_container touch /tmp/newfile  # Run a command in detached mode

Below, we are listing out the contents of the var directory in our container we are exec’ing into.

Docker exec command
Docker exec command

3. docker commit

The docker commit command creates a new image from a currently running container. When you run the command, it captures the state of a container and saves it as a new image. If you started with a base container image but have made many configuration changes inside the container, this command will save those as a new image that you can docker run and have all those changes without starting from scratch.

docker commit my_container my_new_image
docker commit -m "Added new feature" my_container my_new_image
docker commit -a "Author Name" my_container my_new_image

Below, we are capturing a running nginx container as a new container image. When we run the docker image ls command, we can see the newly created container image.

Docker commit command
Docker commit command

4. docker login

The docker login command logs into a Docker registry or another self-hosted registry. When you log in your Docker client it can then work with Docker registries such as pulling and pushing images.

docker login  # Prompts for username and password
docker login -u my_username -p my_password docker.io
docker login registry.example.com  # Login to a custom registry

Below, I am logging into a self-hosted container registry I have running in the home lab.

Docker login command
Docker login command

5. docker pull

The docker pull command downloads an image from a Docker registry to your local machine. When you run the docker pull command, keep in mind it doesn’t spin up a new container. It only pulls a container image from which you can spin up a new container.

docker pull ubuntu  # Pull the latest Ubuntu image
docker pull nginx:1.19  # Pull a specific version of the Nginx image
docker pull myrepo/myimage:latest  # Pull an image from a custom repository

Here, we are pulling the latest nginx container image.

Docker pull command
Docker pull command

6. docker push

The docker push command uploads an image to a Docker registry. This publishes the image from your local machine to a remote repository.

docker push myrepo/myimage:latest
docker push registry.example.com/myrepo/myimage:latest
docker push my_username/myimage:1.0  # Push an image to Docker Hub

Below we are pushing an image from our local registry up to a self-hosted registry we are logged into.

Docker push command
Docker push command

7. docker ps

The docker ps command lists running containers. It shows information about all currently running containers on the system. If you want to see information about even stopped containers, you can use the “-a” parameter to list for all.

docker ps  # List all running containers
docker ps -a  # List all containers (running and stopped)
docker ps -q  # List only container IDs

Below, we see all running containers on the Docker container host.

Docker ps command
Docker ps command

8. docker logs

The docker logs command gets the logs of a specific container. It shows the stdout and stderr output of the specified container.

docker logs my_container  # Show logs of the container
docker logs -f my_container  # Follow (tail) the logs of the container
docker logs --since "10m" my_container  # Show logs from the last 10 minutes
Docker logs command
Docker logs command

9. docker prune

The docker prune command removes unused container data. It is a great command line command to help clean up unused containers, Docker volume objects, images, docker networks, and build cache from the command line interface. If you have disk space issues on your Docker container host, this is a great command to remember, especially the docker system prune -a -f command.

docker system prune  # Remove all unused containers, networks, images, and build cache
docker container prune  # Remove all stopped containers
docker image prune  # Remove all unused images
Docker prune command
Docker prune command

10. docker stats

The docker stats command shows you a live stream of container resource usage statistics from the Docker daemon. It shows real-time information for each running container on CPU, memory, and network usage.

docker stats  # Show stats for all running containers
docker stats my_container  # Show stats for a specific container
docker stats --no-stream  # Show one-time stats snapshot
Docker stats command
Docker stats command

11. docker restart

The docker restart command is a great command to know as it stops and then starts Docker containers. This command is useful for rebooting containers that need to be refreshed without having to remove and recreate them.

docker restart my_container  # Restart a specific container
docker restart $(docker ps -q)  # Restart all running containers
docker restart -t 30 my_container  # Restart a container with a 30-second timeout

Below, we are restarting a running nginx container.

Docker restart
Docker restart

12. docker network

The docker network command manages Docker networks. With it you can create, inspect, connect, and remove networks. You can use it in setting up and managing network connectivity between containers.

docker network create my_network  # Create a new network
docker network ls  # List all networks
docker network inspect my_network  # Inspect details of a specific network
docker network connect my_network my_container  # Connect a container to a network
docker network rm my_network  # Remove a network

Below, we are using the docker network ls command to list out all the available Docker networks configured on the Docker host.

Docker network command
Docker network command

Troubleshooting

If you have issues running Docker commands from the command line:

  • Check and make sure you are part of the Docker users group. You can create this group and add your user using the command: sudo usermod -aG docker $USER && newgrp docker

Note the following command-specific troubleshooting:

docker rm and docker rmi

Issue: Error message “Conflict” or “No such container/image.”

  • Solution: Make sure the container or image exists and is not running or in use. Use docker ps -a to list all containers and docker images to list all images. Stop any running containers with docker stop <container_id> before removing them.

docker exec

Issue: Error message “No such container.”

  • Solution: Make sure the container is running using docker ps. Start the container if it’s not running with docker start <container_id>.

docker commit

Issue: Error message “No such container.”

  • Solution: Make sure the container exists and is running using docker ps -a. Also make sure you have the correct container ID or name.

docker login

Issue: Authentication failure.

  • Solution: Make sure the correct username and password are used. Verify your credentials by logging into the Docker registry website or your private self-hosted registry

docker pull

Issue: Error message “Repository not found” or “Network issues.”

  • Solution: Make sure the image name and tag are correct. Check your network connection. Ensure you are logged in to the correct registry with docker login.

docker push

Issue: Error message “Denied” or “Repository does not exist.”

  • Solution: Make sure you are logged in to the correct registry. Double-check that you have permission to push to the repository. Check the repository name and tag.

docker ps

Issue: No containers listed.

  • Solution: Make sure you have running containers. Use docker ps -a to list all containers, including stopped ones.

docker logs

Issue: Error message “No such container.”

  • Solution: Verify the container name or ID is correct. Make sure the container is running or has run in the past.

docker prune

Issue: Data not being pruned as expected.

  • Solution: Make sure there are unused resources. Use specific prune commands like docker container prune, docker image prune, or docker volume prune to target specific resources.

docker stats

Issue: Error message “No such container.”

  • Solution: Make sure the container is running using docker ps. Also, check the container name or ID is correct.

docker restart

Issue: Error message “No such container.”

  • Solution: Check the container name or ID is correct. Use docker ps -a to list all containers and confirm the container exists.

docker network

Issue: Error message “Network not found” or “Network conflict.”

  • Solution: Make sure the network name or ID is correct. Use docker network ls to list all networks. Check for any conflicting networks and remove them if necessary.

Wrapping up

Hopefully this Docker commands cheat sheet will help you to make better use of the Docker command line to manage your containerized environments running on top of Docker. There are a lot of great underutilized Docker commands that can greatly help with managing Docker containers. I especially like the docker logs and exec commands, along with the docker prune commands that can help reclaim massive amounts of disk space from Docker operations and stale container resources.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.