打包一个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

 

相关内容

    暂无相关文章