Docker Commands with Examples

All the important and commonly used docker commands for your quick reference. This is a cheat sheet for docker commands for managing images and containers.

Build an image from a Dockerfile:

This command is used to build an image from a specified docker file
docker build -t <image-name> <path to docker file>

For example:

docker build -t springboot-docker-image .

List docker images:

This command lists all the locally stored docker images
docker images

Display detailed information on one or more images:

This command is used to get the detailed information of specified docker images.
docker inspect <docker-image>
For example:
docker inspect springboot-docker-image

Show the history of an image:

This command is used to get the history of a particular docker image.
docker image histroy <docker-image>
For example:
docker image history springboot-docker-image

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE:

docker image tag <docker-image> <docker-image:tag>
For example:
docker image tag springboot-docker-image springboot-docker-image:0.1

Remove or Delete a Docker Image

This command is used to delete an image from local storage
docker rmi <image-id>/<image-name>
For example:
docker rmi springboot-docker-image

Remove or Delete a Docker Image Forcefully:

This command is used to delete an image from local storage
docker rmi -f <image-id>/<image-name>
For example:
docker rmi -f springboot-docker-image

Pull an image or a repository from a registry:

This command is used to pull the docker image from the Docker hub.
docker image pull <docker-image>/<repository-name>
For example:
docker image pull rabbitmq

Remove unused images:

This command is used to remove all unused images from local storage.
docker image prune

Push an image or a repository to a registry:

To push a local image to the docker registry, you need to associate the local image with a repository on the docker registry.
 

To tag an image, we use the docker tag command:

docker tag image username/repository:tag

Finally, use the docker push command to push the tagged image to the docker hub like so -

docker push username/repository:tag
For example:

Command to tag the image: 

docker tag springboot-docker-image javaguides/springboot-docker-image:1.0.RELEASE
Command to push the tagged image:
docker push javaguides/springboot-docker-image:1.0.RELEASE

Run the docker image in a new container:

docker run [docker_image]
For example:
docker run -p 8080:8080 springboot-docker-image
-p option to map host port with container port.

Give a name to the container:

docker run --name <container-name> <image-name>
For example:
docker run --name springboot-docker-container -p 8080:8080 springboot-docker-image

Run container in background and print container ID:

docker run -d <docker-image>
-d option to run the container in detached mode (background).
For example:
docker run -d -p 8080:8080 springboot-docker-image

Fetch the logs of a container

docker logs -f <container-id>/<container-name>
For example:
docker logs -f springboot-docker-container

List all the docker running containers:

docker ps

Docker container pause:

docker container pause <container-id>/<container-name>
For example:
docker container pause springboot-docker-container

Docker container unpause:

docker container unpause <container-id>/<container-name>
For example:
docker container unpause springboot-docker-container

Kill docker container:

docker kill <container-id>/<container-name>
For example:

docker kill springboot-docker-container
This command kills the container by stopping its execution immediately. The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shutdown gracefully, in situations when it is taking too much time for getting the container to stop, one can opt to kill it.

Stop running container:

docker stop <container-id>/<container-name>
For example:

docker stop springboot-docker-container

List all running and exited containers:

docker container ls -a

Remove all stopped containers:

docker container prune

Related Tutorials

Comments