Docker Cheat Sheet

To run a Docker container to support a development workflow, we will do the following:

  • Mount our source code into the container
  • Install all dependencies, including the “dev” dependencies
  • Start nodemon to watch for filesystem changes

Basic usage

  • docker ps [-all] list (all) containers
  • docker stop <container-id> or docker stop <container-name> Stop a container
  • docker rm <container-id> Remove one or more containers that have stopped
  • docker rm -f <container-id> Stop and remove a container
  • docker rmi <repository> Remove one or more images
  • docker run -dp 3000:3000 <container-name> Start a detached container at port http://localhost:3000
  • docker attach <container-id/name> Attach local standard input, output, and error streams to a running container
  • Pushing and pulling a Docker Image using Docker Hub: see http://localhost/tutorial/sharing-our-app/

The Container’s Filesystem

  • When a container runs, it uses the various layers from an image for its filesystem. Each container also gets its own “scratch space” to create/update/remove files. Any changes won’t be seen in another container, even if they are using the same image.

  • docker run <container-id/name> <command> Run a docker container and execute a command

  • docker exec <container-id/name> cat /data.txt Execute a command (here cat /data.txt) on a running containter

By default, changes are lost when the container is removed and all changes are isolated to that container.

Docker volumns

Volumes provide the ability to connect specific filesystem paths of the container back to the host machine.

  • docker volume create <volume-name> Create a volume
  • docker volume inspect Display detailed information on one or more volumes
    • The Mountpoint is the actual location on the disk where the data is stored.
  • docker volume ls List volumes
  • docker volume rm Remove one or more volumes
  • docker volume prune Remove all unused local volumes

Comments