background
Have you ever had such confusion?
It is often necessary to install software such as Mysql, Redis, RocketMQ, ES, Zookeeper, Nginx, Nacos, etc. on the local computer, which is time-consuming and laborious.
Whenever the computer is replaced or the system is reinstalled.
Whenever I need to do some tuning or testing at work, and these software services are usually built by operation and maintenance or DBA, I don’t even have permission to log in to the server machine to view it, which is distressing.
Whenever I want to learn, I am tossed about building the basic environment, which is a ruthless waste of time.
Therefore, in order to save time, I summed up dockerization with commonly used local software to achieve one-click startup. Is it very fragrant?
Special note: This article focuses on how to dockerize the software in practice, and the communication between the local host and the Docker service. For docker installation, the basic knowledge of docker, docker-compose commands and usage will not be explained.
Target
Each software docker service is accessed through a fixed IP, and the IP cannot be changed every time the docker service is restarted.
The machine directly communicates with the docker container through IP, not through 127.0.0.1 + port mapping.
It supports one-click deployment of multiple services and one-click deployment of a single service.
Implementation steps
Create a bridge named app-network
docker network create --driver bridge --subnet=172.30.1.0/24 --gateway=172.30.1.1 --opt "com.docker.network.bridge.name"="app-network" app-network
Configure docker services, all bridged to the same bridge app-network
Take mysql as an example (other services are similar)
version: '3.5' services: db: image: hub.c.163.com/library/mysql:5.7 restart: always container_name: "mysql_5_7" restart: always ports: - 3306:3306 environment: TZ: Asia/Shanghai MYSQL_ROOT_PASSWORD: root #command: volumes: - ./conf.d:/etc/mysql/mysql.conf.d - ./data:/var/lib/mysql networks: default: ipv4_address: 172.30.1.2 networks: default: external: name: app-network
Among them: MYSQL_ROOT_PASSWORD is root user login password, specify IP: 172.30.1.2
Write docker-compose.yml file
Write the startup script start.sh
#!/usr/bin/env bash # 创建目录 mkdir -p ./conf.d mkdir -p ./data # 设置目录权限 chmod -R 777 ./conf.d chmod -R 777 ./data # 下载并启动容器,且为 后台 自动启动 docker-compose up -d # 显示 mysql 容器 docker ps |grep mysql_5_7
start mysql
tanyawendeMacBook-Pro:mysql bytearch$ sh ./start.sh mysql_5_7 is up-to-date 1be6fba30c7c hub.c.163.com/library/mysql:5.7 "docker-entrypoint.s…" 7 hours ago Up 7 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql_5_7
So far, the mysql service has started successfully, and everything is going well, but the host (MAC computer) cannot ping the container
tanyawendeMacBook-Pro:mysql bytearch$ ping 172.30.1.2 PING 172.30.1.2 (172.30.1.2): 56 data bytes Request timeout for icmp_seq 0 Request timeout for icmp_seq 1 Request timeout for icmp_seq 2 Request timeout for icmp_seq 3 Request timeout for icmp_seq 4 Request timeout for icmp_seq 5 Request timeout for icmp_seq 6 Request timeout for icmp_seq 7 Request timeout for icmp_seq 8 Request timeout for icmp_seq 9
I tried the Aliyun CentOS system, and it can be pinged.
[root@VM_0_14_centos ~]# ping 172.30.1.2 PING 172.30.1.2 (172.30.1.2) 56(84) bytes of data. 64 bytes from 172.30.1.2: icmp_seq=1 ttl=64 time=0.028 ms 64 bytes from 172.30.1.2: icmp_seq=2 ttl=64 time=0.021 ms 64 bytes from 172.30.1.2: icmp_seq=3 ttl=64 time=0.017 ms 64 bytes from 172.30.1.2: icmp_seq=4 ttl=64 time=0.027 ms 64 bytes from 172.30.1.2: icmp_seq=5 ttl=64 time=0.023 ms 64 bytes from 172.30.1.2: icmp_seq=6 ttl=64 time=0.023 ms 64 bytes from 172.30.1.2: icmp_seq=7 ttl=64 time=0.020 ms
By reviewing the data, the reasons are as follows:
Docker on Mac system architecture
The implementation of docker in OSX is to first create a linux virtual machine, and then put docker into the virtual machine for implementation. For the communication between the linux virtual machine and OSX, the current version uses this socket file to communicate /var/run/docker.sock
. Naturally, the docker container cannot be pinged on the OSX host machine.
Docker in Linux system architecture
Docker is implemented on the basis of the linux kernel container. After installing docker in linux, a virtual network card called docker0 will be created. The communication between the linux host and the docker container is carried out through the docker0 virtual network card.
So for Linux systems, this is it! ! !
What about Macs? For a programmer who pursues perfection, this cannot be tolerated, and he must fight to the end.
Mac computer and docker container communication solution
After many attempts to find a solution (if any students have other alternatives, please let me know!)
Mac install docker-connector through brew
brew install wenjunxiao/brew/docker-connector
Execute the following command to add all docker
bridge
networks to the routedocker network ls --filter driver=bridge --format "{ {.ID}}" | xargs docker network inspect --format "route { {range .IPAM.Config}}{ {.Subnet}}{ {end}}" >> /usr/local/etc/docker-connector.conf
Or modify
/usr/local/etc/docker-connector.conf
to add a routeroute 172.30.1.0/24
The configuration is complete, restart the service
sudo brew services start docker-connector
Use the following command to run wenjunxiao/mac-docker-connector on the docker side, you need to use
host
the network, and allowNET_ADMIN,pull connector容器的作用是作为桥接
docker run -it -d --restart always --net host --cap-add NET_ADMIN --name connector wenjunxiao/mac-docker-connector
you're done
tanyawendeMacBook-Pro:docker-app bytearch$ ping 172.30.1.2 PING 172.30.1.2 (172.30.1.2): 56 data bytes 64 bytes from 172.30.1.2: icmp_seq=0 ttl=63 time=3.019 ms 64 bytes from 172.30.1.2: icmp_seq=1 ttl=63 time=3.751 ms 64 bytes from 172.30.1.2: icmp_seq=2 ttl=63 time=1.850 ms 64 bytes from 172.30.1.2: icmp_seq=3 ttl=63 time=3.992 ms 64 bytes from 172.30.1.2: icmp_seq=4 ttl=63 time=3.695 ms
other
The above source code has been open source, interested students can move to view.
Gitee: https://gitee.com/bytearch_admin/docker-app.git
Github: https://github.com/bytearch/docker-app.git
The directory structure is as follows:
.
├── README.md
├── install_network.sh
├── mac_connect_docker.md
├── mysql
│ ├── conf.d //数据库配置文件
│ ├── data //数据库data目录挂载,重启数据不丢失
│ ├── docker-compose.yml
│ ├── start.sh //启动脚本
│ └── stop.sh //关闭脚本
├── redis
│ ├── conf
│ ├── data
│ ├── docker-compose.yml
│ ├── start.sh
│ └── stop.sh
├── rocketmq
│ ├── docker-compose.yml
│ ├── readme.md
│ ├── rmq
│ ├── rmqs
│ ├── start.sh
│ └── stop.sh
├── startAll.sh //启动所有服务
├── stopAll.sh //关闭所有服务
└── zookeeper
├── docker-compose.yml
├── start.sh
├── stop.sh
├── zoo1
├── zoo2
└── zoo3
执行./startAll看看效果:
嗯,真香!要的就是这种效果!以后可以愉快地玩耍了!