打包一個Docker鏡像,讓你的好友載入開啟一個容器,並且每隔一秒輸出hello,world到指定的檔案中,
一、兩個指令碼代碼
Dockerfile
1 FROM bash2 COPY . /usr/herui/3 WORKDIR /usr/herui/4 CMD [ "sh", "hello_world.sh" ]
hello_world.sh
1 #!/bin/bash2 while true3 do4 echo 'hello world!' >> /usr/herui/hello_world.log5 sleep 16 done
二、打包成鏡像
1、
1 [root@localhost herui]# docker build -t hello_world:1.01 2 "docker build" requires exactly 1 argument(s). 3 See 'docker build --help'. 4 5 Usage: docker build [OPTIONS] PATH | URL | - 6 7 Build an image from a Dockerfile 8 [root@localhost herui]# docker build -t hello_world:1.01 . 9 Sending build context to Docker daemon 3.072kB10 Step 1/4 : FROM bash11 ---> 906f4bf24f0012 Step 2/4 : COPY . /usr/herui/13 ---> Using cache14 ---> ab38183c9bcb15 Step 3/4 : WORKDIR /usr/herui/16 ---> Using cache17 ---> 24442df0587c18 Step 4/4 : CMD sh hello_world.sh19 ---> Using cache20 ---> 9413de166e6f21 Successfully built 9413de166e6f22 Successfully tagged hello_world:1.01
這裡要注意,打包指令需要在Dockerfile、hello_world.sh下進行,指令後面最後一個“.”,這叫上下文路徑。
2、查看鏡像檔案列表,看看是否成功
1 [root@localhost herui]# docker images2 REPOSITORY TAG IMAGE ID CREATED SIZE3 hello_world 1.01 9413de166e6f 2 days ago 12.1MB
三、儲存成tar.gz格式並檢查目前的目錄下包是否存在
1 [root@localhost herui# docker save hello_world:1.01 | gzip > hello_world.tar.gz2 [root@localhost herui]# ls -lh hello_*3 -rw-r--r--. 1 root root 92 Aug 31 17:53 hello_world.sh4 -rw-r--r--. 1 root root 4.4M Sep 3 01:29 hello_world.tar.gz