打包一個Docker鏡像,讓你的好友加載開啟一個容器,並且每隔一秒輸出hello,world到指定的文件中


一、兩個腳本代碼

Dockerfile

1 FROM bash
2 COPY . /usr/herui/
3 WORKDIR /usr/herui/
4 CMD [ "sh", "hello_world.sh" ]

hello_world.sh

1 #!/bin/bash
2 while true
3 do
4 echo 'hello world!' >> /usr/herui/hello_world.log
5 sleep 1
6 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.072kB
10 Step 1/4 : FROM bash
11  ---> 906f4bf24f00
12 Step 2/4 : COPY . /usr/herui/
13  ---> Using cache
14  ---> ab38183c9bcb
15 Step 3/4 : WORKDIR /usr/herui/
16  ---> Using cache
17  ---> 24442df0587c
18 Step 4/4 : CMD sh hello_world.sh
19  ---> Using cache
20  ---> 9413de166e6f
21 Successfully built 9413de166e6f
22 Successfully tagged hello_world:1.01

這里要注意,打包指令需要在Dockerfile、hello_world.sh下進行,指令后面最后一個“.”,這叫上下文路徑。

2、查看鏡像文件列表,看看是否成功

1 [root@localhost herui]# docker images
2 REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
3 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.gz
2 [root@localhost herui]# ls -lh hello_*
3 -rw-r--r--. 1 root root   92 Aug 31 17:53 hello_world.sh
4 -rw-r--r--. 1 root root 4.4M Sep  3 01:29 hello_world.tar.gz

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM