Skip to content

Docker cheat sheet

Gabriel Araujo edited this page Jul 26, 2022 · 5 revisions

Docker cheat sheet

Remarks

  • Docker containers will exit if no process is running inside it

  • If you are running something that needs input you have make the container --interactive and attach the --tty

    e.g: docker run -it ruby:3.1.2-alpine vi

  • Containers are meant to be disposable and won't persist any changes made inside it (Reference: https://docs.docker.com/storage)

Running containers

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

e.g: docker run -it --rm ruby:3.1.2-alpine sh

You can use docker run --help to get the full OPTIONS list, but here are some important ones:

-i, --interactive          Keep STDIN open even if not attached
-t, --tty                  Allocate a pseudo-TTY
--rm                       Automatically remove the container when it exits
--mount                    Attach a filesystem mount to the container

-d, --detach               Run container in background and print container ID
-p, --publish list         Publish a container's port(s) to the host
--name string              Assign a name to the container
-w, --workdir string       Working directory inside the container

Starting a container with a volume

Reference: https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag

  • with --mount syntax: docker run -it --rm --mount type=bind,src=$(PWD),dst=/usr/src/app ruby:3.1.2-alpine sh
  • with -v syntax: docker run -it --rm -v $PWD:/usr/src/app ruby:3.1.2-alpine sh

Listing images and containers

docker image ls List images docker ps Shows only running containers docker ps -a Shows all containers

Clone this wiki locally