一、MySQL

#下载 mysql 镜像
docker pull mysql:5.7

#启动容器
docker run -d -p 3306:3306 --name mysql \
-v /docker/mysql/log:/var/log/mysql \
-v /docker/mysql/data:/var/lib/mysql \
-v /docker/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root mysql:5.7

#修改配置文件
vi /docker/mysql/conf/my.cnf
i
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve

#重启
docker restart mysql

#开机自启
docker update mysql --restart=always

二、Tomcat

docker pull tomcat:9.0

docker run -d -p 8080:8080  --name tomcat  --restart=always  \
-v /docker/tomcat/webapps:/usr/local/tomcat/webapps    tomcat:9.0 \

docker exec -it tomcat /bin/bash

三、Nacos

#启动一个nacos
docker run -d -p 8848:8848  --name nacos   --restart=always --env MODE=standalone nacos/nacos-server:1.3.0
#把config文件夹复制出来
docker cp nacos:/home/nacos/conf /docker/nacos/conf
docker stop nacos
docker rm nacos
#再通过卷挂载config文件夹
docker run -d -p 8848:8848  --name nacos   --restart=always --env MODE=standalone \
-v /docker/nacos/conf:/home/nacos/conf  nacos/nacos-server:1.3.0

#修改nacos的数据源为mysql
vim /docker/nacos/conf/application.properties
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.168.117.129:3316/mall_nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=root

#重启nacos
docker restart nacos

四、nginx

4.1 单机版

#启动一个nginx
docker run -d   -p 80:80  --name nginx --restart=always  --ip 172.17.0.2 nginx
#把config文件夹复制出来
docker cp nginx:/etc/nginx/nginx.conf /docker/nginx/conf
docker cp nginx:/etc/nginx/conf.d/default.conf /docker/nginx/conf
docker stop nginx
docker rm nginx
#再通过卷挂载config、www、log文件夹
docker run -d   -p 80:80  --name nginx --restart=always  \
-v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  \
-v /docker/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf \
-v /docker/nginx/www:/usr/share/nginx/html \
-v /docker/nginx/logs:/var/log/nginx nginx

4.2 负载均衡集群

#另开三个nginx容器,从服务器可以不用暴露端口
docker run -d   --name nginx02 --restart=always --ip 172.17.0.4  nginx
docker run -d   --name nginx03 --restart=always --ip 172.17.0.5  nginx
docker run -d   --name nginx04 --restart=always --ip 172.17.0.6  nginx
#在默认配置中添加upstream和server两个字典
vim /docker/nginx/conf/default.conf

upstream 172.17.0.2 {
  server 172.17.0.4 weight=2;  
  server 172.17.0.5 weight=2; 
  server 172.17.0.6 weight=2; 
}


server {

    listen       80;
    server_name  172.17.0.2;


    location / {
    proxy_pass http://172.17.0.2;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }


    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}


五、redis

5.1 单机版

#创建配置文件
mkdir -p /docker/redis/conf
touch /docker/redis/conf/redis.conf



#启动容器
docker run -d -p 6379:6379 --name redis --restart=always \
-v /docker/redis/data:/data \
-v /docker/redis/conf/redis.conf:/etc/redis/redis.conf \
redis redis-server /etc/redis/redis.conf




#开启 aof 持久化
vim /docker/redis/conf/redis.conf

# 添加如下内容
appendonly yes

#重启 redis
docker restart redis

#测试
docker exec -it redis /bin/sh
cd  /usr/local/bin

5.2 集群高可用

#建一个网段给redis集群
docker network create --driver bridge --subnet 192.169.0.0/16 --gateway 192.169.0.1 redis_net

#多个网段间可以用docker network conect [netName] [Container] 打通

#shell创建redis节点:
for port in $(seq 1 6); \
do \
mkdir -p /docker/redis/node-${port}/conf
touch /docker/redis/node-${port}/conf/redis.conf
cat << EOF > /docker/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-announce-ip 192.169.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

for port in $(seq 1 6); \
do \
docker run -d -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /docker/redis/node-${port}/data:/data \
-v /docker/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
--net redis_net --ip 192.169.0.1${port} redis redis-server /etc/redis/redis.conf
done

#关联集群
docker exec redis-1 -it /bin/sh
cd  /usr/local/bin
redis-cli --cluster create \
192.169.0.11:6379 192.169.0.12:6379  192.169.0.13:6379  \
192.169.0.14:6379 192.169.0.15:6379  192.169.0.16:6379  \
--cluster-replicas 1

# 测试
redis-cli -c 
cluster info
cluster nodes

六、portainer

docker run -d -p 8088:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock  --privileged=true portainer/portainer

七、springboot

编写Dockerfile

FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]

将写好的.jar放到同目录,执行命令生成docker镜像

docker build -t springboot_demo .

八、zookeeper

docker run -d -p 2181:2181 --name zookeeper --restart always zookeeper

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议