Docker commands

calendar_today timer Reading time ~1 minute

If you deal with Docker on a daily basis, you need to know a few commands. If you’re like me and forget these things, here’s a cheat sheet to help ;)

Summary

  1. Listing
  2. Removing
  3. Killing
  4. Some useful options to know

Listing

Images on your machine

Lists all the images on your machine, including the dangling ones

class="highlight">
1
$ docker images -a

Running containers

class="highlight">
1
$ docker ps

All containers

And their info

class="highlight">
1
$ docker ps -a

Only the IDs of all containers

class="highlight">
1
$ docker ps -qa

Removing

A container that was stopped

class="highlight">
1
$ docker rm ID

All containers that were stopped

class="highlight">
1
$ docker rm $(docker ps -qa)

An image from the system

If the image isn’t in use, you can remove it using the command below along with the image ID

class="highlight">
1
$ docker rmi ID

All images from the system

Removes ALL Docker images that aren’t in use from your machine. Use with care!

It’ll show an error if any image is in use.

class="highlight">
1
$ docker rmi $(docker images -qa)

All images from the system

Removes ALL Docker images that aren’t in use from your machine. Use with care!

It’ll ignore images in use.

class="highlight">
1
$ docker system prune --all

removal result


Killing

One or more containers

Just pass a list of IDs to kill more than one container

class="highlight">
1
$ docker kill ID

Some useful options to know

-a

Most, if not all, Docker commands and subcommands have an -a option which comes from all.

–rm

Same thing happens with the --rm option. When used, it indicates that the container should be removed after its execution.

-q

Most, if not all, Docker commands and subcommands have a -q option that lists only the IDs.

animated jess' signature

Related Articles