dockerfile常用命令(ADD 特别说明)

本文详细介绍了Dockerfile中关键命令如FROM、ADD、CMD等的用途、用法和示例,强调了ADD命令中构建上下文的重要性,以及如何正确处理文件路径和权限问题。
摘要由CSDN通过智能技术生成

dockerfile常用命令(ADD 特别说明)

  1. FROM:

    • 用途:指定基础镜像,构建新镜像的起点。
    • 用法:FROM <镜像名称>:<标签>FROM <镜像名称>@<摘要>
    • 用法示例:FROM ubuntu:20.04
  2. MAINTAINER(已过时,建议使用 LABEL):

    • 用途:设置镜像的作者信息。
    • 用法:MAINTAINER <作者信息>
    • 用法示例:MAINTAINER John Doe <johndoe@example.com>
  3. ADD:

    • 用途:将文件或目录从构建上下文(通常是 Dockerfile 所在目录)复制到镜像中。
    • 用法:ADD <源路径> <目标路径>
    • 用法示例:ADD app.jar /app/
  4. CMD:

    • 用途:定义容器启动时要执行的默认命令。
    • 用法:CMD ["可执行文件", "参数1", "参数2"]CMD 命令 参数1 参数2
    • 用法示例:CMD ["python", "app.py"]
  5. ENV:

    • 用途:设置环境变量。
    • 用法:ENV <变量名> <值>
    • 用法示例:ENV DATABASE_URL=mysql://user:password@localhost/db
  6. RUN:

    • 用途:在构建过程中执行命令,用于安装软件、配置环境等。
    • 用法:RUN <命令>
    • 用法示例:RUN apt-get update && apt-get install -y python
  7. ENTRYPOINT:

    • 用途:设置容器启动时要执行的入口命令。
    • 用法:ENTRYPOINT ["可执行文件", "参数1", "参数2"]ENTRYPOINT 命令 参数1 参数2
    • 用法示例:ENTRYPOINT ["java", "-jar", "app.jar"]
  8. WORKDIR:

    • 用途:设置容器内的工作目录。
    • 用法:WORKDIR <目录路径>
    • 用法示例:WORKDIR /app
  9. EXPOSE:

    • 用途:声明容器运行时监听的网络端口。
    • 用法:EXPOSE <端口号>
    • 用法示例:EXPOSE 80
  10. VOLUME:

    • 用途:声明容器中的数据卷,用于持久化存储。
    • 用法:VOLUME ["/数据卷路径"]
    • 用法示例:VOLUME ["/data"]
  11. ONBUILD:

    • 用途:定义触发器命令,在子镜像中执行。
    • 用法:ONBUILD <指令>
    • 用法示例:ONBUILD ADD . /app

例子:

如何构建请看具体例子

# 一个jdk、tomcat环境的配置
FROM centos:7
MAINTAINER xwhking<2837468248@qq.com>
ADD jdk-8u171-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.80.tar.gz /usr/local/
RUN yum -y install vim
RUN yum -y install iproute
RUN yum -y install net-tools
ENV MYPATH /usr/local
WORKDIR $MYPATH
# 配置jdk和tomcat环境
ENV JAVA_HOME /usr/local/jdk1.8.0_171
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.80
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

#容器运行时监听的端口
EXPOSE 8080
# 启动时运行tomcat
CMD /usr/local/apache-tomcat-9.0.80/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.80/bin/logs/catalina.out

构建镜像

Usage:  docker buildx build [OPTIONS] PATH | URL | -

Start a build

Aliases:
  docker buildx build, docker buildx b

Options:
      --add-host strings              Add a custom host-to-IP mapping (format: "host:ip")
      --allow strings                 Allow extra privileged entitlement (e.g., "network.host",
                                      "security.insecure")
      --attest stringArray            Attestation parameters (format: "type=sbom,generator=image")
      --build-arg stringArray         Set build-time variables
      --build-context stringArray     Additional build contexts (e.g., name=path)
      --builder string                Override the configured builder instance (default "default")
      --cache-from stringArray        External cache sources (e.g., "user/app:cache",
                                      "type=local,src=path/to/dir")
      --cache-to stringArray          Cache export destinations (e.g., "user/app:cache",
                                      "type=local,dest=path/to/dir")
      --cgroup-parent string          Optional parent cgroup for the container
  -f, --file string                   Name of the Dockerfile (default: "PATH/Dockerfile")
      --iidfile string                Write the image ID to the file
      --label stringArray             Set metadata for an image
      --load                          Shorthand for "--output=type=docker"
      --metadata-file string          Write build result metadata to the file
      --network string                Set the networking mode for the "RUN" instructions during
                                      build (default "default")
      --no-cache                      Do not use cache when building the image
      --no-cache-filter stringArray   Do not cache specified stages
  -o, --output stringArray            Output destination (format: "type=local,dest=path")
      --platform stringArray          Set target platform for build
      --progress string               Set type of progress output ("auto", "plain", "tty"). Use
                                      plain to show container output (default "auto")
      --provenance string             Shorthand for "--attest=type=provenance"
      --pull                          Always attempt to pull all referenced images
      --push                          Shorthand for "--output=type=registry"
  -q, --quiet                         Suppress the build output and print image ID on success
      --sbom string                   Shorthand for "--attest=type=sbom"
      --secret stringArray            Secret to expose to the build (format:
                                      "id=mysecret[,src=/local/secret]")
      --shm-size bytes                Size of "/dev/shm"
      --ssh stringArray               SSH agent socket or keys to expose to the build (format:
                                      "default|<id>[=<socket>|<key>[,<key>]]")
  -t, --tag stringArray               Name and optionally a tag (format: "name:tag")
      --target string                 Set the target build stage to build
      --ulimit ulimit                 Ulimit options (default [])

特别注意

ADD命令使用的源地址一个要和镜像中的目的地址

**源地址是以在docker build 【options】.(一般是.) **中.为基础地址的,因为在构建的时候docker只会把这个目录下的文件传到docker客户端进行构建,否则会找不到文件.

主要理解构建上下文,就是指的是构建命令是指定的地址为基础

在Dockerfile中,ADD命令用于将文件或目录从构建上下文(通常是Dockerfile所在目录)复制到镜像中。要确保正确指定源地址,需要考虑以下几点:

  1. 构建上下文ADD命令相对于Dockerfile所在的目录来解释路径。如果你的Dockerfile位于 /root 目录下,你可以将相对路径或绝对路径作为源地址。

  2. 源文件的存在:确保源文件或目录确实存在于指定的路径下。如果文件不存在,Docker构建将会失败。

  3. Dockerfile所在的目录:在Dockerfile所在的目录中使用相对路径时,确保你的Dockerfile与源文件的相对路径正确匹配。

  4. 绝对路径:如果要使用绝对路径,确保指定的绝对路径是正确的,并且在Docker构建的上下文中可访问。

  5. 构建上下文的内容:构建上下文是通过docker build命令的最后一个参数指定的。在你的示例中,如果Dockerfile位于/root目录下,那么/root/jdk-8u171-linux-x64.tar.gz应该在构建上下文中可访问。

例如,如果你的Dockerfile位于/root目录下,并且要将jdk-8u171-linux-x64.tar.gz添加到镜像中,你可以使用以下Dockerfile中的ADD命令:

ADD jdk-8u171-linux-x64.tar.gz /目标目录

确保jdk-8u171-linux-x64.tar.gz文件存在于/root目录下,或者在构建上下文中。如果不在构建上下文中,你需要在构建上下文中包含它。要注意,构建上下文通常是docker build命令的最后一个参数,因此确保正确指定构建上下文。

如果你仍然遇到问题,可以检查Dockerfile所在目录和构建上下文,确保路径和文件存在性都是正确的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xwhking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值