原创

docker-compsoer构建spirngboot,nginx,redis应用

1.docker

使用docker需要如下环境和基础知识
1.下载docker建议搜索菜鸟教程的docker教程,各种技术入门都很好的网站
2.下载docker-compose,这是python写的,也可以用pip安装,yum也可以
3.学习简单的docker基础知识
4.学习Dockerfile构建镜像,学习docker-compse编排多个镜像
5.linux基本命令操作

2.构建博客基础镜像

1.首先新建Dockerfile文件写入如下内容
2.准备jdk1.8.0_211.zip,博客应用jar包blog-web-2.2.1.jar

FROM ubuntu:latest

RUN apt-get update; 
RUN apt-get install -y unzip
#RUN apt-get install -y maven

COPY jdk1.8.0_211.zip /usr/local/
RUN cd /usr/local/;unzip -q jdk1.8.0_211.zip
ENV JAVA_HOME /usr/local/jdk1.8.0_211
ENV PATH $PATH:$JAVA_HOME/bin
RUN java -version

COPY blog-web-2.2.1.jar /usr/local/
#CMD ["/usr/local/jdk1.8.0_211/bin/java","-jar","/usr/local/blog-web-2.2.1.jar"]
#docker run --network=fblog -p 8081:8081  -itd 033aa305d9d5

3.执行命令

ZBMAC-C02WD12BH:blog lishihao$ docker build -t blog:onlinev1 .
Sending build context to Docker daemon  485.9MB
Step 1/9 : FROM ubuntu:latest
 ---> a2a15febcdf3
....省略!
 ---> 1f03370e3c4c
Step 8/9 : RUN java -version
 ---> Running in 3c6fc3d0a615
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
Removing intermediate container 3c6fc3d0a615
 ---> 964cb5434673
Step 9/9 : COPY blog-web-2.2.1.jar /usr/local/
 ---> cfb43f1f0176
Successfully built cfb43f1f0176
Successfully tagged blog:onlinev1

博客应用基础镜像构建完成

3.docker-compose构建应用依赖的应用

1.创建文件docker-compose.yml文件写入如下内容

version: '3'
services:
    php:
      image: "php:7.2"
      ports:
        - "9000:9000"
      restart: always 
      stdin_open: true
      tty: true
    nginx:
      network_mode: "bridge"
      image: "nginx:1.16.1"
      volumes:
            - "./nginx/html:/usr/share/nginx/html"
            - "./nginx/conf.d:/etc/nginx/conf.d"
      ports:
        - "80:80"
      restart: always 
    redis:
      image: "redis:latest"
      ports:
        - "6379:6379"
      restart: always 
      networks:
            fblog:
                aliases:
                    - dkredis
    dkmyqsl56:
        environment:
            MYSQL_ROOT_PASSWORD: "123456"
            MYSQL_USER: 'root'
            MYSQL_PASS: '123456'
        image: "mysql:5.6"
        restart: always
        volumes:
            - "./mysql56/mysqldatadir:/var/lib/mysql"
            - "./mysql56/conf/my.cnf:/etc/my.cnf"
        ports:
            - "3306:3306"
        networks:
            fblog:
                aliases:
                    - dkmyqsl56
    blog:
      build: .
      #image: "blog:dev2"
      depends_on:
        - dkmyqsl56 
        - redis
      restart: always
      ports:
          - "8081:8081"
      networks:
            - fblog
      command: /usr/local/jdk1.8.0_211/bin/java -jar /usr/local/blog-web-2.2.1.jar
networks:
  fblog:
    external: true

2.创建mysql挂载数据目录 mkdir -p ./mysql56/mysqldatadir
3.创建mysql挂载配置目录 mkdir -p ./mysql56/conf/my.cnf
4.单独启动mysql镜像,最好单独启动不用compose启动,然后进入镜像 命令docker exec -it mysql容器id /bin/bash
5.去mysql配置文件/etc/my.cnf目录把my.conf文件复制到挂载目录下面./mysql56/conf/my.cnf
6.nginx的挂载配置同mysql了...,配置不好可以联系我

4.运行

1.运行主要2个命令即可非常简单初始化构建docker-compose build
2.docker-compose build只需要运行一次即可,第一次构建时间比较长,如果Dockerfile有修改就需要再次运行build

ZBMAC-C02WD12BH:blog lishihao$ docker-compose build
php uses an image, skipping
nginx uses an image, skipping
redis uses an image, skipping
dkmyqsl56 uses an image, skipping
Building blog
Step 1/9 : FROM ubuntu:latest
 ---> a2a15febcdf3
...省略掉
Step 8/9 : RUN java -version
 ---> Using cache
 ---> 321488ccc2fc
Step 9/9 : COPY blog-web-2.2.1.jar /usr/local/
 ---> Using cache
 ---> 0eae41d8787b
Successfully built 0eae41d8787b
Successfully tagged blog_blog:latest

3.构建运行编排 docker-compose up -d

ZBMAC-C02WD12BH:blog lishihao$ docker-compose up -d
Starting blog_nginx_1     ... done
Starting blog_dkmyqsl56_1 ... done
Starting blog_php_1       ... done
Starting blog_redis_1     ... done
Recreating blog_blog_1    ... done

好了至此本博客的docker-compose编排就完成了,以后就可以自由自在的迁移了

正文到此结束
本文目录