OR博客
Docker入门(五)Docker Compose部署Spring Boot访问MySQL
OrdinaryRoad
创建于:2021-09-27 16:57:42
0
17
82
0
Docker Compose部署Spring Boot访问MySQL
# Docker入门系列 [Docker入门(一)安装Docker - 苗锦洲的个人博客 (ordinaryroad.top)](https://ordinaryroad.top/article/52.html) [Docker入门(二)安装Docker-Compose (ordinaryroad.top)](https://ordinaryroad.top/article/53.html) [Docker入门(三)Docker Compose部署MySQL - 苗锦洲的个人博客 (ordinaryroad.top)](https://ordinaryroad.top/article/54.html) [Docker入门(四)Docker Compose部署Redis - 苗锦洲的个人博客 (ordinaryroad.top)](https://ordinaryroad.top/article/55.html) [Docker入门(五)Docker Compose部署Spring Boot访问MySQL - 苗锦洲的个人博客 (ordinaryroad.top)](https://ordinaryroad.top/article/56.html) # Dockerfile ```dockerfile # Docker image for springboot file run # VERSION 0.0.1 # Author: eangulee # 基础镜像使用java FROM java:8 # 作者 MAINTAINER miaojinzhou <1962247851@qq.com> #工作目录 #WORKDIR /opt/docker/blog/ # VOLUME 指定了临时文件目录为/tmp。 # 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp VOLUME /tmp #声明了容器应该打开的端口,实际上不会有什么操作,只是一个提示 #EXPOSE 80 443 #定义参数 #ARG JAR_FILE # 时区配置 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN echo 'Asia/Shanghai' >/etc/timezone # 环境变量 #ENV HOST 81.70.194.184 # 将jar包添加到容器中并更名为app.jar ADD *.jar app.jar #拷贝本地文件到镜像中#复制上下文目录下的target/demo-1.0.0.jar 到容器里 #COPY ${JAR_FILE} penguin-log-web.jar # COPY和ADD的区别时COPY的SRC只能是本地文件,其他用法一致。 # 运行jar包 #RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] ``` # docker-compose.yml配置 ```yaml version: '3' services: hello_docker: build: context: . dockerfile: Dockerfile image: hello_docker:1.0.0 # 自动添加host extra_hosts: - "master:192.168.56.100" container_name: hello_docker restart: always networks: - nets ports: - "9260:9260" networks: nets: external: false ``` # 打包镜像 项目Git地址:[1962247851/hello_docker: 简单的Spring Boot项目,访问master虚拟机Docker Compose部署的MySQL和Redis (github.com)](https://github.com/1962247851/hello_docker) 先将jar包和Dockerfile复制到Vagrantfile所在目录(vagrant默认Vagrantfile所在目录就是共享目录) 然后在master上执行 ```bash touch /vagrant/Dockerfile touch /vagrant/hello_docker-0.0.1-SNAPSHOT.jar ``` 重启master,可以触发自动同步,然后复制到docker-compose.yml所在目录,执行 ```bash docker-compose build ``` # 运行 ```bash docker-compose up -d ``` # 进入容器查看Host ```bash [root@master hello_docker]# docker exec -it eeb6f53963cf bash ccartroot@eeb6f53963cf:/# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 192.168.56.100 master 172.19.0.2 eeb6f53963cf root@eeb6f53963cf:/# ``` 可以看到自动增加了 ```bash 192.168.56.100 master ```
评论
楼主暂时不想被别人评论哦~