45

学习 Docker(三):Dockerfile

 4 years ago
source link: https://www.tuicool.com/articles/ruq2aa2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

创建 Docker Image 主要有两种方式:

docker commit
docker build

在介绍 Dockerfile 之前,需要了解 Layer(层)的概念。

Layer

Docker Image 实际上是由基础的 Docker Base Image 和在其上的 Layer 构建而成的。

以构建一个 Spring Boot 应用为例:

+--------------------------------------------+
|                                            |
|                                            |
|                 ENTRYPOINT                 |
|                                            |
|                                            |
+--------------------------------------------+
|                                            |
|                                            |
|                    COPY                    |
|                                            |
|                                            |
+--------------------------------------------+
|                                            |
|                                            |
|                 EXPOSE 8080                |
|                                            |
|                                            |
+--------------------------------------------+
|                                            |
|                                            |
|            openjdk:8-jdk-alpine            |
|                                            |
|                                            |
+--------------------------------------------+

由底到顶,每层依次为:

① 使用 openjdk:8-jdk-alpine 镜像作为基础镜像;

② 暴露 8080 端口;

③ 拷贝本地软件包到镜像;

④ 运行应用。

Dockerfile

相应的 Dockerfile 为:

FROM openjdk:8-jdk-alpine  
EXPOSE 8080  
COPY target/app.jar app.jar  
ENTRYPOINT ["java", "-jar", "/app.jar"]

FROM 指令指定 Docker Base Image;

EXPOSE 指令通知 Docker 在运行时应用监听的端口;

COPY 指令拷贝本地文件或目录到容器;

ENTRYPOINT 指令定义了可执行的命令。

构建

构建 Docker Image:

docker build -t spring-boot:alpine .

查看 Docker Image:

docker history spring-boot:alpine

参考


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK