Docker options are additional flags that can be added to Docker commands to modify their behavior. For example, the -p
option can be used with the docker run
command to specify the ports that should be exposed on the container. Another example is the --rm
option, which can be used with the docker run
command to automatically remove the container when it stops running.
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
docker run --rm -d -it --name <my-image-name> <image>
Caution
TODO
Download an image from a registry. More info here
If no tag is provided, Docker Engine uses the :latest
tag as a default. This example pulls the ubuntu:latest
image:
docker pull ubuntu:22.04
Caution
TODO
We usually start by finding the installed version of docker that we are working on
docker version
Lists all images with their image id, repo, tags and size. More info here
docker images
List all running docker containers
List all containers, including stopped
List all running docker containers
List all containers, including stopped
Build a Docker image from a Dockerfile that is located via a filepath of url. More info here
docker build from path context
docker build <target-docker-folder>
give the image a name and optional a tag
docker build -t <my-image-name> <target-docker-folder>
remove intermediate containers after a successful build
docker build -t <my-image-name> --rm <target-docker-folder>
name of the dockerfile to build (default is Dockerfile
).
docker build -f<my-dockerfile-name> <target-docker-folder>
This command is used to create and start a new container from a Docker image. It combines the docker create and docker start commands into a single step.
If you specify the image by name, Docker will search for the image with that name in your local Docker registry. If it is not found locally, Docker will attempt to download the image from a remote registry (such as Docker Hub) before running the container. It is also possible to specify the image id but it is more readable to specify the image name. More info here
docker run [OPTIONS] <image-name> [ARGS]
Specify the name of the container.
If you don't want Docker to automatically download the image from a remote registry if it's not found locally.
Used to automatically remove the container when it is stopped. This can be useful to avoid accumulating stopped containers on your system.
Pulish a container's port(s) to the host. The example exposes port 8080 of the image to port 80 on the host.
docker run -p 8080:80 <image-name>
When you run a Docker container with the docker run
command, the container runs in the foreground by default, which means that the terminal that you used to start the container is attached to the container's input and output streams. This is useful for debugging and troubleshooting, but it's not ideal for long-running containers, as it ties up the terminal and prevents you from running other commands.
With this option, the container runs in the background, and Docker returns the container ID. You can use this ID to interact with the container later, if necessary.
List all images
docker images --format '{{.Repository}}:{{.Tag}}' | grep 'benjamin'
This command is used to start a stopped container. When you start a container with docker start, it will use the same configuration options that were specified when the container was created with docker run.
This command is used to stop a running container. When you stop a container with docker stop, Docker sends a SIGTERM signal to the container process, allowing it to perform any necessary cleanup tasks before shutting down.
This command is used to remove a stopped container. When you remove a container with docker rm, all of the container's data and configuration options are deleted.
docker images --format '{{.Repository}}:{{.Tag}}' | grep 'benjamin' | xargs docker rmi -f
Caution
TODO
You can attach to the container's input and output streams.
docker attach <container-name>
Execute a command in a running container
docker exec <container-name> <my-command>
This flag enables interactive mode, which allows you to interact with the container's STDIN (standard input). When this flag is used, Docker will keep the STDIN of the container open, allowing you to send input to the container and receive output from it. (input)
This flag allocates a pseudo-TTY (terminal), which allows you to enter and view output from the container as if you were working in a terminal. Without this flag, the output from the container would be printed in the console without any formatting or color-coding. (output)
Caution
TODO
Caution
TODO