Docker CMDs
20 Feb 2024 #docker
Image
Search for an Image
docker search [OPTIONS] IMAGE_NAME
--automated=false
Show only automated builds--no-trunc=false
Show all results-s=n
--stars=n
Show only images with at least n stars- e.g.,
docker search --stars=100 mysql
Download an Image
docker pull [OPTIONS] IMAGE_NAME[:TAG_NAME]
-a
Download all versions of the imageTAG_NAME
Specify the version to download, if not specified, the latest version is downloaded- e.g.
docker pull ubuntu:22.04
List Images
docker images [OPTIONS] [REPOSITORY]
-a
--all
Show all images--digests
Show digests-q
--quiet
Show only image IDs- *To list containers:
docker ps
Inspect Image Details
docker image inspect IMAGE_ID
- You can enter part of the ID instead of the full ID.
Delete an Image
docker rmi [OPTION] IMAGE_NAME:TAG_NAME
-f
Force delete the image of a running container, but in practice, it only untagged and does not actually delete the image or container.- Delete multiple images at once:
docker rmi IMAGE_NAME_1 IMAGE_NAME_2 ...
- Stop all running containers of a specific image and then delete the image:
docker rm -f $(docker ps -a --filter ancestor=IMAGE_NAME) docker rmi IMAGE_NAME
Save and Load Images
docker save -o DIRECTORY IMAGE_NAME
-o
(output) Specify the directory to save the image
docker load -i DIRECTORY
-i
Specify the directory of the image to load (input), the image in that directory is loaded.
Tag an Image
docker tag IMAGE_NAME:TAG NEW_NAME:NEW_TAG
- Allows you to reference an existing image with a new name and tag.
- e.g.,
docker tag ubuntu:22.04 abcd:0.1
Container
List Containers
docker ps [OPTION]
- Lists running containers.
-a
Show all containers, including those that have stopped.
Inspect Container Details
docker inspect CONTAINER_NAME
Run a Container from an Image
docker run [OPTIONS] IMAGE_NAME
--name CONTAINER_NAME
Set the container name--rm
Delete the container after therun
command is executed. One-time use of the container-it
Keep passing terminal input to the container-i
Keep stdin open even if not attached-t
Allocate a pseudo-TTY, use TTY mode to write commands to the shell
-d
Run the container in the background. When this option is specified, the container ID is output.-e
Add environment variables. Use as many as you need to add.- e.g.,
docker run -e APP_ENV=production APP2_ENV=dev ubuntu:22.04 env
- e.g.,
-p HOST_PORT:CONTAINER_PORT
Bind a specific port of the container connected to the host to the port of the host. Usually used to expose the web server port to the outside.-w DIR
Change the working directory-v HOST_DIR:CONTAINER_DIR
Mount a specific directory of the host to the container- e.g.,
docker run -v volume:/data ubuntu:22.04
- Mount the current working directory to the container
docker run -v `pwd`:/opt ubuntu:22.04
- e.g.,
-u USER_ID
Access the container with a specific user ID. The account must be added when building the image.
Execute Commands in a Running Container
docker exec CONTAINER_ID or NAME CMD
-it
Run the shell in the container environment,
Difference between run
and exec
run
: Run a container from an imageexec
: Run commands in an already running container
Stop a Container
docker stop CONTAINER_ID/NAME
- Stop a running container (Graceful shutdown)
docker kill CONTAINER_ID/NAME
- Force stop a running container
Restart a Container
docker start CONTAINER_ID/NAME
- Restart a stopped container
docker restart CONTAINER_ID/NAME
- Stop and then restart the container