docker 部署总结

1.docker

// 安装必要工具
yum install -y yum-utils device-mapper-persistent-data lvm2
// 设置yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
//下载docker
yum install -y docker-ce 
//启动docker
systemctl start docker 

sudo service docker start /stop /restart
docker ps -a //查看运行的镜像
docker exec -it xxx bash //进行xxx镜像
docker pull xx       //下载镜像
docker rmi xx       //删除镜像
docker run xx       //运行容器
docker exec -it xx /bin/bash  //进入容器
docker rm xx        //删除容器
docker stop xx     //停止容器
docker restart xx  //重启容器
docker rename 原名 改后名  //更改容器别名

2.docker 换源

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://5bwox49q.mirror.aliyuncs.com"]
}
EOF

sudo systemctl daemon-reload
sudo systemctl restart docker

3.mysql

1、安装

docker pull mysql  
#进入容器 docker 
exec -it mysql bash
#进入 mysql 
mysql -u root -p    
#给root远程登录权限 
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; 
#刷新 
FLUSH PRIVILEGES;
#修改密码 
alter user 'root'@'localhost' identified by '123456'; 
#设置 root账号的密码 
update user set authentication_string = password('root') where user = 'root';
#对root进行授权 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

2、配置

//新建一个mysql的配置文件
vim /usr/blog/mysql/conf/my.cnf

mkdir -p /usr/blog/mysql/conf //mysql配置路径
mkdir -p /usr/blog/mysql/data //数据路径
mkdir -p /usr/blog/mysql/logs //日志路径

3、运行

docker run -d 
-p 3307:3307 --privileged=true 
-v /usr/blog/mysql/conf/my.cnf:/etc/mysql/my.cnf 
-v /usr/blog/mysql/logs:/logs 
-v /usr/blog/mysql/data:/var/lib/mysql 
--name mysql 
-e MYSQL_ROOT_PASSWORD=123456 
-e TZ=Asia/Shanghai 
mysql

docker run -d -p 3307:3307 --privileged=true -v /home/blog/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /usr/blog/mysql/logs:/logs -v
/home/blog/mysql/data:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD=123456 -eTZ=Asia/Shanghai mysql

4、my.conf

[mysqld]
port = 3307
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
symbolic-links=0

4.redis

1.安装

 docker pull redis

2.配置

mkdir -p /mydata/redis/conf
touch /mydata/redis/conf/redis.conf

3.运行

docker run 
-p 6379:6379 
--name redis 
-v /mydata/redis/data:/data 
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf 
-d redis redis-server /etc/redis/redis.conf

docker run --name redis --restart=always -p 6379:6379 -d redis --requirepass "123456"

4.redis.conf

# 配置文件进行了精简,完整配置可自行和官方提供的完整conf文件进行对照。端口号自行对应修改
#后台启动的意思
daemonize yes 
#端口号
port 6381
# IP绑定,redis不建议对公网开放,这里绑定了服务器私网IP及环回地址
bind 172.17.0.13 127.0.0.1
# redis数据文件存放的目录
dir /redis/workingDir
# 日志文件
logfile "/redis/logs/cluster-node-6381.log"
# 开启AOF
appendonly yes
 # 开启集群
cluster-enabled yes
# 集群持久化配置文件,内容包含其它节点的状态,持久化变量等,会自动生成在上面配置的dir目录下
cluster-config-file cluster-node-6381.conf
# 集群节点不可用的最大时间(毫秒),如果主节点在指定时间内不可达,那么会进行故障转移
cluster-node-timeout 5000

5.nginx

1、安装

docker pull nginx

2、配置


3、运行

#启动nginx,映射本地配置文件
docker run --name nginx --restart=always -p 80:80 -p 8081:8081 -p 8082:8082 -p
8083:8083 -d -v /usr/blog/nginx/nginx.conf:/etc/nginx/nginx.conf -v
/usr/blog/vue:/usr/blog/vue -v /usr/blog/upload:/usr/blog/upload nginx

docker run 
--name nginx 
--restart=always 
-p 80:80 -p 8081:8081 -p 8082:8082 -p 8083:8083 
-d 
-v /home/blog/nginx/nginx.conf:/etc/nginx/nginx.conf 
-v /home/blog/vue:/usr/blog/vue 
-v /home/blog/upload:/usr/blog/upload 
nginx 

4、nginx.conf

events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    client_max_body_size 50m;
    client_body_buffer_size 10m;
    client_header_timeout 1m;
    client_body_timeout 1m;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 4;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
server {
    listen 80;
    server_name 192.168.0.172;
    location / {
        root /home/blog/vue/blog;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    location ^~ /api/ {
      proxy_pass http://192.168.0.172:8080/;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
server {
listen 8081;
server_name 192.168.0.172;
  location / {
    root /home/blog/vue/admin;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
  location ^~ /api/ {
    proxy_pass http://192.168.0.172:8080/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}
server {
  listen 8082;
  server_name 192.168.0.172;
  location / {
    proxy_pass http://192.168.0.172:8080/websocket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}
server {
    listen 8083;
    server_name 192.168.0.172;
    location / {
                root /usr/local/upload/;
            }
    }
}

6.rabbitMQ

1、安装

docker pull rabbitmq:management

2、配置


3、运行

#启动RabbitMQ,默认guest用户,密码也是guest。 15672
docker run --name rabbit --restart=always -p 15672:15672 -p 5672:5672  -d  rabbitmq:management 

4、rabbitmq.conf


7.elasticsearch

1、安装

docker pull elasticsearch

2、配置


3、运行

#启动elasticsearch,默认guest用户,密码也是guest。 9200
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.9.2

#进入elasticsearch容器
docker exec -it elasticsearch /bin/bash 

4、elasticsearch.conf


8.kibana

1、安装


2、配置


3、运行

#启动kibana,默认guest用户,密码也是guest。 5601
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.9.2

#进入elasticsearch容器
docker exec -it elasticsearch /bin/bash 

4、kibana.conf


编写Dockerfile

上传dockerfile到usr/local/blog/docker中

FROM openjdk:8
VOLUME /tmp
ADD blog-springboot-0.0.1.jar blog-springboot.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/blog-springboot.jar"]
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

编写启动脚本 blog-start.sh

#源jar路径
SOURCE_PATH=/usr/blog/docker
#docker 镜像/容器名字或者jar名字 这里都命名为这个
SERVER_NAME=blog-springboot.jar
TAG=latest
SERVER_PORT=8080
#容器id
CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
#镜像id
IID=$(docker images | grep "$SERVER_NAME:$TAG" | awk '{print $3}')
if [ -n "$CID" ]; then
echo "存在容器$SERVER_NAME, CID-$CID"
docker stop $CID
docker rm $CID
fi
# 构建docker镜像
if [ -n "$IID" ]; then
echo "存在$SERVER_NAME:$TAG镜像,IID=$IID"
docker rmi $IID
else
echo "不存在$SERVER_NAME:$TAG镜像,开始构建镜像"
cd $SOURCE_PATH
docker build -t $SERVER_NAME:$TAG .
fi
# 运行docker容器
docker run --name $SERVER_NAME -v /usr/local/upload:/usr/local/upload -d -p $SERVER_PORT:$SERVER_PORT $SERVER_NAME:$TAG
echo "$SERVER_NAME容器创建完成"

docker run --name blog-springboot.jar -v /usr/local/upload:/usr/local/upload -d -p 8080:8080 blog-springboot.jar:latest

常用指令

1、端口映射

yum install net-tools
netstat -anp | grep 3306

2、文件复制

scp -p -r /Users/zhangdada/blog/blog-vue/blog/dist [email protected]:/usr/local/vue

防火墙

安装 apt-get install firewall

开启防火墙systemctl start firewalld.service
关闭防火墙systemctl stop firewalld.service
开机自动启动systemctl enable firewalld.service
禁止开机启动systemctl disable firewalld.service

查看开启的端口 firewall-cmd --list-ports

启动端口号 firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --list-ports
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --zone=public --add-port=4369/tcp --permanent
firewall-cmd --zone=public --add-port=5671/tcp --permanent 
firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --zone=public --add-port=15671/tcp --permanent
firewall-cmd --zone=public --add-port=25672/tcp --permanent
firewall-cmd --zone=public --add-port=80/tcp --permanent

在不改变状态的条件下重新加载防火墙 firewall-cmd --reload
修改配置后需要重启服务使其生效 systemctl restart firewalld.service

查看当前所有tcp端口· netstat -ntlp

查看所有1935端口使用情况 netstat -ntulp |grep 1935

问题

问题 一、 ERROR 1273 (HY000): Unknown collation: ‘utf8mb4_0900_ai_ci‘

是因为linux下MySQL版本不兼容导致的
把文件中的所有的utf8mb4_0900_ai_ci替换为utf8_general_ci 以及utf8mb4替换为utf8
本地:mysql 8.0.19
服务器:mysql 5.7.29
高版本导入到低版本,引起1273错误

问题二、 ERROR 1045 (28000): Access denied for user ‘root‘@‘172.17.0.1‘ (using password: YES)

不允许远程连接
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'Grand_p0ss' WITH GRANT OPTION;
FLUSH PRIVILEGES; 授予权限
密码错误
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; 修改密码 

问题三、连接 Linux 阿里云报错-[08S01] Communications link failure The last packet sent successfully to the serve

https://blog.csdn.net/qq_42557844/article/details/118365259

问题四、解决运行docker命令要用sudo的问题

1.查看是否创建docker 组  cat /etc/group | grep docker
2.创建docker组,第一步打印出东西证明以前建过,就直接跳过这一步。 sudo groupadd docker 
3.将用户添加到docker分组  sudo usermod -aG docker 用户名
4.查看有没有生效  cat /etc/group
5.重启docker sudo systemctl restart docker
6.给docker.sock添加权限 sudo chmod a+rw /var/run/docker.sock
7.切换当前会话到新 group 或者重启 X 会话  newgrp - docker

你可能感兴趣的:(centos)