Docker Commands
Docker Basics
- Check the Docker version
docker version
- To check all the available images
docker images
- Pull or download an image from a registry to your local machine.
docker pull <image name>
//Eg: docker pull nginx
- Run a container. Docker will pull the image first if it is not available locally.
-
NOTE: If you only provide the image name, Docker uses the default tag, usually
latest. You can also specify a version such asnginx:1.14. -
Common 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 an interactive environment-e- For environment variable
-
docker run <image-name>
//Eg: docker run nginx
- We can also pass a complete
.envfile
--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
bashso we should usesh
- NOTE: every image does not support
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 the list of available 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 specific or user-created 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 your own tagged image from 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 a bind mount.
- This helps you sync local files with a container.
-
Sync local changes into a container with a bind mount.
-vis used to define the mount. You can also mount a second path as read-only when needed.
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 the mount read-only, add :ro at the end of the mount definition.
- Mount the Docker socket into a container when you need that container to talk to the host Docker daemon, for example in some Jenkins setups.
docker run -it -v /var/run/docker.sock:/var/run/docker.sock docker-image:version bin/bash
Docker Compose
- Run a Docker Compose project.
Note: If you do not pass
-f, Docker looks forcompose.yamlordocker-compose.yamlin the working directory.
docker compose up -d
docker compose down
- Rebuild the image with new changes.
docker compose up --build
- Combine multiple Compose files.
docker compose -f compose.yaml -f compose.dev.yaml
Docker Swarm and Services
- Initialize Swarm
docker swarm init
- Check all available nodes
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
- Get all task containers running across nodes
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
- Check which services are running inside a stack
docker stack services <stack name>
- Check which tasks 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
docker run <image-name> <command>
// Eg: `docker 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
Read next
- Docker Introduction - Review the core Docker concepts behind these commands.
- Learning Resources - Continue with courses and videos for deeper practice.
- Other Resources - Explore sample Dockerfiles and Compose files.