⑦. ADD 、COPY 复制文件
- ①. ADD和COPY的功能是一样的,ADD多了自动下载远程文件和解压的功能
# 这是我的第一个dockerfile镜像 FROM alpine # 把上下文context指定的内容复制到镜像中,如果是压缩包,自动解压,如果是远程文件,自动下载 # 把当前内容复制到alpine小系统里面 ADD https://download.redis.io/releases/redis-6.2.1.tar.gz /dest/ RUN ls -l # RUN 指令上下并没有上下文关系 RUN cd /dest # 当前还是列举的跟目录 RUN ls -l RUN cd /dest && ls -l
[root@i-id8g0yu9 ~]# docker build --no-cache -t dockerfileadd:v1 -f dockerfileADD . Sending build context to Docker daemon 27.65kB Step 1/6 : FROM alpine ---> 6dbb9cc54074 Step 2/6 : ADD https://download.redis.io/releases/redis-6.2.1.tar.gz /dest/ Downloading [==================================================>] 2.438MB/2.438MB ---> 80aaa61dc520 Step 3/6 : RUN ls -l ---> Running in 1acdb19a9af4 total 8 drwxr-xr-x 2 root root 4096 Apr 14 10:25 bin drwxr-xr-x 2 root root 32 Apr 18 12:53 dest drwxr-xr-x 5 root root 340 Apr 18 12:53 dev drwxr-xr-x 1 root root 66 Apr 18 12:53 etc drwxr-xr-x 2 root root 6 Apr 14 10:25 home drwxr-xr-x 7 root root 247 Apr 14 10:25 lib drwxr-xr-x 5 root root 44 Apr 14 10:25 media drwxr-xr-x 2 root root 6 Apr 14 10:25 mnt drwxr-xr-x 2 root root 6 Apr 14 10:25 opt dr-xr-xr-x 114 root root 0 Apr 18 12:53 proc drwx------ 2 root root 6 Apr 14 10:25 root drwxr-xr-x 2 root root 6 Apr 14 10:25 run drwxr-xr-x 2 root root 4096 Apr 14 10:25 sbin drwxr-xr-x 2 root root 6 Apr 14 10:25 srv dr-xr-xr-x 13 root root 0 Apr 18 12:53 sys drwxrwxrwt 2 root root 6 Apr 14 10:25 tmp drwxr-xr-x 7 root root 66 Apr 14 10:25 usr drwxr-xr-x 12 root root 137 Apr 14 10:25 var Removing intermediate container 1acdb19a9af4 ---> aff230b986d5 Step 4/6 : RUN cd /dest ---> Running in e154c1af02e8 Removing intermediate container e154c1af02e8 ---> ef4756ff7262 Step 5/6 : RUN ls -l ---> Running in 32c3f351c176 total 8 drwxr-xr-x 2 root root 4096 Apr 14 10:25 bin drwxr-xr-x 2 root root 32 Apr 18 12:53 dest drwxr-xr-x 5 root root 340 Apr 18 12:53 dev drwxr-xr-x 1 root root 66 Apr 18 12:53 etc drwxr-xr-x 2 root root 6 Apr 14 10:25 home drwxr-xr-x 7 root root 247 Apr 14 10:25 lib drwxr-xr-x 5 root root 44 Apr 14 10:25 media drwxr-xr-x 2 root root 6 Apr 14 10:25 mnt drwxr-xr-x 2 root root 6 Apr 14 10:25 opt dr-xr-xr-x 113 root root 0 Apr 18 12:53 proc drwx------ 2 root root 6 Apr 14 10:25 root drwxr-xr-x 2 root root 6 Apr 14 10:25 run drwxr-xr-x 2 root root 4096 Apr 14 10:25 sbin drwxr-xr-x 2 root root 6 Apr 14 10:25 srv dr-xr-xr-x 13 root root 0 Apr 18 12:53 sys drwxrwxrwt 2 root root 6 Apr 14 10:25 tmp drwxr-xr-x 7 root root 66 Apr 14 10:25 usr drwxr-xr-x 12 root root 137 Apr 14 10:25 var Removing intermediate container 32c3f351c176 ---> c45f1cca41d7 Step 6/6 : RUN cd /dest && ls -l ---> Running in c82e471ef25e total 2384 -rw------- 1 root root 2438367 Mar 2 06:35 redis-6.2.1.tar.gz Removing intermediate container c82e471ef25e ---> 5ac189b07621 Successfully built 5ac189b07621 Successfully tagged dockerfileadd:v1 [root@i-id8g0yu9 ~]#
②. 这里能使用docker build -t demo:test .的方式构建,是由于这个Dockerfile的文件在当前目录下,如果不在,那么需要将. 变成指定的文件夹(.代表的是当前上下文坏境)
# 这是我的第一个dockerfile镜像 FROM alpine # 把上下文context指定的内容复制到镜像中,如果是压缩包,自动解压,如果是远程文件,自动下载 # 把当前内容复制到alpine小系统里面 ADD https://download.redis.io/releases/redis-6.2.1.tar.gz /dest/ # 本地linux系统的内容文件添加进去[宿主机--镜像内] # docker build -t demo:test .: .代表当前dockerfile指定的上下文坏境 # 通过执行我们可以看到,redis进行了自动解压的处理 ADD *.tar.gz /app/ # RUN 指令上下并没有上下文关系 RUN cd /dest && ls -l RUN cd /app && ls -l
[root@i-id8g0yu9 ~]# docker build --no-cache -t dockerfileadd2:v1 -f dockerfileADD2 . Sending build context to Docker daemon 2.468MB Step 1/5 : FROM alpine ---> 6dbb9cc54074 Step 2/5 : ADD https://download.redis.io/releases/redis-6.2.1.tar.gz /dest/ Downloading [==================================================>] 2.438MB/2.438MB ---> b7598fcec68b Step 3/5 : ADD *.tar.gz /app/ ---> 9b222d9290b5 Step 4/5 : RUN cd /dest && ls -l ---> Running in 4a32a2a9866b total 2384 -rw------- 1 root root 2438367 Mar 2 06:35 redis-6.2.1.tar.gz Removing intermediate container 4a32a2a9866b ---> f72d59644485 Step 5/5 : RUN cd /app && ls -l ---> Running in 48fe6f35756f total 4 drwxrwxr-x 7 root root 4096 Mar 2 06:14 redis-6.2.1 Removing intermediate container 48fe6f35756f ---> 762a609e68e9 Successfully built 762a609e68e9 Successfully tagged dockerfileadd2:v1
⑧. WORKDIR 配置工作目录
①. 根目录,WORKDIR指令可在Dockerfile中多次使用。 如果提供了相对路径,则它将相对于上一个WORK DIR指令的路径(如下面的/app/abc是根目录)
FROM alpine RUN pwd && ls -l # 为以下所有命令运行指令了基础目录 # /app/abc WORKDIR /app # 当我们使用docker exec 进入容器控制台,会发现根目录是 /app/abc WORKDIR abc # 复制到当前目录下 COPY *.txt ./ RUN pwd && ls -l CMD ping baidu.com
[root@i-id8g0yu9 ~]# docker build --no-cache -t dockerfileworkdir:v1 -f dockerfileWORKDIR . Sending build context to Docker daemon 2.472MB Step 1/7 : FROM alpine ---> 6dbb9cc54074 Step 2/7 : RUN pwd && ls -l ---> Running in 7ef554287c6f / total 8 drwxr-xr-x 2 root root 4096 Apr 14 10:25 bin drwxr-xr-x 5 root root 340 Apr 18 13:54 dev drwxr-xr-x 1 root root 66 Apr 18 13:54 etc drwxr-xr-x 2 root root 6 Apr 14 10:25 home drwxr-xr-x 7 root root 247 Apr 14 10:25 lib drwxr-xr-x 5 root root 44 Apr 14 10:25 media drwxr-xr-x 2 root root 6 Apr 14 10:25 mnt drwxr-xr-x 2 root root 6 Apr 14 10:25 opt dr-xr-xr-x 111 root root 0 Apr 18 13:54 proc drwx------ 2 root root 6 Apr 14 10:25 root drwxr-xr-x 2 root root 6 Apr 14 10:25 run drwxr-xr-x 2 root root 4096 Apr 14 10:25 sbin drwxr-xr-x 2 root root 6 Apr 14 10:25 srv dr-xr-xr-x 13 root root 0 Apr 18 12:58 sys drwxrwxrwt 2 root root 6 Apr 14 10:25 tmp drwxr-xr-x 7 root root 66 Apr 14 10:25 usr drwxr-xr-x 12 root root 137 Apr 14 10:25 var Removing intermediate container 7ef554287c6f ---> 27e092e104fb Step 3/7 : WORKDIR /app ---> Running in ee015d4b2a18 Removing intermediate container ee015d4b2a18 ---> 4419d4f77345 Step 4/7 : WORKDIR abc ---> Running in bc8172263488 Removing intermediate container bc8172263488 ---> e29383588c07 Step 5/7 : COPY *.txt ./ ---> 715b2d4db2d9 Step 6/7 : RUN pwd && ls -l ---> Running in 5e5c8d7ab9b8 /app/abc total 4 -rw-r--r-- 1 root root 5 Apr 18 13:46 a.txt Removing intermediate container 5e5c8d7ab9b8 ---> d196ffbdf968 Step 7/7 : CMD ping baidu.com ---> Running in e0b6b78b1eec Removing intermediate container e0b6b78b1eec ---> a1be08fec3d1 Successfully built a1be08fec3d1 Successfully tagged dockerfileworkdir:v1 [root@i-id8g0yu9 ~]# docker run -d --name myworkdir dockerfileworkdir:v1 b4d8744b6a920fa5eea4b2388ede0e04489905a8449d0c2e40ef8d3d298cfecb [root@i-id8g0yu9 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4d8744b6a92 dockerfileworkdir:v1 "/bin/sh -c 'ping ba…" 3 seconds ago Up 3 seconds myworkdir [root@i-id8g0yu9 ~]# docker exec -it b4d8744b6a92 /bin/sh /app/abc #