Docker cheatsheet

I usually forget everything if I don’t use it that much. Here is a brief list of docker commands learned on the go.

Start a simple container

$ docker run -i -t ubuntu /bin/bash
  • run runs the ubuntu image
  • -i allows you to write to the container (“Keep STDIN open even if not attached”)
  • -t allocates a pseudoTTY and allows to read from the container
  • ubuntu is the name of the image to start
  • /bin/bash is the command to launch once the image is started

In practice: use -i -t to read and write from the docker image in the console.

Optional commands:

  • --name XXX assigns the name XXX to the container
  • -d launches the container as a daemon
  • -c XXX passes XXX to the command. In our example the command is /bin/bash. With -c we can pass any kind of commands; for example -c "while true; do echo hello world; sleep 1; done". Docker would pass this string t bash and the container would print hello world every second without stopping. Note the quotes.

See containers status

$ docker ps -a 
  • ps alone prints only the active containers
  • -a shows also the exited containers

Inspecting a container

If the container is started as daemon you might want to see the output.

$ docker logs -t -f NAME_OF_CONTAINER 
  • logs NAME_OF_CONTAINER shows you the logs of the container. Without options you see the latest logs and then the propt gets back to your shell.
  • -f works like -f in tail -f; logs become live. Use CTRL-C to exit.
  • -t shows also timestamps.

To check container processes:

$ docker top daemon_dave

To get stats about a bunch of docker containers:

$ docker stats daemon_dave daemon_kate

Running programs in a container

Daemon mode:

$ sudo docker exec -d daemon_dave touch /etc/new_config_file
  • -d stands for daemon mode.
  • exec is used to run a command in the daemon-dave container.

Interactive mode:

$ docker exec -t -i daemon_dave /bin/bash
  • -t creates a TTY
  • -i captures STDIN

…basically opens an interactive shell.

Stopping & Deleting

$ docker stop CONTAINER 
$ docker rm CONTAINER 

Listing docker images

$ docker images

Related Posts: