Docker CMDs
20 Feb 2024 #docker
Image
Search for an Image
docker search [OPTIONS] IMAGE_NAME
--automated=falseShow only automated builds--no-trunc=falseShow all results-s=n--stars=nShow only images with at least n stars- e.g.,
docker search --stars=100 mysql
Download an Image
docker pull [OPTIONS] IMAGE_NAME[:TAG_NAME]
-aDownload all versions of the imageTAG_NAMESpecify 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--allShow all images--digestsShow digests-q--quietShow 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
-fForce 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
-iSpecify 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.
-aShow 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_NAMESet the container name--rmDelete the container after theruncommand is executed. One-time use of the container-itKeep passing terminal input to the container-iKeep stdin open even if not attached-tAllocate a pseudo-TTY, use TTY mode to write commands to the shell
-dRun the container in the background. When this option is specified, the container ID is output.-eAdd 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_PORTBind 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 DIRChange the working directory-v HOST_DIR:CONTAINER_DIRMount 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_IDAccess 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
-itRun 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