Docker 实战—使用 Dockerfile 构建镜像

GitHub Page:http://blog.cloudli.top/posts/Docker实战-使用-Dockerfile-构建镜像/html

Dockerfile 指令详解请访问:http://www.javashuo.com/article/p-ekfnhhto-dx.htmllinux

使用 Alpine Linux 做为基础镜像

Alpine 是一个很是轻量的 Linux 镜像,他只有大约 5MB 的大小,基于它构建镜像,能够大大减小镜像的体积。nginx

Alpine 的 Docker Hub 页面:https://hub.docker.com/_/alpinedocker

docker pull alpine

Alpine 使用 apk 命令来安装软件包,支持的软件包列表能够在官网查看:https://pkgs.alpinelinux.org/packagesshell

这里以安装 Nginx 为例,学习镜像的构建。另外 Nginx 自己有官方镜像,pull 便可。缓存

构建 Nginx 镜像

编写 Dockerfile

FROM alpine

RUN apk update \
    # 安装 nginx
    apk add --no-cache nginx \
    mkdir /run/nginx && \
    # 清除缓存
    rm -rf /tmp/* /var/cache/apk/*
    
# 添加容器启动命令,启动 nginx,之前台方式运行
CMD [ "nginx", "-g", "daemon off;" ]

这里有一个坑点,必须建立 /run/nginx 目录,否则会报错。post

构建镜像

使用 docker build 命令构建:学习

docker build -t nginx-alpine .

在 Dockerfile 目录下执行以上命令便可构建镜像。-t 参数指定了镜像名称为 nginx-alpine,最后的 . 表示构建上下文(. 表示当前目录).ui

在使用 COPY 指令复制文件时,指令中的源路径是相对于构建上下文的(若是指定上下文为 /home,那么至关于全部的源路径前面都加上了 /home/)。code

若是你的 Dockerfile 文件名不是 “Dockerfile”,可使用 -f 参数指定。

千万不要将 Dockerfile 放在根目录下构建,假如你将 Dockerfile 放在一个存放大量视频目录下,而且构建上下文为当前目录,那么镜像将会很是大(视频都被打包进去了)。最佳作法是将 Dockerfile 和须要用到的文件放在一个单独的目录下。

运行容器

使用构建的镜像运行容器:

docker run --name my-nginx -p 80:80 -d nginx-apline
  • --name 指定容器的名称,能够省略(后续只能经过容器 id 来操做);
  • -p 映射端口,宿主端口 -> 容器端口;
  • -d 后台运行。

运行后访问 http://localhost/,会出现一个 nginx 的 404 页面,说明已经运行成功了,由于这里安装的 Nginx 并无默认页面,/etc/nginx/conf.d/default.conf 中的内容:

# This is a default site configuration which will simply return 404, preventing
# chance access to any other virtualhost.

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # Everything is a 404
        location / {
                return 404;
        }
}

使用构建的 Nginx 镜像运行一个静态页面

在一个空目录下建立 Nginx 配置文件:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www;
        
        location / {
                index index.html;
        }
}

编写一个静态页面:

<!DOCTYPE html>
<html>
    <head>
        <title>Index</title>
    </head>
    <body>
        <h1>Hello, Docker!</h1>
    </body>
</html>

使用以前构建的镜像构建一个新的镜像:

FROM nginx-alpine
# 拷贝配置文件,覆盖默认的
COPY default.conf /etc/nginx/conf.d/
# 拷贝静态页面
COPY index.html /var/www

构建镜像、运行容器:

docker build -t site .
docker run --name my-site -p 80:80 -d site

如今访问 http://localhost/,就能够看到 Hello, Docker!

相关文章
相关标签/搜索