Use of Docker and Docker
Docker architecture
- Docker daemon (Docker daemon): Docker daemon is a background process running on the host (DOCKER-HOST). It can communicate with it through the Docker client.
- Client (Docker client): Docker client is the user interface of Docker, it can accept user commands and configuration identification, and communicate with the Docker daemon. In the figure, docker build and so on are all related commands of Docker.
- Images (Docker image): Docker image is a read-only template that contains instructions for creating Docker containers. It is a bit similar to the system installation CD. The system can be installed using the system installation CD. Similarly, the Docker image can be used to run the programs in the Docker image.
- Container: A container is a runnable instance of an image. The relationship between image and container is a bit similar to the relationship between class and object in object-oriented. You can start, stop, move, and delete containers through Docker API or CLI commands.
- Registry: Docker Registry is a service that centrally stores and distributes images. After the Docker image is built, it can be run on the current host. There are a large number of excellent images stored on Docker Hub, we can use Docker commands to download and use.
Docker commonly used commands
Basic command
command | effect |
---|---|
docker version | You can view the version information of docker |
docker info | View docker's system information, number of images and containers |
docker --help | View all docker commands |
docker command --help | View the help information of the current command |
docker system df | Show docker details |
docker volume rm data volume folder | Delete docker's data volume folder |
docker volume prune | Delete useless data volumes |
docker volume ls | View docker's volume |
docker network ls | View docker's network |
Mirror command
command | effect | parameter | Example & explanation |
---|---|---|---|
docker run [parameter] image name | Boot mirror | -d Run in the background --rm delete when used up | docker run -p 8080:8080 -d tomcat |
docker images | View all local mirrors | -a Show all -q show only id |
The name is Mirror and Dangling Mirror: It only displays the mirror information but it is not in the container. |
docker search image name | Search mirror list | --filter It is recommended to go directly to dockerHub for filtering |
docker search mysql --filter=STARS=5000 filter images with STARS greater than 5000 |
docker pull image name | Get the mirror from the remote | docker pull tomcat default lastest version |
|
docker rmi name or id | Delete the specified mirror | -f $(docker images -aq) delete all images | docker rmi tomcat can delete multiple images by space |
docker image prune | Delete dangling mirror | May be triggered by docker pull or docker build | |
docker tag image id new image name: version | Change the information of the mirror |
docker run description
Parameter Description
- --Name assign a name to the container --name tomcat1
- -d Let the container run in the background (run in daemon mode)
- -it use interactive mode to run into the container docker run -it centos /bin/bash
/bin/bash
use bash to interact - -p 8080:8080 specifies that the left side of the port is the host port and the right side is the container port
- -P random designated port
- -v ./webapps/ROOT/:/usr/tomcat/webapps/ROOT/ The mapping path of the host on the left of the data volume The mapping path of the container on the right is separated by a colon. If the directory does not exist, it will be created automatically
- -e configure environment variables
- If you do not start using words -d want to quit container by container
CTRL+P+Q
can not exit the container exit to the outside of the container
docker run -v description
- Mounting is divided into Named mount withAnonymous mount
Anonymous mount
- docker run -v /etc/nginx/conf This is an anonymous mount, only the directory in the container is specified, and the host directory is not specified
- In this case, docker will create a directory for the container immediately
Named mount
- docker run -v conf:/etc/nginx/conf This is to mount the specified volume name and the directory in the container by name
- In this case docker will generate a directory according to the name you specify
The directories generated by these two mounting methods are in the core directory of docker
/var/lib/docker/volumes # docker存放数据卷的目录
# 进入目录 可以查看 所有的数据卷
root@yufire:/var/lib/docker/volumes# ls
33c1fdb0df03784e20d585bce84266e40a032ce3bb4d26b9f2467c521f3b2d21 ecc57ad17f08d760aab21286e28eb6da56bb8d483d1f43e3fc5d5293d0263824 redis.conf
5c061cd30891f00fb7e44e02f8309cb982083c04ce03050b32a8b11d4f4fa38c metadata.db
Mount the specified directory
- docker run -v /etc/nginx/conf:/etc/nginx/conf-This is the automatic synchronization of the specified directory mounting container and the host directory sharing
docker run --volumes-from 说明
Data synchronization between containers (sharing)
docker run -d --name nginx01 nginx # 运行主容器
docker run -d --name nginx02 --volumes-from nginx01 # 使用子容器继承父容器
# 两个容器之间的数据就可以胡互通了
- If the inherited parent container is closed, is the data still there?
- The answer is still there: If any sub-container is running, even if there is only one data, it will not be lost
- Data will be synchronized between the mounted containers and data will be transmitted to each other
in conclusion
- This method can be used as configuration sharing between clusters to simplify configuration
docker pull instructions
If you have already downloaded image there is a low local version of the words docker pull
will beStratification
Hierarchical means: you can use the local existing low version and high version to share the same place directly use the local in downloading other different places
- Benefits: Greatly save storage space
Container command
command | effect | parameter | Example |
---|---|---|---|
docker rm name or id | Delete the specified container | -f Force delete $(docker ps -aq) delete all containers | docker rmi tomcat docker rim -f tomcat |
docker ps | View the running container | -a view all containers including those that are not running -q only view the id of the container | |
docker stats id | View the resource situation of the host machine occupied by the container | ||
docker start id | Start a stopped container | docker start container id | |
docker restart id | Restart a stopped container | ||
docker stop id | Stop the running container | docker stop container id | |
docker kill id | Kill container | docker kill container id can use this command if the container reports an error | |
docker exec -it name或id /bin/bash | Enter the specified container that is running | /bin/bash is how to interact with Ctrl+D or exit | |
docker attach container id | The second way to enter the container | enter directly | |
docker container prune | Delete all stopped containers | ||
docker build -t name . | Build the Dockerfile | -t specifies the name of the built image. Represents the context | |
docker cp host directory container ID:/container path | Copy files from the host to the container | docker cp /usr/local/tomcat 96f7f14e99ab:/usr/local/tomcat | |
docker cp container ID:/container path host path | Copy files from the container to the host | docker cp 96f7f14e99ab:/usr/local/tomcat /usr/local/tomcat | |
docker logs id | View the log of container startup | -tf --tail 10 display 10 logs | docker logs tomcat or id |
docker top id | View the process of the container | docker top 96f7f14e99ab | |
docker inspect id | View detailed information (metadata) of the container | ||
docker history id | Check how the image is built |
docker exec command description
- docker exec -it container id /bin/bash enter the container in interactive mode and use bash for interaction