← Back

The Most Frequently Used docker Commands

To launch a command (here, pwd) in a new container based on a given image httpd [1]:

$ docker run httpd pwd
/usr/local/apache2

To do the same, but in an interactive bash session, use -i and -t flags:

$ docker run -it httpd bash
root@255923c57efa:/usr/local/apache2#

or:

$ docker run -it hseeberger/scala-sbt /root/scala-2.12.2/bin/scala
Welcome to Scala 2.12.2 (OpenJDK 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

scala> 

To connect to an existing container to execute a single command, use docker exec [2]:

$ docker exec www pwd
/usr/local/apache2

Likewise, there is a possibility to launch an interactive session based on an existing container:

$ docker exec -it www bash
root@bb551b4e86d5:/usr/local/apache2#

Inspecting a running container

docker inspect dumps everything Docker knows about a container as JSON — first place to look when you need details about something that's already running. The one I reach for most: a container's local IP address, under NetworkSettings.Networks.<network-name>.IPAddress (bridge for the default network):

$ docker inspect www --format '{{.NetworkSettings.Networks.bridge.IPAddress}}'
172.17.0.2

Or just run it without --format and search the full JSON output for the IPAddress key by hand.

Inspecting image layers

dive is a separate tool (not part of Docker itself) for visually exploring the individual layers of an image — what each layer added, changed, or removed, and how much space it's wasting. Point it at an image and it drops you into a TUI split between the layer list and a file tree:

$ dive httpd

Resources:

  1. https://docs.docker.com/engine/reference/run/

  2. https://docs.docker.com/engine/reference/commandline/exec/

  3. http://lib.ru/unixhelp/docker.txt