整篇博客以个人论坛ZBlog命名,命名可替换
环境:CentOS 7 腾讯云
使用 yum 安装(CentOS 7下)
yum -y install docker
启动 Docker 后台服务
service docker start
测试运行 hello-world
docker run hello-world
docker network create --subnet=xxx.xx.xx.0/24 zblog-net
docker volume create zblog-mysql-volume
//查看创建的Volume
docker volume ls
docker run -d --name zblog-mysql -p 3306:3306 -v zblog-mysql-volume:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --network zblog-net --ip xxx.xx.xx.11 mysql:8.0.21
● 容器名称
docker ps -a
● volume
docker volume ls
docker volume inspect zblog-mysql-volume
进入容器
docker exec -it zblog-mysql /bin/bash
登录MySQL
mysql -uroot -proot
授权root远程访问
GRANT ALL ON *.* TO 'root'@'%';
修改密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
刷新权限
FLUSH PRIVILEGES;
maven clean package
FROM java:8
MAINTAINER your_author_name
COPY *.jar zblog.jar
EXPOSE 8080
CMD ["java","-jar","-Dfile.encoding=UTF-8","-Xms1024m","-Xmx1024m","zblog.jar"]
docker build -t zblog .
docker images
● zblog01
docker run -d --name zblog01 -p 8081:8080 --network zblog-net --ip xxx.xx.xx.12 zblog:1.0
● zblog02
docker run -d --name zblog02 -p 8082:8080 --network zblog-net --ip xxx.xx.xx.13 zblog:1.0
● zblog03
docker run -d --name zblog03 -p 8083:8080 --network zblog-net --ip xxx.xx.xx.14 zblog:1.0
docker ps -a
//查看ip
docker exec -it zblog01 ip a
//zblog01
xxx.xx.xx.xxx:8081/article/hot
//zblog02
xxx.xx.xx.xxx:8082/article/hot
//zblog03
xxx.xx.xx.xxx:8083/article/hot
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream backend{
server xxx.xx.xx.12:8080;
server xxx.xx.xx.13:8080;
server xxx.xx.xx.14:8080;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
include /etc/nginx/conf.d/*.conf;
}
docker run -d --name zblog-ngx -p 80:80 -v /tmp/nginx/nginx.conf:/etc/nginx/nginx.conf --network zblog-net --ip xxx.xx.xx.15 nginx
xxx.xx.xx.xx/article/hot
https://blog.csdn.net/BThinker/article/details/123374236
docker run \
-d \
--name zblog-redis \
-p 6379:6379 \
-v /home/redis/data:/data \
-v /home/redis/conf/redis.conf:/etc/redis/redis.conf \
--network zblog-net \
--ip 159.75.54.16 \
redis
#!/bin/bash
echo "SpringBoot项目部署开始"
echo "***********"
echo "开始移除原有容器"
docker rm -f $(docker ps -a | grep zblog0 | awk '{print $1}')
echo "移除结束"
echo "开始移除原有镜像"
docker rmi zblog
echo "移除结束"
echo "开始重新构建zblog镜像"
docker build -t zblog .
echo "构建结束"
echo "重启mysql、nginx和redis服务"
docker restart zblog-mysql
docker restart zblog-ngx
docker restart zblog-redis
echo "重启结束"
echo "开始重新构建三个zblog容器"
docker run -d --name zblog01 -v /home/zblog/data/zblog01-log:/logs/zblog-log -p 8081:8080 --network zblog-net --ip 159.75.54.12 zblog
docker run -d --name zblog02 -v /home/zblog/data/zblog02-log:/logs/zblog-log -p 8082:8080 --network zblog-net --ip 159.75.54.13 zblog
docker run -d --name zblog03 -v /home/zblog/data/zblog03-log:/logs/zblog-log -p 8083:8080 --network zblog-net --ip 159.75.54.14 zblog
echo "构建结束"