重温dockerfile&dockerCompose
复习dockerfile
将应用程序及其需要的系统函数库、环境、配置、依赖,打包
常用命令
指令 | 说明 | 示例 |
---|---|---|
FROM | 指定镜像base | FROM centos:7 |
ENV | 设置环境变量,供后续指令使用 | ENV key value |
COPY | 拷贝物理机文件对镜像指定目录 | COPY ./mysql-5.7.rpm /tmp |
RUN | 执行Linux中的shell命令 | RUN yum install gcc |
EXPOSE | 指定容器运行时监听的端口 | EXPOSE 8080 |
ENTRYPOINT | 镜像中的启动命令,在容器运行时调用 | ENTRYPOINT java -jar xxx.jar |
案例
# 指定基础镜像
FROM ubuntu:16.04
# 配置环境变量,JDK的安装目录
ENV JAVA_DIR=/usr/local
# 拷贝jdk和java项目的包
COPY ./jdk8.tar.gz $JAVA_DIR/
COPY ./docker-demo.jar /tmp/app.jar
# 安装JDK
RUN cd $JAVA_DIR \
&& tar -xf ./jdk8.tar.gz \
&& mv ./jdk1.8.0_144 ./java8
# 配置环境变量
ENV JAVA_HOME=$JAVA_DIR/java8
ENV PATH=$PATH:$JAVA_HOME/bin
# 暴露端口
EXPOSE 8090
# 入口,java项目的启动命令
ENTRYPOINT java -jar /tmp/app.jar
上述的工作等价于
# 指定基础镜像
FROM java:8-alpine
# 拷贝jdk和java项目的包
COPY ./docker-demo.jar /tmp/app.jar
# 暴露端口
EXPOSE 8090
# 入口,java项目的启动命令
ENTRYPOINT java -jar /tmp/app.jar
dockerCompose
可以基于Compose文件,实现快速部署分布式应用
案例
不建议docker中部署MySQL,仅作为案例!
version: "3.8"
services:
mysql:
image: mysql:5.7.25
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- /tmp/mysql/data:/var/lib/mysql
- /tmp/mysql/conf/hmy.cnf:/etc/mysql/conf.d/hmy.cnf
web:
build: .
ports:
- 8090: 8090
安装
最简单的办法:
-
下载
docker-compose
-
拖入
/usr/local/bin
-
添加执行权限
chomod +x /usr/local/bin/docker-compose
实战
compose
version: "3.2"
services:
nacos:
image: nacos/nacos-server
environment:
MODE: standalone
ports:
- "8848:8848"
mysql:
image: mysql:5.7.25
environment:
MYSQL_ROOT_PASSWORD: 123
volumes:
- "$PWD/mysql/data:/var/lib/mysql"
- "$PWD/mysql/conf:/etc/mysql/conf.d/"
userservice:
build: ./user-service
orderservice:
build: ./order-service
gateway:
build: ./gateway
ports:
- "10010:10010"
files
.
│ docker-compose.yml
│
├─gateway
│ app.jar
│ Dockerfile
│
├─mysql
│ ├─conf
│ │ hmy.cnf
│ │
│ └─data
│ │ ...
│ │
│ ├─cloud_order
│ │ ...
│ │
│ ├─cloud_user
│ │ ...
│ │
│ ├─mysql
│ │ ...
│ │
│ ├─performance_schema
│ │ ...
│ │
│ └─sys
│ ...
│
├─order-service
│ app.jar
│ Dockerfile
│
└─user-service
app.jar
Dockerfile
gateway.Dockerfile; order-service.Dockerfile; user-service.Dockerfile
FROM java:8-alpine
COPY ./app.jar /tmp/app.jar
ENTRYPOINT java -jar /tmp/app.jar
构建
docker-compose up -d
重启服务
docker-compose restart gateway userservice orderservice
关闭服务(不销毁)
docker-compose stop
# --------------------------------------------
# Stopping cloud-demo_nacos_1 ... done
# Stopping cloud-demo_gateway_1 ... done
# Stopping cloud-demo_orderservice_1 ... done
# Stopping cloud-demo_userservice_1 ... done
# Stopping cloud-demo_mysql_1 ... done
查看日志
docker-compose logs -f
标签:...,compose,app,jar,mysql,docker,dockerfile
From: https://www.cnblogs.com/jentreywang/p/17133436.html