赞
踩
FROM:
FROM <镜像名称>:<标签> 或 FROM <镜像名称>@<摘要>FROM ubuntu:20.04MAINTAINER(已过时,建议使用 LABEL):
MAINTAINER <作者信息>MAINTAINER John Doe <johndoe@example.com>ADD:
ADD <源路径> <目标路径>ADD app.jar /app/CMD:
CMD ["可执行文件", "参数1", "参数2"] 或 CMD 命令 参数1 参数2CMD ["python", "app.py"]ENV:
ENV <变量名> <值>ENV DATABASE_URL=mysql://user:password@localhost/dbRUN:
RUN <命令>RUN apt-get update && apt-get install -y pythonENTRYPOINT:
ENTRYPOINT ["可执行文件", "参数1", "参数2"] 或 ENTRYPOINT 命令 参数1 参数2ENTRYPOINT ["java", "-jar", "app.jar"]WORKDIR:
WORKDIR <目录路径>WORKDIR /appEXPOSE:
EXPOSE <端口号>EXPOSE 80VOLUME:
VOLUME ["/数据卷路径"]VOLUME ["/data"]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所在目录)复制到镜像中。要确保正确指定源地址,需要考虑以下几点:
构建上下文:ADD命令相对于Dockerfile所在的目录来解释路径。如果你的Dockerfile位于 /root 目录下,你可以将相对路径或绝对路径作为源地址。
源文件的存在:确保源文件或目录确实存在于指定的路径下。如果文件不存在,Docker构建将会失败。
Dockerfile所在的目录:在Dockerfile所在的目录中使用相对路径时,确保你的Dockerfile与源文件的相对路径正确匹配。
绝对路径:如果要使用绝对路径,确保指定的绝对路径是正确的,并且在Docker构建的上下文中可访问。
构建上下文的内容:构建上下文是通过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所在目录和构建上下文,确保路径和文件存在性都是正确的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。