Useful commands for Docker

Image for post
Image for post
  • docker version :This command is used to get the currently installed version of docker
$ docker --version
  • docker pull :This command is used to pull images from the docker repository( By default, this command always downloads the latest version of image, though we can specify the particular image by using the tag name. (<image_name>:<tag_name>)
$ docker pull <image name>
  • docker run: This command is used to create a container from the mentioned image. Note :If a port is exposed in the dockerfile, we need to map it in the container by using ‘-p’ flag.
$ docker run -it -d <image name>
  • docker start: Start the existing container. We assume the container has already been downloaded and created.
$ docker start <CONTAINER>
  • docker ps: This command is used to list the running containers
$ docker ps
  • docker ps -a :This command is used to show all the running and exited containers
$ docker ps -a
  • docker exec :This command is used to access the running container
$ docker exec -it <container id> bash
  • docker stop: This command stops a running container
$ docker stop <container id>
  • docker kill: 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
$ docker kill <container id>
  • docker commit: This command creates a new image of an edited container on the local system
$ docker commit <container id> <username/image name>
  • docker login: Before pulling any docker image from docker hub repository, one needs to login to the docker hub repository. Hence this command is used to login the docker hub repository.
$ docker login
  • docker push: This command is used to push an image to the docker hub repository
$ docker push <username/image name>
  • docker images: This command lists all the locally stored docker images.
$ docker images
  • docker tag: This command is used to rename the existing docker image.
$ docker tag <image_name> <new_name>
  • List your images.
$ docker image ls
  • Delete a specific image.
$ docker image rm [image name]
  • Delete an image from local storage
$ docker rmi <image-id>
  • Docker Build : This command is used to build an image from a specified docker file.
$ docker build -t <name_the_image>[:<tag_name>] <path_to_docker_file>
  • Delete all existing images.
$ docker image rm $(docker images -a -q)
  • Change a container name at running time.
$ docker run --name [container name] [image name]
  • Stop all running containers.
$ docker stop $(docker ps -a -q)
  • Delete a specific container (only if stopped).
$ docker rm [container name]
  • Delete all containers (only if stopped).
$ docker rm $(docker ps -a -q)
  • Display logs of a container.
$ docker logs [container name]

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store