Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerfile整理 #32

Open
lqshow opened this issue Jun 10, 2018 · 0 comments
Open

Dockerfile整理 #32

lqshow opened this issue Jun 10, 2018 · 0 comments

Comments

@lqshow
Copy link
Owner

lqshow commented Jun 10, 2018

构建

docker构建镜像的2个条件

  1. Dockerfile文件
  2. 镜像构建的上下文

构建命令

docker build .
  1. Docker是严格按照顺序运行指令的,并且每个指令都会创建一个新的镜像层并对镜像进行提交
  2. Dockerfile一般位于构建上下文的根目录下,也可以通过-f指定该文件的位置
  3. docker build命令最后的.,表示当前目录,是指定上下文路径
  4. Docker会在构建镜像时将构建上下文和该上下文中的文件和目录上传到Docker守护进程

Comand Options

flag desc
-t 打标签
-f 指定Dockerfile路径。若无此参数,从当前目录找到Dockerfiler
--no-cache 不实用缓存
--build-arg 搭配ARG使用

.dockerignore

用来忽略上下文中构建用不到的目录文件,语法同.gitignore

相关问题

Dockerfile中的CMDENTRYPOINT有什么区别

  1. CMDENTRYPOINT指令都定义了在运行容器时执行的命令
  2. ENTRYPOINT指定一个在容器启动时总是执行的命令。
  3. CMD当容器启动时运行的命令或者指定了ENTRYPOINT的参数。
FROM debian:wheezy
ENTRYPOINT ["/bin/ping"]
CMD ["localhost", "-c", "2"]

以上Dockerfile,将接受CMD作为ENTRYPOINT命令的缺省参数

不带参数执行结果

➜  /Users/linqiong/workspace/docker/example docker run -it --rm example
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.066 ms
64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.137 ms

--- localhost ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1023ms
rtt min/avg/max/mdev = 0.066/0.101/0.137/0.036 ms

带参数(覆盖CMD)执行结果

➜  /Users/linqiong/workspace/docker/example docker run -it --rm example www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38: icmp_req=1 ttl=37 time=34.7 ms
64 bytes from 14.215.177.38: icmp_req=2 ttl=37 time=35.3 ms
64 bytes from 14.215.177.38: icmp_req=3 ttl=37 time=34.2 ms
64 bytes from 14.215.177.38: icmp_req=4 ttl=37 time=34.1 ms
64 bytes from 14.215.177.38: icmp_req=5 ttl=37 time=32.8 ms
^C
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4012ms
rtt min/avg/max/mdev = 32.812/34.260/35.353/0.847 ms

参考:

Dockerfile中的COPYADD命令有什么区别

  1. COPY只支持将本地文件复制到容器中
  2. ADD具有额外的功能(如仅限本地的tar提取和远程URL支持)

如果仅仅用于复制文件,请选择 COPY,简单更有效。
如果需要提取存档,需要使用 ADD。

# 比如将一个local 的包解压,通过 COPY 命令,执行如下,需要三步
# 这里用 COPY,其实是个错误的选择
RUN mkdir -p /usr/java
COPY resource/jdk1.8.0_77.tgz /usr/java/jdk1.8.0_77.tgz
RUN tar -zxvf /usr/java/jdk1.8.0_77.tgz -C /usr/java

# 用 ADD 命令,一步到位
ADD resource/jdk1.8.0_77.tgz /usr/java
@lqshow lqshow added the DevOps label Aug 4, 2018
@lqshow lqshow self-assigned this Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant