Article directory
- 1. Introduction to Docker
- 2. Install Docker on Linux
- Three, Docker common commands (important)
- Fourth, configure the Docker orchestration tool docker-compose (the most important)
- Five, the use of docker-compose
1. Introduction to Docker
Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable image, and then publish it to any popular Linux or Windows operating system machine, and can also implement virtualization. Containers use a sandbox mechanism completely, and there will be no interfaces between them.
Why use docker? Existing Problems?
1. Environmental inconsistency: For example, the code is the same, but due to environmental problems, it cannot run or an error occurs
2. Isolation problems (due to a multi-user system, there may be a problem with one user, and other users are involved)
3. Elastic scaling (fast Horizontal expansion of multiple servers, one-click deployment)
4. Learning costs (when learning software, the cost of installing software is too high)
.
Docker has helped us solve the above problems very well
Docker core idea
- Image: A container that stores the operating system, environment, and software.
- Container: Run the image, which is the container.
- Warehouse: The place where images are stored.
2. Install Docker on Linux
1. The installation depends on the environment, and then set the mirror source to Ali (you can install it in yum mode)
# 安装依赖的环境
yum -y install yum-utils device-mapper-persistent-data lvm2
# 设置镜像源为阿里
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装Docker,稍微等几分钟
yum -y install docker-ce
The installation is successful, the result is as follows
2. Set warehouse information
First check the central warehouse address:https://hub.docker.com/
The mirror in the central warehouse is the most complete, but the download speed is slow.
Docker default download mirror address
Check out our domestic warehouses:http://hub.daocloud.io/
One of the domestic mirror warehouses, the download speed is accelerated
Next, docker configures the domestic warehouse address
1. /etc/docker
Create a new daemon.json in the directory and configure the address information
{
"registry-mirrors": ["http://hub.daocloud.io/"]
}
2. Restart the daemon
systemctl daemon-reload
3. Restart Docker
systemctl restart docker
4. View running docker information
docker info
As shown in the figure below, you can finally see that the default mirror address has been changed successfully
Three, Docker common commands (important)
1. Pull the image to the local
docker pull 镜像地址
Go to the mirror market and see the following picture: just copy the address and execute the command
2. View the local mirror
docker images
3. Delete the mirror image
docker images
4. View the startup container log
docker logs -f 容器标识
5. View running containers
Only view the running containers, -a: means all, view all containers, -q: means only view the id of the container
docker ps -qa(或者 -a 或者 -q)
6. Container Control
Start, stop, restart, and delete management containers... Only stopped containers can be deleted!
docker start|stop|restart|rm 容器标识
7. Enter into the container
Entering the container, the container is actually an independent operating system
docker exec -it 容器标识 bash
8. Host and container copy file directories to each other
The host copies the content to the container
docker cp 宿主机文件 容器标识:容器目录
Fourth, configure the Docker orchestration tool docker-compose (the most important)
1. Download Docker-compose
Download Docker-Compose (download is a file docker-compose-Linux-x86_64)
After the download is complete, copy the docker-compose-Linux-x86_64 file to the Linux systemroot目录下
and execute the following command
Download link 传送门
:
Link: Extract code:
https://pan.baidu.com/s/124vfP_N1kzBeiDAnITWHYg
xzq9
2. Rename and modify permissions
mv docker-compose-Linux-x86_64 docker-compose
chmod 777 docker-compose
3. Move the docker-compose file to $PATH
echo $PATH
View system variable storage path
mv docker-compose /usr/bin/
4. Enter docker-compose anywhere
This is 最后一步
, as shown in the figure below, the configuration is successful! ! !
Five, the use of docker-compose
1. Create docker-compose.yml text
1. Create docker-compose.yml text: Note that there are some when writing
key: value之间是有空格
! !
The preliminary template description is as follows:例如常用的属性进行说明,还有其他的属性字段可以按需去详细了解...
version: '3.1'
services:
tomcat: # 服务名称
image: 镜像名称 # 镜像标识
restart: always # docker启动,当前容器必启动
container_name: 容器名称 # 容器名称
ports: # 映射多个端口
- 宿主机端口:容器端口
- 宿主机端口:容器端口
environment: # 编写多个环境
- 环境名=环境值
- 环境名=环境值
volumes: # 映射多个数据卷
- 宿主机相对yml文件的路径:容器的绝对路径
- 宿主机相对yml文件的路径:容器的绝对路径
Examples are as follows:
version: '3.1'
services:
mysql:
image: "daocloud.io/library/mysql:5.7.5-m15"
container_name: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_USER: 'root'
MYSQL_PASS: 'root'
volumes:
- "./data:/var/lib/mysql"
Example explanation:
version: version number
services: managed service
mysql: custom service name (here, take the mysql service as an example, named mysql)
image: pull a good image
container_name: start the container identification name (generally consistent with the service name)
ports: mapping Port
environment: environment configuration (there are many attributes configured in it, you can learn about it later as needed)
volumes: map multiple data volumes
Data volumes are mentioned above:
2. Learn about data volumes
Data volume specification
宿主机的一个目录,映射上容器内部的一个目录
Add content to the directory of the host, and the directory inside the container will also append corresponding content.
The content generated or added inside the container will also be generated in the directory of the host.
This directory of the host is the data volume
Data volumes can solve two problems:
1. The internal commands of the container are incomplete, resulting in inconvenient operation. It can be operated on the host through the data volume, and then mapped to the inside of the container
2, storage-type middleware, if the container is deleted, the data will be lost. The persistent files of the storage middleware can be mapped to the host through the data volume. Even if the container is deleted, the host still has the persistent files.
3. Common commands for docker-compose
Later, you can
docker-compose.yml
manage container information through text
bydocker-compose up -d
starting the container
bydocker-compose down
stopping and deleting the container
bydocker-compose logs -f 标识
viewing the container startup log
todocker-compose start/stop/rm/restart/up/down
start the container/close the container/delete the closed container/restart the container/create a container based on yml/stop and delete the container
Basic middleware, databases, and components used in microservices can be deployed quickly through Docker. The
Docker deployment is as follows:能够很快上手环境搭建,而不用去手动配置繁琐的环境以及担心版本问题,减少学习成本、提高开发效率
So far, Docker can quickly deploy and manage services through subsequent containerization...