Skip to content

Instantly share code, notes, and snippets.

@fabiomolinar
Last active December 10, 2018 14:02
Show Gist options
  • Save fabiomolinar/a41b69f5f7a2d0d43d65bdb3512bd0af to your computer and use it in GitHub Desktop.
Save fabiomolinar/a41b69f5f7a2d0d43d65bdb3512bd0af to your computer and use it in GitHub Desktop.
List of useful Docker commands

List of useful Docker commands

Source:

  • Learning Docker (Book)
  • Authors: Jeeva S. Chelladhurai, Vinod Singh, Pethuru Raj

docker run

The docker run subcommand takes an image as an input and launches it as a container. You have to pass the -t and -i flags to the docker run subcommand in order to make the container interactive. The -i flag is the key driver, which makes the container interactive by grabbing the standard input (STDIN) of the container. The -t flag allocates a pseudo-TTY or a pseudo Terminal (Terminal emulator) and then assigns that to the container. In the following example, we are going to launch an interactive container using the ubuntu:16.04 image and /bin/bash as the command:

$ sudo docker run -i -t ubuntu:16.04 /bin/bash

To exit the interactive shell, just type exit.

docker ps

The docker ps subcommand will list all the running containers and their important properties.

This command won't show the stopped containers unless we pass a -a flag.

$ sudo docker ps

docker attach

A way to attach a terminal window to the docker container. A better way may be to use docker exec when containers are already running.

$ sudo docker attach <container name>

docker exec

docker exec -it [container-id] /bin/bash

docker diff

Command to detect changes on theh container file system.

$ sudo docker diff <container ID>

Each line of this command's output will be preceded by A, C or D. And they mean:

  • A: added
  • C: changed
  • D: deleted

docker {start, stop, restart}

$ sudo docker start <container ID>
$ sudo docker stop <container ID>
$ sudo docker restart <container ID>

The command restart is nothing but a stop followed by a start command.

docker {pause, unpause}

sudo docker pause <container ID>
sudo docker unpause <container ID>

docker rm

Remove a container if the container is stopped. If it is necessary to remove a running container, we need to pass the -f flag.

docker container prune

Removes all stopped containers.

docker commit

The docker commit subcommand can be performed on a running or a stopped container. When a commit is performed on a running container, the Docker Engine will pause the container during the commit operation in order to avoid any data inconsistency. We strongly recommend that you perform the commit operation on a stopped container.

$ sudo docker commit <container ID> <repository name>/<tag name>

Example: $ sudo docker commit 472c96295678 learningdocker/ubuntu_wget

docker logs

The docker logs subcommand is used for viewing the output generated by our daemon container.

docker inspect

Shows all detailed information about a container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment