docker-compose部署一个springboot项目(包含环境)
搭建docker及docker-compose环境
【说明】首先对于环境的搭建
- docker环境搭建
# 安装需要的软件包 yum install -y yum-utils device-mapper-persistent-data lvm2 # 设置stable镜像仓库 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 更新yum软件索引 yum makecache fast # 安装Docker-ce yum -y install docker-ce # 启动Docker systemctl start docker # 开机自启 systemctl enable docker # 配置Docker镜像加速器 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://c2yf9ia3.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
- docker-compose环境搭建
# 这里下载可能有点慢 sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose # 配置权限 sudo chmod +x /usr/local/bin/docker-compose
【警告】注意这里我选择的是1.29.2版本,因为我的docker版本是最新的,所以选择docker-compose也是最新的
- 创建一个公共的docker网络
- docker network ls
- 创建命令为
docker network create kuocai_network
- 编写dockerfile文件
#环境 FROM openjdk:8-jdk-alpine #日期问题,8小时差解决 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone # 这个可以不加,直接运行 COPY test-project-0.0.1-SNAPSHOT.jar /test-project.jar CMD ["java","-jar","test-project.jar"]
- 上传打包的test-project.jar包到dockerfile同级目录下执行以下命令
# test-project为镜像的名称,默认没给版本号就是最新版,如果指定build版本可用test-project:1.0 docker build -t test-project .
- 编写docker-compose.yml文件
version: '3' services: web: image: test-project environment: SPRING_DATASOURCE_URL: jdbc:mysql://test-project-mysql:3306/test_cdn?serverTimezone=Hongkong&useAffectedRows=true&characterEncoding=utf-8 SPRING_DATASOURCE_USERNAME: root SPRING_DATASOURCE_PASSWORD: test123 SPRING_REDIS_HOST: test-project-redis SPRING_REDIS_PORT: 6379 SPRING_REDIS_PASSWORD: test123 SPRING_STORAGE_TYPE: minio SPRING_MINIO_URL: http://test-project-minio:9000 SPRING_MINIO_ACCESS_KEY: test SPRING_MINIO_SECRET_KEY: test123 ports: - "8086:8080" depends_on: - test-project-mysql - test-project-redis - test-project-minio networks: - kuocai_network test-project-mysql: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: test123 MYSQL_DATABASE: test_cdn MYSQL_USER: test MYSQL_PASSWORD: test123 MYSQL_LOWER_CASE_TABLE_NAMES: 1 MYSQL_LOWER_CASE_COLUMN_NAMES: 1 ports: - "3309:3306" volumes: - ./mysql:/var/lib/mysql networks: - kuocai_network test-project-redis: image: redis ports: - "6376:6379" volumes: - ./redis.conf:/usr/local/etc/redis/redis.conf - ./redis-data:/data command: redis-server /usr/local/etc/redis/redis.conf networks: - test_network test-project-minio: image: minio/minio environment: MINIO_ACCESS_KEY: test MINIO_SECRET_KEY: test123 MINIO_LOG_LEVEL: debug ports: - "9002:9000" - "9003:9001" command: server /data volumes: - ./data:/data networks: - kuocai_network networks: kuocai_network: driver: bridge
【警告】上面的端口映射其实可以去掉的,因为他们都在同一个network下面,所以根据容器名称就可以直接访问了,同时springboot的配置文件中也是配置web.environment下的东西
- 最后执行
docker-compose up -d