Useful commands for Docker

Srini
3 min readAug 10, 2020

--

  • 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(hub.docker.com). 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.

Example: docker run -it -p 8080:8080 <image_name>

$ 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.

Note: Before pushing the docker image to docker hub repository, it is necessary to tag the image as <username/image_name>

$ 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]

--

--

No responses yet