moneyslow.com

Docker--使用 Dockerfile 定制nginx镜像

# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

nginx v2 3e3413f5d526 3 hours ago 109MB

ubuntu 14.04 2ff3b426bbaa 3 days ago 188MB

nginx latest 3448f27c273f 8 days ago 109MB


# cat Dockerfile

FROM nginx

RUN echo '<h1>Hello, Docker!1111111111</h1>' > /usr/share/nginx/html/index.html


# docker build -t nginx:v3 .

Sending build context to Docker daemon 2.048kB

Step 1/2 : FROM nginx

---> 3448f27c273f

Step 2/2 : RUN echo '<h1>Hello, Docker!1111111111</h1>' > /usr/share/nginx/html/index.html

---> Running in 0b2ccc2b562b

---> a3bb8892de9c

Removing intermediate container 0b2ccc2b562b

Successfully built a3bb8892de9c

Successfully tagged nginx:v3

命令解释:指定最终镜像的名称 -t nginx:v3

从命令的输出结果中,我们可以清晰的看到镜像的构建过程。在 Step 2 中,如同我们之前所说的那样,RUN 指令启动了一个容器 0b2ccc2b562b,执行了所要求的命令,并最后提交了这一层 a3bb8892de9c,随后删除了所用到的这个容器 0b2ccc2b562b。


# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

nginx v3 a3bb8892de9c 8 minutes ago 109MB

nginx v2 3e3413f5d526 4 hours ago 109MB

ubuntu 14.04 2ff3b426bbaa 3 days ago 188MB

nginx latest 3448f27c273f 8 days ago 109MB


用这个镜像启动个容器看看:

# docker run --name webserver3 -d -p 82:80 nginx:v3

8e5842cb984a9e1033df9792fecdb906a60ff5a39a7d4f4077954d8fc13fc814


# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

8e5842cb984a nginx:v3 "nginx -g 'daemon ..." 2 minutes ago Up 2 minutes 0.0.0.0:82->80/tcp webserver3

50f00085b48f nginx:v2 "nginx -g 'daemon ..." 3 hours ago Exited (0) 20 minutes ago webserver2

e033ce3a2d90 nginx "nginx -g 'daemon ..." 5 hours ago Exited (0) 20 minutes ago webserver1


# curl http://localhost:82

<h1>Hello, Docker!1111111111</h1>

Exit mobile version