Useful Commands
Here follows a list of useful Docker commands with useful flags for each command.
Table of Contents
Build an image from a Dockerfile.
docker build [DOCKERFILE PATH]Example
Build an image tagged my-org/my-image where the Dockerfile can be found at /tmp/Dockerfile.
docker build -t my-org:my-image -f /tmp/DockerfileUseful flags
--file -fPath where to find the Dockerfile--force-rmAlways remove intermediate containers--no-cacheDo not use cache when building the image--rmRemove intermediate containers after a successful build (this istrue) by default--tag -tName and optionally a tag in the ‘name:tag’ format
Execute a command inside a running container.
docker exec [CONTAINER ID]Example
docker exec [CONTAINER ID] touch /tmp/exec_worksUseful flags
--detach -dDetached mode: run command in the background-itThis will not make the container you started shut down immediately, as it will create a pseudo-TTY session (-t) and keep STDIN open (-i)
List all downloaded/created images.
docker imagesUseful flags
-qOnly show numeric IDs
Shows all the info of a container.
docker inspect [CONTAINER ID]Gets logs from container.
docker logs [CONTAINER ID]Useful flags
--detailsLog extra details--follow -fFollow log output. Do not stop when end of file is reached, but rather wait for additional data to be appended to the input.--timestamps -tShow timestamps
Shows information about all running containers.
docker psUseful flags
--all -aShow all containers (default shows just running)--filter -fFilter output based on conditions provided,docker ps -f="name="example"--quiet -qOnly display numeric IDs
Remove one or more images.
docker rmi [IMAGE ID]Useful flags
--force -fForce removal of the image
Creates and starts a container in one operation. Could be used to execute a single command as well as start a long-running container.
Example:
docker run -it ubuntu:latest /bin/bashThis will start a ubuntu container with the entrypoint /bin/bash. Note that if you do not have the ubuntu image downloaded it will download it before running it.
Useful flags
-itThis will not make the container you started shut down immediately, as it will create a pseudo-TTY session (-t) and keep STDIN open (-i)--rmAutomatically remove the container when it exit. Otherwise it will be stored and visible runningdocker ps -a.--detach -dRun container in background and print container ID--volume -vBind mount a volume. Useful for accessing folders on your local disk inside your docker container, like configuration files or storage that should be persisted (database, logs etc.).
Learn More
A list of more useful Docker commands can be found in the docker-cheat-sheet.
Last updated
Was this helpful?