25 Docker Commands : A Complete Guide with Examples (2024)

See the source image

Want to brush up on the most popular docker commands? Follow our essential guide below, of the top docker commands you'll be using often. Docker, which bills itself as "a better way to build apps," is an open-source platform for developing apps and microservices, according to its website.

The catch is that Dockers provides OS-level virtualization, which allows you to deploy your app in a container automatically.

Dockers are better than virtual machines (VMs) because they eliminate the costs of maintaining and starting the latter.

Your app and its dependencies (i.e. the pre-requisite apps for its proper functioning) become portable during all phases of development and testing by deploying them in a container.

Furthermore, the isolated apps reduce time-to-market by eliminating conflicts, enabling team collaboration, and reducing time-to-market.


List of Docker Commands

  • docker run – Starts a new container and executes a command. (docker run --name test -it debian)
  • docker start – Starts one or more containers that have been stopped. (docker start my_container)
  • docker stop – Puts an end to one or more currently running containers. (docker stop my_container)
  • Docker pull - Pulls an image or a repository from a registry. (docker image pull debian)
  • Docker push - Pushes an image or a repository to a registry. (docker image push registry-host:5000/myadmin/rhel-httpd:latest)
  • docker export – Creates a tar archive of a container's filesystem. (docker export red_panda > latest.tar)
  • docker exec – Executes command in a container at runtime. (docker exec -d mycontainer touch /tmp/execWorks)
  • Docker search – Looks for images on the Docker Hub. (docker search --filter=stars=3 --no-trunc busybox)
  • docker attach - Attaches to a running container (docker attach topdemo)
  • docker commit – Creates a new image based on the changes made to a container. (docker commit c3f279d17e0a svendowideit/testimage:version3)
  • Docker version - shows the docker version information  (docker version)
  • Docker ps - list all docker containers (docker ps --no-trunc)
  • Docker restart - restart one or more containers (docker restart my_container)
  • Docker kill - kill one or more running containers (docker kill my_container)
  • Docker login - log in to a registry (docker login localhost:8080)
  • Docker logout - log out from a registry (docker logout localhost:8080)
  • Docker network - manage networks. There are several other network commands
  • Docker history - shows the history of an image (docker history docker)
  • Docker rmi - remove one or more images (docker rmi fd484f19954f)
  • Docker rm - remove one or more containers (docker rm /redis)
  • Docker ps -a - show all containers (docker ps --no-trunc)
  • Docker cp - copy files / folders between a container and the local filesystem (docker cp ./some_file CONTAINER:/work)
  • Docker logs  - get the logs of a container (docker logs -f --until=2s test)
  • Docker volume - there are various volume commands you can run

When Do You Need to Use a Docker?

  • To run your code locally on your laptop while replicating the environment on your server.
  • During various development phases (dev/test/QA), Docker CI/CD was used.
  • As a version control system and for distributing your app's OS with a team.

How Do You Setup a Docker Locally

  • Download the Docker Toolbox and a Docker edition.
  • Check to see if your BIOS supports Virtualization Technologies, AMD-V, or KVM.
  • Install the Oracle VirtualBox Extension Pack.
  • Run the Setup.

How Do You Use a Docker?

The most significant benefit of virtual machines is that they create snapshots that can be reverted to at any time.

Docker containers improve lightweight process virtualization by being OS agnostic and utilizing the Linux Kernel's capabilities.

They're made from Docker images, similar to snapshots. A Docker file is used to create Docker images, which can be customized or used as is 'libcontainer' is the default execution driver for docker containers.

Docker Hub can be used to look up docker images and see how they were created.

To make a Docker container, type the following command in the terminal to download the 'hello world' image –

$ docker run hello world

Use the following command to determine the number of images on your system –

$ docker images

Using the Docker Hub to find an image –

$ docker search <image>

Examples of Using a Docker

  • By downloading Docker, you can run WordPress on your laptop without having to install Apache, PHP, MySQL, or other software. In order to run Docker in a virtual machine, the Docker Toolbox creates a containerized version of Linux.
  • Install Oracle VirtualBox using Docker Tool Box.
  • Open VirtualBox and install the Extension Pack.
  • To verify that your installation was successful, type $ docker run hello-world in the terminal.
  • To install WordPress locally, search for a WordPress image on the Docker Hub.
  • Dockers can also be used to set up DokuWiki.
  • Testing SDN components with Dockers is possible.

Here are a few examples to help you get started with your Docker engine.

*This article's examples are all for installing Docker on Windows. You can always use a Linux virtual machine to run it.

Docker Commands FAQs

What is Docker and how does it function?

Docker is a containerization platform that is free and open source. It allows programmers to package applications into containers, which are standardized executable components that combine application source code with OS libraries. By having containers of work, it is typically easier to scale such applications.

Is it possible to use Docker for free?

Yes, Docker Desktop is still free for personal use and non-commercial open-source projects.

Is Docker a virtual machine?

Docker is a configuration management tool, not a virtual machine. Also, keep in mind that Docker for Mac and Docker for Windows both use the virtualization layer. 

About the Author
Daniel Luke
Daniel is a web designer and developer. He has been a developer for the last 10 years working with various WordPress themes that allows him to compare and contrast different themes, understand the strengths and weaknesses to develop factual, real-world reviews. He is also a mobile app developer and technology reviewer. Over several years, he has developed his own mobile apps, both on Android and iPhone. This hands-on specialisation in mobile and web development allows him to be an authoritative voices when it comes to technology reporting.

One more thing... Did you know that people who share useful stuff like this post look AWESOME too? ;-)
Please leave a useful comment with your thoughts, then share this on your Facebook group(s) who would find this useful and let's reap the benefits together. Thank you for sharing and being nice!

Disclosure: This page may contain links to external sites for products which we love and wholeheartedly recommend. If you buy products we suggest, we may earn a referral fee. Such fees do not influence our recommendations and we do not accept payments for positive reviews.

Author(s) Featured On:  Inc Magazine Logo   Sitepoint logo   CSS Tricks logo    webdesignerdepot logo   WPMU DEV logo   and many more ...