Skip to main content

Docker Commands

Docker Basic

  • To check Docker vesrion
docker version
  • To check all the available images
docker images
  • Pull/Download the image from the Docker registry to local machine.
docker pull <image name>
//Eg: docker pull nginx
  • To run an container (It will 1st pull the image if not present in the local sytem)
    • NOTE: When we just provide the name of the image it will pull the latest one, i.e nginx:latest. We can also specify the version nginx:1.14

    • Additioanly we can use flags

      • --name <name> - To give a name to the container.
      • -p <Host port:container port>- To forward the port.
      • -d - To run in detached mode
      • -it - For interactive envirnoment
      • -e - For environment variable
docker run <image-name>
//Eg: docker run nginx
  • We can also pass a complete .env file
--env-file <path-to-env-file>
Eg: --env-file ./.env

Docker Container

  • To stop a running container
docker stop <container-ID/name>
  • To resume a stopped container
docker start <container-ID/name>
  • To check the running processes inside a container.
docker top <container-name/id>
  • To check stats of running container.
docker stats <container-name/id>
  • Check the config and info of a container.
docker inspect <container-name/id>
//Eg: docker inspect mynginx
  • Check all the container running.
docker ps
or
docker container ls
  • To start and interactive session and attach terminal with the container.

    • NOTE: every image does not support bash so we should use sh
docker exec -it <container-ID/name> bash/sh
  • To check which ports has been exposed and forwarded
docker port <image-name>
  • To check all the containers (stopped, paused and running)
docker ps -a
  • Check logs of a container
docker logs <container-ID/name>
  • Delete all the stopped containers
docker container prune -f
  • Delete all the containers (stopped, paused and running)
docker rm -f $(docker ps -aq)
  • Delete all the images
docker rmi -f $(docker images -q)
  • Remove unused images
docker image prune -all
  • Auto cleanup when the container exits
 docker container run —rm <image-name>

Docker Network

  • Check list of avilable networks.
docker network ls
  • Inspect a network components, like which container are attached to that network.
docker network inspect <network-name>
  • Run a container on a certian network/own careted network
docker run --network <network-name> <image-name>
docker inspect --format "{{.NetworkSettings.IPAddress}}" <container-name>

Docker Images

  • Remove an image
docker rmi <image name> -f
  • Remove all the images at once
docker rmi $(docker images -q)
  • To inspect an image layers and other info
docker inspect  <image-name/id>
  • Check the image layers formation
docker history <image-name/id>
  • Create a our own image with an existing image.
docker image tag <image-name:tag> <new-image-name:tag>
docker image tag nginx pradumna/nginx:hello
docker image tag ubuntu:18.04 pradumna/ubuntu:example

Docker Volume

  • Create bind mount

    • Help to sync our local files with help of Docker container.
  • To sync our local machine changes with help of Docker volume (Bind mount)

    • - v is use to define volume, also we give another -v flag to override the changes so that it will not chnage in container.
docker run -v <path-to-folder-on-local-machine>:<path-to-folder-on-container> -p <host-port>:<container-port> -d --name docker-node docker-node
docker
docker run -v <path-to-folder-on-local-machine>:<path-to-folder-on-container> -v <path-to-file/folder-on-container> -p <local-machine-port>:<container-port> -d --name docker-node docker-node

To make it read only so that when you add some files inside it, the container will not get created on your local machine use -v port:port:ro

  • docker volume command for mounting the docker socket to the docker container for accessing the host's docker daemon for performing the continuous integration in jenkins while using docker as a agent..
docker run -it -v /var/run/docker.sock:/var/run/docker.sock docker-image:version bin/bash

Docker Compose

  • Run docker compose file. Note: By default it finds for the file name docker-compose.yaml, to give file with other naming use -f <file-name.yaml> command
docker compose up -d
docker compose down
  • To rebuilt the new Image with thew new changes
docker compose up --build
  • Override the existing of compose file
docker compose -f docker-compose.yaml  -f docker-compose.dev.yaml

Docker Swarm and Services

  • Initalize swarm
docker swarm init
  • Check all the node available
docker node ls
  • To add a node as a manager
docker node update --role manager <node-name>
  • To create an overlay network
docker network create -d overlay backend
  • Create a service. Also we can add flags for further customization.

    • --name - to give a service name
    • --replicas - to define how many running instance of the same image.
    • -p - for port forwarding
docker service create -p 8080:80 --name vote --replicas 2 nginx
  • To get all task containers running on different node
docker service ps <service-name/id>

SERVICE UPDATE

  • To scale up the service (i.e increasing the no of replicas)
docker service scale <service name>=<no to scale>
docker service scale mynginx=5
  • To update the image in running service
docker service update --image <updated image> <service name>
docker service update --image mynginx:1.13.6 web
  • To update the port

We can't directly update the port We have to add and remove the ports

docker service update --publish-rm 8080 --publish-add 808180 <service name>
docker service update --publish-rm 8080 --publish-add 808180 mynginx

Docker Stack

  • To deploy a stack file
docker stack deploy -c <file-name.yaml> <stackname>
  • To remove running stack
docker stack rm <stack name>
  • To check list of stacks running
docker stack ls

STACK -> SERVICES -> TASKS -> CONTAINERS

  • To check which services are running inside a staacks
docker stack services <stack name>
  • To check taks are running inside a stack
docker stack ps <stack name> 

Registry

127.0.0.0:5000/v2/_catalog

Tips and Short hands

  • Run the command with the container creation
doc run <image-name> <command>
// Eg: `doc run ubuntu:16.04 echo hey`
  • Creating our Own image and container.
Step 1 - create Dockerfile
Step 2 - docker build -t myimage:1.0 <path-of-dockerfile> (-t for tag)
Step 3 - docker run myimage:1.0