Docker 学习之旅(三):创建基础镜像

基础镜像,官方说法是 base image,官方解释为“ An image that has no parent is a base image. ”也就是说,没有父镜像的镜像就是基础镜像,我们要做的就是创建一个基础镜像。

文档链接:https://docs.docker.com/engine/userguide/eng-image/baseimages/

使用 tar 创建一个完整镜像

执行 $ docker run raring cat /etc/lsb-release 命令时报错且解决无果,可能 Debian 的发行版和 Ubuntu还是存在些许差异。

1
2
3
4
$ docker run raring cat /etc/lsb-release
container_linux.go:247: starting container process caused "exec: \"cat\": executable file not found in $PATH"
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"cat\": executable file not found in $PATH".
ERRO[0000] error getting events from daemon: net/http: request canceled

使用 scratch 创建一个简单的基础镜像

scratch 在 Docker 的库中出现时,你不能 pull、run 或者在其他镜像标记这个名字,但是你可以在你的 Dockerfile 文件中引用它。

例如,使用 scratch 创建一个最小化的容器:

1
2
3
FROM scratch
ADD hello /
CMD ["/hello"]

然后你可以在当前目录使用 docker build --tag hello . 来构建容器。在这里需要 hello 文件,你可以从 Docker Github 的 示例源代码 中下载并编译,过程如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ vi Dockerfile
$ wget https://raw.githubusercontent.com/docker-library/hello-world/master/hello.c
$ gcc -o hello -static -nostartfiles hello.c
$ docker build --tag hello .
$ docker run hello

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/