📜 list containers/images
Section titled “📜 list containers/images”docker ps -a← show even stopped containersdocker images -a← show even unused images
✅ build/run an image
Section titled “✅ build/run an image”docker build -t abc .← build a container in a current directorydocker build -f docker/base/Dockerfile -t abc .← build a Docker image from another pathdocker build -f docker/def/Dockerfile --build-arg BASE_IMAGE=abc -t def← pass a build argument
docker run -it abc bash← run Docker image in an interactive bash shelldocker run --name xyz -it abc bash← give it a name
docker exec -it xyz bash← get into a running container (first make sure todocker start xyzif stopped)
💾 mount volume to a container (docs)
Section titled “💾 mount volume to a container (docs)”docker run --name xyz -v $(pwd):/app -it abc bash← mount current directory to a container and run it- you’ll be able able to access your local folder in the
/appfolder of the Docker image - ⚠ if you’re using a terminal on Windows, make sure to replace
$(pwd)(see more info in the section below)
- you’ll be able able to access your local folder in the
🧑🏻💻 develop through a container
Section titled “🧑🏻💻 develop through a container”docker pull python:3.8← pull the latest Python image from DockerHubcd <project_directory>← move into the chosen project directorydocker run --name xyz -v $(pwd):/app -it python:3.8 bash← run a container named “xyz” while mounting the current directory
🌐 publish a container (e.g. to use REST/curl)
Section titled “🌐 publish a container (e.g. to use REST/curl)”docker run -it --rm -p 8080:80 abc← expose port 8080 (inside container) to port 80 (on the host), and automatically remove the container after exitingdocker run -d -p 8080:80 abc← run in a detached mode (detach from the container and return to the terminal prompt)
🚶♂ exit a container
Section titled “🚶♂ exit a container”[CTRL]+[D]orexit← exit and stop the container[CTRL]+[P]+[Q]← exit without stopping the container
❌ delete containers/images
Section titled “❌ delete containers/images”docker system prune -a --volumes← remove all (stopped containers, unused images, volumes)docker stop $(docker ps -a -q)← stop all running containersdocker stop <container_id>← stop a running container
docker rm $(docker ps -a -q)← remove all stopped containersdocker rm -f <id/name>← remove a container forcefully
docker rmi $(docker images -q)← remove all imagesdocker rmi -f <image_id>← remove image by its ID forcefullydocker image rm -f <name>← remove image by its name
docker volume rm $(docker volume ls -q)← remove all volumes
⚠ running Docker commands on Windows
Section titled “⚠ running Docker commands on Windows”- cmd.exe
- replace
$(pwd)with%cd% - replace any newline characters
\with^. If you’re usingbash -c "…", then add^and make a newline before the first"
- replace
- PowerShell
- replace
$(pwd)with${pwd} - replace any newline characters
\with`(but not the ones withinbash -c "…")
- replace
- Git Bash
- replace
$(pwd)with/$(pwd) - you may need to precede the command with
winpty(at least not in the VS Code terminal) - it’s safer to use
bashinstead of/bin/sh - if you’re specifying a working directory, instead of
-w /appuse-w "//app"
- replace