docker ps -a
β show even stopped containersdocker images -a
β show even unused images
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 xyz
if stopped)
πΎ 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 /app folder of the Docker image
- β if you're using a terminal on Windows, make sure to replace
$(pwd)
(see more info in a section below)
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. NOTE: These steps were executed on a macOS device. Mounting files within a docker container on a Linux machine may modify the file owner. You could try mitigating it with the suggested workarounds here or here.
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)
[CTRL] + [D]
orexit
β exit and stop the container[CTRL] + [P] + [Q]
β exit without stopping the container
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
- 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
bash
instead of/bin/sh
- if you're specifying a working directory, instead of
-w /app
use-w "//app"
- replace
You can find all the commands in the official Docker CLI Reference.