Docker Commands
Docker is an open source platform that enables developers to build & deploy containers that combines application source code with OS, libraries and dependencies required to run that code.
Docker Objects
- Images: read only template with instructions for crating docker containers.
- Container: running instance of a docker image
- Network: network interface used to connect the containers to each other or external networks
- Volumes: used to persist the data generated by and used by the containers
- Registry: private or public collection of docker images
- Service: used to deploy application in a docker multi node cluster
Image Commands
# get the list of images available on the machine
> docker image ls
# download or pull the image on the machine
# > docker image pull <image name>
# the image gets stored in /var/lib/docker/images directory
> docker image pull hello-world
# remove image from machine
# > docker image rm <image name>
> docker image rm hello-world
# get information about an image
> docker image inspect hello-world
Container Commands
# get the list of running containers
> docker container ls
# get the list of containers in all states (created, updated)
> docker container ls -a
# create a container
> docker container create hello-world
# get the details of selected container
# > docker container inspect <container name or container id>
> docker container inspect c1c
# start already created container
> docker container start <container id or name>
# stop a running container
> docker container stop <container id or name>
# remove a stopped container
> docker container rm <container id or name>
# remove a running container
> docker container rm --force <container id or name>
# run the container
# create and start the container
# this command will run a new container every time
> docker container run <image name or id>
# set the name while running the application
# this container will run by default in attached mode
> docker container run --name <name> <image name or id>
# run the container in detached mode (background)
# -d: detached mode
# -i: enable the docker interactive mode
# -t: enable the teletype terminal for the docker
> docker container run --name <name> -itd <image name or id>
# enable port forwarding for a container
# source port
# - port allocated on OS
# - can be any available port
# container port
# - port exposed by the container
# > docker container run --name <name> -itd -p <source port>:<container port> <image name or id>
> docker container run --name myhttpd -itd -p 8080:80 httpd
# run mysql in a container
> docker container run --name mysql -itd -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql
# execute a command inside the container
# > docker container exec <container name or id> <command>
> docker container exec myhttpd date
# get the terminal from the container
# > docker container exec -it <container name or id> <shell>
> docker container exec -it myhttpd bash
# get the logs generated by the container
# > docker container logs <container name or id>
> docker container logs mysql
Volume
# get the list of volumes
> docker volume ls
# remove the unused volumes
> docker volume prune
# create a volume
# - this will create a new directory name myvolume under /var/lib/docker/volumes
> docker volume create myvolume
# find more information about a volume
> docker volume inspect <volume name>
# remove a volume
> docker volume rm <volume name>
# attach volume to a container
# > docker container run --name <name> -itd -p <source port>:<container port> -v <volume name>:<mount point inside the container> <image name>
> docker container run --name mysql -itd -p 3306:3306 -v myvolume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mysql
images
customize the docker image to run your application by using **Dockerfile**
Dockerfile contains instructions/commands to create an image
commands
**FROM**
used to select the base image
**COPY**
used to copy file/directory from local machine to the image
e.g. COPY index.html /usr/local/apache2/htdocs/ will copy the index.html from local machine to the /usr/local/apache2/htdocs/ of image
**WORKDIR**
used to set the working directory
if the directory does not exist, the image creates this new directory
**RUN**
used to run a command while building an image
installing dependencies while building an image
**CMD**
used to run the command when container starts
this must be the last command of your Dockerfile
**EXPOSE**
used to expose a port for consumer to access the application running inside the container
# build a custom image
# > docker image build -t <image name>:<image tag> <context>
> docker image build -t myimage .
# login with docker credentials
> docker login -u <user name>
# create a new tag for your image to push to the docker hub
# > docker image tag <existing image> <docker username>/<image name>
> docker image tag myserver rishabh/myserver
# push the image to the docker hub
# > docker image push <image name>
> docker image push rishabh/myserver
# to build the image for other CPU architecture use buildx command
> docker buildx build --platform <platform> .
swarm
- swarm is nothing but cluster. a cluster created by docker to run multiple containers.
- Docker Swarm is a container orchestration engine
- It takes multiple Docker Engines running on different hosts and lets you use them together
- The usage is simple: declare your applications as stacks of services, and let Docker handle the rest
- It is secure by default
- It is built using Swarmkit
# check if the node is a part of any swarm
> docker system info | grep Swarm
# start initializing the swarm
> docker swarm init
# remove the current node from the cluster
> docker swarm leave --force
# generate a token to add a worker
> docker swarm join-token worker
node
- node is a machine in a cluster. One of the node is manager and other are workers. Managers main job is to manage the workers.
# get the list of nodes
> docker node ls
# get the details of selected node
> docker node inspect <node id>
# remove a node from cluster
> docker node rm <node id>
# promote a worker to work as manager
> docker node promote <worker node id>
# demote a manager to work as worker
> docker node demote <manager node id>
service
- A service is the defination of the tasks to execute on the manager or worker nodes.
# get the list of services
> docker service ls
# create a service
# > docker service create --name <service name> <image name>
> docker service create --name myservice httpd
# create a service with required desired count
# > docker service create --replicas <desired count> --name myservice httpd
> docker service create --replicas 5 --name httpd -p 8080:80 httpd
# get the list of containers created by the service
> docker service ps <service name>
# remove a service
> docker service rm <service name>
# to watch the current state of service
> watch -n 1 docker container ls
# horizontally scale the service
# > docker service scale <service name>=<new desired count>
> docker service scale myservice=10
Docker compose
- used in development to deal with multiple microservices at a time
- in-built in the docker engine
- can be done using yaml configuration
- does not support Swarm
# get the list of services managed by docker compose
> docker compose ls
# create images for all the services mentioned in the docker-compose file
> docker compose build
# create containers for all the services
> docker compose up -d
# delete all the containers for the services
> docker compose down
# delete all the images along with the containers for the services
> docker compose down --rmi all
Docker stack
# get the list of stacks
> docker stack ls
# deploy or create a new stack
# > docker stack deploy --compose-file <stack yaml file> --detached=true <stack name>
> docker stack deploy --compose-file docker-stack.yaml --detached=true myapp
# remove a running stack
# > docker stack rm <stack name>
> docker stack rm myapp