inline Docker Cheat Sheet

Originally published on August 25th, 2020 (Last updated on August 25th, 2020)

Dock­er commands #

  • docker build -t <image-name> . builds an image from the Dockerfile locat­ed in the cur­rent direc­to­ry and names it after image-name
  • docker run to cre­ate a con­tain­er form an image and run it. See incom­plete!, see para­me­ters below!
    • -p <port-internal>:<port-external> map an EXPOSEd port to a local port, so you can com­mu­ni­cate with the ser­vice in the container
    • -v <dir or volume>:<dir-in-container>:[rw|ro] mount a local direc­to­ry or vol­ume into the con­tain­er at the spec­i­fied path with either read/​write or read-only
    • -it open an inter­ac­tive ter­mi­nal-ses­sion on the container.
    • <image-name> to run the default com­mand of the image or
    • <executable> <parameters-for-exec> spawns a new con­tain­er to run the exe­cutable in it, with the giv­en parameters.
      • Exam­ple: docker run test/image ls lists the files of the default work-dir in the image.
  • docker ps list all run­ning docker-containers
    • -a show all con­tain­ers (includes stopped ones!)
  • docker logs <container> to see the out­put of the giv­en container.
    • -f to fol­low the output
  • docker stop <container-name or id> stops the spec­i­fied container
  • docker images lists all local images
  • docker exec -it <container-name or id> <program> runs he spec­i­fied program on the cur­rent­ly run­ning container.
    • bash as the program-para­me­ter starts an inter­ac­tive shell on the container
  • docker kill <container-name or id> kills the spec­i­fied con­tain­er (stop is preferred!)
  • docker rmi <image-name or id> delete a dock­er image
  • docker rm <container-name or id> delete a dock­er container
  • docker volume create --name="<name>" cre­ate a new named data-volume
  • docker volume ls list all data-volumes
  • docker volume rm <name or id> remove a data-volume

Dock­er­file Reference

Helpers #

Small one-line helpers to do com­mon Dock­er tasks:

  • docker rm $(docker ps -a -q) Deletes all stopped Containers
  • docker rmi $(docker images -f "dangling=true" -q) Deletes all dan­gling Images (named <none> in Image-list).

Dock­er machine #

Runs a vir­tu­al machine on Windows/​Mac OSX which runs dock­er (not on Lin­ux, dock­er runs nativ­ly there). Also enables com­mu­ni­ca­tion with a host­ed remote-machine (such as a serv­er or AWS instance).

  • docker-machine ls list all machines
  • docker-machine create --driver [hyperv|virtualbox] <name> cre­ates a new machine with the spec­i­fied dri­ver. The name can be default to make it the default machine
  • docker-machine start <name> start the named machine
  • docker-machine stop <name> stop the named machine
  • docker-machine env <name> dis­play the envi­ron­ment-con­fig­u­ra­tion for the named machine.
    • Exe­cute these com­mands (or use the pro­vid­ed instruci­tons) to con­fig­ure your lokal dock­er-com­mands to send orders to the giv­en docker-machine.

Comments

No com­ment sec­tion here 😄

You can reach me over at @knuth_dev or send me an Email.