dockerfile常用命令(ADD 特别说明)
-
FROM
:- 用途:指定基础镜像,构建新镜像的起点。
- 用法:
FROM <镜像名称>:<标签>
或FROM <镜像名称>@<摘要>
- 用法示例:
FROM ubuntu:20.04
-
MAINTAINER
(已过时,建议使用 LABEL):- 用途:设置镜像的作者信息。
- 用法:
MAINTAINER <作者信息>
- 用法示例:
MAINTAINER John Doe <johndoe@example.com>
-
ADD
:- 用途:将文件或目录从构建上下文(通常是 Dockerfile 所在目录)复制到镜像中。
- 用法:
ADD <源路径> <目标路径>
- 用法示例:
ADD app.jar /app/
-
CMD
:- 用途:定义容器启动时要执行的默认命令。
- 用法:
CMD ["可执行文件", "参数1", "参数2"]
或CMD 命令 参数1 参数2
- 用法示例:
CMD ["python", "app.py"]
-
ENV
:- 用途:设置环境变量。
- 用法:
ENV <变量名> <值>
- 用法示例:
ENV DATABASE_URL=mysql://user:password@localhost/db
-
RUN
:- 用途:在构建过程中执行命令,用于安装软件、配置环境等。
- 用法:
RUN <命令>
- 用法示例:
RUN apt-get update && apt-get install -y python
-
ENTRYPOINT
:- 用途:设置容器启动时要执行的入口命令。
- 用法:
ENTRYPOINT ["可执行文件", "参数1", "参数2"]
或ENTRYPOINT 命令 参数1 参数2
- 用法示例:
ENTRYPOINT ["java", "-jar", "app.jar"]
-
WORKDIR
:- 用途:设置容器内的工作目录。
- 用法:
WORKDIR <目录路径>
- 用法示例:
WORKDIR /app
-
EXPOSE
:- 用途:声明容器运行时监听的网络端口。
- 用法:
EXPOSE <端口号>
- 用法示例:
EXPOSE 80
-
VOLUME
:- 用途:声明容器中的数据卷,用于持久化存储。
- 用法:
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所在目录和构建上下文,确保路径和文件存在性都是正确的。