Welcome to our guide on managing Docker images and containers! Whether you’re new to Docker or looking to expand your knowledge, this comprehensive tutorial will equip you with the essential commands and techniques for handling Docker images and containers like a pro.
Images
Download an Image from a Registry Like DockerHub
docker pull image-name:tag
Replace
image-name
with the name of the Docker image, andtag
with the version or tag you want to pull.
List All Images
docker images
Build an Image
docker build -t custom-image-name:tag .
Replace
custom-image-name
with the desired name for your image andtag
with the version or tag.
Push an Image to a Registry
docker push image-name:tag
Replace
image-name
with the name of the Docker image andtag
with the version or tag.
Remove an Image
docker rmi image-name:tag
Replace
image-name
with the name of the Docker image andtag
with the version or tag. If you have running containers using the image, you may need to stop and remove them first.
Remove All Unused Images
docker image prune
This command removes all images that are not associated with any containers.
Save Image to Tar File
docker save -o image-name.tar image-name:tag
Load Image from Tar file
docker load -i image-name.tar
Replace
image-name
with the name of the Docker image andtag
with the version or tag.
Containers
Run a Container
docker run -d -p local-port:service-port image-name:tag
docker run -d --name my-container -p local-port:service-port image-name:tag
Replace
my-container
with the desired container name, andimage-name:tag
with the Docker image and tag.
List Running Containers
docker ps
docker ps -a
Add the
-a
flag to include stopped containers
Start & Stop a Container
docker stop container-id
docker start container-id
Check Logs of a Container
docker logs container-id
Removing Containers
docker rm container-id
docker rm container-id -f
docker rm -f $(docker ps -a -q)
Add the
-f
flag to forcefully remove a running container
docker rm -f $(docker ps -a -q)
: Removes all containers, both running and stopped, by passing their IDs to thedocker rm
command.
Here is the Github Repo for this Article!