Docker 使用 Dockerfile 构建自己的镜像

可以使用Dockerfile的配置文件方式进行构建自己的镜像
下面利用docker构建一个Caddy web服务器

构建脚本

Dockerfile有自己的命令,下面使用了一些比较常用的命令,更多的Dockerfile命令,可查看以下博文:
Dockerfile命令详解(超全版本)

Dockerfile文件内容

# 基础镜像Ubuntu
FROM ubuntu:latest
# 镜像作者信息
MAINTAINER zhoutao "zhoutao825638@vip.qq.com"
#RUN 命令执行脚本
RUN mkdir -p /opt/soft/caddy
RUN mkdir -p /usr/www/html
RUN echo "<h1>Hello,World!</h1>" >> /usr/www/html/index.html
# COPY 执行本地拷贝命令
COPY ./caddy /opt/soft/caddy/
COPY ./Caddyfile /opt/soft/caddy/
# 启动执行命令
CMD  ["/opt/soft/caddy/caddy", "-conf","/opt/soft/caddy/Caddyfile"]
# 暴露端口
EXPOSE 80

Caddyfile文件内容

其中Caddyfile配置文件内容如下:

:80 {
  gzip
  root /usr/www/html
}

caddy文件可从caddy官方下载

构建命令

首先确认下文件结构

$ tree
.
├── caddy
├── Caddyfile
└── Dockerfile
  • caddy 是Caddy的运行文件
  • Caddyfile 是caddy的配置文件
  • Dockerfile是构建docker的配置文件

然后开始构建image(注意切换到和Dockerfile同级目录)

docker build -t caddy_image:0.1 .
  • 构建结果

第一次构建需要拉取ubuntu基础镜像,然后才开始构建

Sending build context to Docker daemon  20.12MB
Step 1/9 : FROM ubuntu:latest
latest: Pulling from library/ubuntu
124c757242f8: Pull complete 
9d866f8bde2a: Pull complete 
fa3f2f277e67: Pull complete 
398d32b153e8: Pull complete 
afde35469481: Pull complete 
Digest: sha256:de774a3145f7ca4f0bd144c7d4ffb2931e06634f11529653b23eba85aef8e378
Status: Downloaded newer image for ubuntu:latest
 ---> cd6d8154f1e1
Step 2/9 : MAINTAINER zhoutao "zhoutao825638@vip.qq.com"
 ---> Running in 976ccb6d9fd0
Removing intermediate container 976ccb6d9fd0
 ---> c41619ec0682
Step 3/9 : RUN mkdir -p /opt/soft/caddy
 ---> Running in fea53d5e3b98
Removing intermediate container fea53d5e3b98
 ---> 2b84b490ac72
Step 4/9 : RUN mkdir -p /usr/www/html
 ---> Running in 2b6d3ef30fb2
Removing intermediate container 2b6d3ef30fb2
 ---> eef4e4e0584a
Step 5/9 : RUN echo "<h1>Hello,World!</h1>" >> /usr/www/html/index.html
 ---> Running in 9784c9f94636
Removing intermediate container 9784c9f94636
 ---> e2b8568e77c5
Step 6/9 : COPY ./caddy /opt/soft/caddy/
 ---> 7f5e675846bb
Step 7/9 : COPY ./Caddyfile /opt/soft/caddy/
 ---> e8fbed1ae131
Step 8/9 : CMD  ["/opt/soft/caddy/caddy", "-conf","/opt/soft/caddy/Caddyfile"]
 ---> Running in a71772181530
Removing intermediate container a71772181530
 ---> 66b878cddbf6
Step 9/9 : EXPOSE 80
 ---> Running in 39741df7c755
Removing intermediate container 39741df7c755
 ---> 51c30425c00e
Successfully built 51c30425c00e
Successfully tagged caddy_image:0.1

运行容器

# docker运行容器
$ docker run -p 9090:80 -d --name caddy_9090 caddy_image:0.1
575c7966b1021de5fe95212cb09723e3256ad1153a8cd04a836ab171d40860ed

# 查看docker所有的容器
$ docker ps -a                                              
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                                     NAMES
575c7966b102        caddy_image:0.1                          "/opt/soft/caddy/cad…"   5 seconds ago       Up 4 seconds        0.0.0.0:9090->80/tcp                      caddy_9090

访问测试

可以看到,当我访问80端口的时候,他会到/usr/www/html目录下寻找数据,而index.html正是我们在Dockerfile文件中的RUN命令写入的RUN echo "<h1>Hello,World!</h1>" >> /usr/www/html/index.html

$ curl -X GET http://localhost:9090/index.html
<h1>Hello,World!</h1>
posted @ 2019-02-15 10:45  燕归来兮  阅读(859)  评论(0编辑  收藏  举报