一.Dockerfile是什么
DockerFile是一个用于构建自定义镜像的配置文件,其中包含了一系列构建的指令
二.使用场景
当我们自己编写了一个web应用的时候,每次都需要将它打包部署到服务器端,服务器端配置了环境还好,没有配置环境的话还得单独配置给web服务的环境,部署一个应用就得配置一次环境很麻烦
但是有了dockerfile之后,我就只需要编写一个配置文件,然后执行一条命令就可以做成一个docker image,然后直接在拥有docker环境中run它就可以,是不是很方便.
三.简单使用
我们使用dockerfile打包一个helloworld的web应用吧
步骤1:
编写web应用
package com.antg.docker_hello_world.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
}
步骤2:
打成jar包,先测试在本地可以java -jar 运行起来
步骤3:
目录结构:
(base) fujunhua@Antg hello_world_web % ls
Dockerfile docker_hello_world-0.0.1-SNAPSHOT.jar
(base) fujunhua@Antg hello_world_web %
编写Dockerfile
# 说明你这镜像的母亲是谁(以谁为基础)
FROM java:8
# 作者
MAINTAINER Antg<798940311@qq.com>
# 将当前目录的jar包添加到容器
ADD docker_hello_world-0.0.1-SNAPSHOT.jar app.jar
# 容器启动后执行的命令 相当于 java -jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
步骤4:
构建镜像
(base) fujunhua@Antg hello_world_web % docker build -t myweb:v1 .
[+] Building 15.7s (7/7) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 177B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/java:8 15.4s
=> [internal] load build context 0.0s
=> => transferring context: 61B 0.0s
=> [1/2] FROM docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d 0.0s
=> CACHED [2/2] ADD docker_hello_world-0.0.1-SNAPSHOT.jar app.jar 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.0s
=> => writing image sha256:14e76e36b020c1fef3f7a2f143d09b1e039fe85b219064c851d9ec177eb0c9e2 0.0s
=> => naming to docker.io/library/myweb:v1 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
(base) fujunhua@Antg hello_world_web % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myweb v1 14e76e36b020 4 hours ago 661MB
tomcat latest fb5657adc892 3 months ago 680MB
wordpress latest c3c92cc3dcb1 3 months ago 616MB
mysql 5.7 c20987f18b13 3 months ago 448MB
centos latest 5d0da3dc9764 6 months ago 231MB
**注意:**构建的时候一定注意后面的点,他就是代表是从当前目录开始构建,是上下文环境
步骤5:
创建容器实例
(base) fujunhua@Antg hello_world_web % docker run --name helloweb -d -p 8081:8080 myweb:v1
4a890aa8273f7dd512b7d4d57da742a5fd5317b1d556271afb967f42e8f4b247
(base) fujunhua@Antg hello_world_web % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a890aa8273f myweb:v1 "java -jar app.jar" 5 seconds ago Up 3 seconds 0.0.0.0:8081->8080/tcp helloweb
步骤6:
宿主机测试接口
四.常用的Dockerfile命令详解
COPY
COPY 源路径 目标路径
复制指令,将当前上下文路径中的文件复制到容器中因为docker是CS模式,本机是c,docker引擎是s,实际构建的过程是在s端做的,所以无法找到本地的文件
所以就需要将本地的文件cp到docker引擎上
其中构建命令最后的点就是制定了从当前路径开始构建,copy的时候的相对路径就是当前路径下
注意:目标路径没得的话会自己创建
ADD
ADD 源路径 目标路径
和COPY指令一样都是复制
但是ADD会把压缩的文件自动解压到目标路径
所以根据是否需要解压文件选择对应的指令
CMD
CMD [“java”,"-jar",“app.jar”]
在容器run的时候运行
写多条CMD指令仅最后一条生效
run的时候追加的指令会覆盖CMD的指令
ENTRYPOINT
ENTRYPOINT [“java”,"-jar",“app.jar”]
在容器run的时候运行
多个ENTRYPOINT仅最后一个生效
不会被docker run的参数覆盖,但是, 如果运行 docker run 时使用了 --entrypoint 选项,将覆盖 ENTRYPOINT 指令指定的程序。
一般结合CMD一起使用
FROM nginx
ENTRYPOINT [“nginx”, “-c”] # 定参
CMD ["/etc/nginx/nginx.conf"] # 变参
RUN
RUN [“可执行文件”, “参数1”, “参数2”]
构建的时候执行的指令
RUN一次就会在镜像上新建一层
所以一般多个RUN写法如下
FROM centos
RUN yum -y install wget
&& wget -O redis.tar.gz “http://download.redis.io/releases/redis-5.0.3.tar.gz”
&& tar -xvf redis.tar.gz
ENV
ENV
ENV = =
设置环境变量,在后面的构建指令中就可以使用这些环境变量
ARG
ARG <参数名>[=<默认值>]
和ENV一样,不过这个参数仅在构建的过程中有效
VOLUME
VOLUME ["<路径1>", “<路径2>”…]
VOLUME <路径>
定义匿名数据卷,在忘记run的是忘记挂载可以自动挂载上
docker run 的时候可以通过 -v 参数来指定挂载
具体什么是匿名数据卷和具名数据卷可以看改系列的数据卷篇
EXPOSE
EXPOSE <端口1>
声明端口,在docker run -P 的时候会自动映射到该端口上
WORKDIR
WORKDIR <工作目录路径>
指定工作目录,进入容器后的目录默认就是工作目录
五.常用构建参数详解
(base) fujunhua@Antg hello_world_web % docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
-o, --output stringArray Output destination (format: type=local,dest=path)
--platform string Set platform if server is multi-platform capable
--progress string Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--secret stringArray Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret
--ssh stringArray SSH agent socket or keys to expose to the build (only if BuildKit enabled) (format: default|<id>[=<socket>|<key>[,<key>]])
-t, --tag list Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build.
-f
指定dockerfile 推荐编写的时候起名为Dockerfile,这样就不用加这个参数
例如 docker build -t myweb:v1 -f mydockerfile .
-t
指定镜像的名称和tag
例如 docker build -t myweb:v1 .