Springboot+Mysql+Redis使用Docker部署常用命令及配置

  • 服务器目录结构

Springboot+Mysql+Redis使用Docker部署常用命令及配置_第1张图片

  • Springboot应用的dockerfile文件配置
    FROM java:8
    VOLUME /tmp
    ADD supplier-platform-1.0.0.RELEASE.jar app.jar 
    RUN sh -c 'touch /app.jar'
    ENV JAVA_OPTS=""
    ENTRYPOINT [ "sh", "-c", "java -jar /app.jar" ]
  • Redis服务的redis.config文件配置
    # Redis configuration file example.
      bind 0.0.0.0
    
      protected-mode no
    
      port 6379
    
      tcp-backlog 511
    
      timeout 0
    
      tcp-keepalive 300
    
      daemonize no
    
      supervised no
    
      pidfile /var/run/redis_6379.pid
    
      loglevel notice
    
      logfile ""
    
      databases 16
    
      always-show-logo yes
    
      save 900 1
      save 300 10
      save 60 10000
    
      stop-writes-on-bgsave-error no
    
      rdbcompression yes
    
      rdbchecksum yes
    
      dbfilename dump.rdb
    
      dir ./
    
      # replicaof  
    
      # masterauth 
    
      replica-serve-stale-data yes
    
      replica-read-only yes
    
      repl-diskless-sync no
    
      repl-diskless-sync-delay 5
    
      # repl-ping-replica-period 10
    
      # repl-timeout 60
    
      repl-disable-tcp-nodelay no
    
      # repl-backlog-size 1mb
    
      # repl-backlog-ttl 3600
    
      replica-priority 100
    
      # min-replicas-to-write 3
      # min-replicas-max-lag 10
    
      # requirepass foobared
    
      # maxclients 10000
    
      # maxmemory-samples 5
      
      # replica-ignore-maxmemory yes
    
      lazyfree-lazy-eviction no
      lazyfree-lazy-expire no
      lazyfree-lazy-server-del no
      replica-lazy-flush no
    
      appendonly no
    
      appendfilename "appendonly.aof"
    
      # appendfsync always
      appendfsync everysec
      # appendfsync no
    
      no-appendfsync-on-rewrite no
    
      auto-aof-rewrite-percentage 100
      auto-aof-rewrite-min-size 64mb
    
      aof-load-truncated yes
    
      aof-use-rdb-preamble yes
    
      lua-time-limit 5000
    
      slowlog-log-slower-than 10000
    
      slowlog-max-len 128
    
      latency-monitor-threshold 0
    
      notify-keyspace-events ""
    
      hash-max-ziplist-entries 512
      hash-max-ziplist-value 64
    
      list-max-ziplist-size -2
    
      list-compress-depth 0
    
      set-max-intset-entries 512
    
      zset-max-ziplist-entries 128
      zset-max-ziplist-value 64
    
      hll-sparse-max-bytes 3000
    
      stream-node-max-bytes 4096
      stream-node-max-entries 100
    
      activerehashing yes
    
      client-output-buffer-limit normal 0 0 0
      client-output-buffer-limit replica 256mb 64mb 60
      client-output-buffer-limit pubsub 32mb 8mb 60
    
      hz 10
    
      dynamic-hz yes
    
      aof-rewrite-incremental-fsync yes
    
      rdb-save-incremental-fsync yes
    
  •  docker-compose.yml文件配置
    version : '2.2'
    services:
      mysql:
        image: docker.io/mysql:5.7
        ports:
          - "3308:3306"
        volumes:
          - /www/supplier-platform/mysql:/var/lib/mysql
        environment:
          MYSQL_DATABASE: supplier_platform
          MYSQL_ROOT_PASSWORD: 123456
          MYSQL_ROOT_HOST: '%'
        restart: always
        command: [
          '--character-set-server=utf8',
          '--collation-server=utf8_general_ci',
          '--default-time-zone=+8:00'
        ]
        networks:
          - net
    
      redis:
        image: "library/redis:alpine"
        environment:
          - TZ=Asia/Shanghai
        ports:
          - "6380:6379"
        volumes:
          - /home/dockers/supplier-platform/redis.conf:/usr/local/etc/redis/redis.conf
          - /www/supplier-platform/redis:/data/
          - /www/supplier-platform/redis/logs:/var/log/redis/
        command: redis-server /usr/local/etc/redis/redis.conf
        restart: always
        networks: 
          - net
    
      server:
        build:
          context: .
          dockerfile: dockerfile
        ports:
          - "8092:8092"
        volumes:
          - /www/supplier-platform/server/upload:/vol/development/file/upload
          - /www/supplier-platform/server/template:/vol/development/file/template
          - /www/supplier-platform/server/export:/vol/development/file/temporary
          - /www/supplier-platform/server/img:/vol/development/file/img
          - /www/supplier-platform/server/logs:/vol/development/logs
        environment:
          - TZ=Asia/Shanghai
        depends_on:
          - mysql
          - redis
        restart: always
        networks:
          - net
    
    networks:
      net:
        driver: bridge
  •   Springboot 中yml配置redis及mysql地址
spring:
  redis:
    host: redis  # redis:docker-compose中服务名
    port: 6379
    password:
    database: 5
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver
    url: jdbc:mysql://mysql:3306/supplier_platform?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8 # mysql:docker-compose中服务名
    username: root
    password: 123456
    driverClassName: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver
    # 连接池配置
    hikari:
      pool-name: HikariPool
      minimum-idle: 10
      maximum-pool-size: 100
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000

  •  查看镜像 
docker images
  •  删除镜像 
docker rmi 镜像名
  •  查看容器
docker ps
  •  删除容器 
docker rm 容器名(容器id)
  • 创建并启动容器(前台)
docker-compose up
  •  创建并启动容器(后台)
docker-compose up -d
  •   停止并删除容器
docker-compose down
  •   进入容器
 docker exec -it 容器名(容器id) /bin/bash
  •   退出容器
exit
  •    重新构建某个服务镜像并后台启动(镜像无改动将不会重新创建容器)
docker-compose up -d --build 服务名
  •    强制创建并重启某个服务镜像(即使镜像无改动)并后台启动,依赖的服务将保持不变
docker-compose up -d --force-recreate --build --no-deps 服务名
  •    重新构建服务镜像时,由于名称相同或者容器没有关闭,会生成镜像名为的无用镜像,也称悬挂镜像,使用下面命令删除所有悬挂镜像
 docker rmi $(docker images -f "dangling=true" -q)

你可能感兴趣的:(spring,boot,mysql,redis)