编写Dockerfile构建你自己的镜像
2017-08-26 00:00
751 查看
1. 编写第一个Dockerfile
mkdir mynginx && cd mynginx touch Dockerfile
Dockerfile中的内容为:
FROM nginx RUN echo '<h1>Hello Docker!</h1>' > /usr/share/nginx/html/index.html
2. 构建docker镜像
docker build -t nginx:v3 .
执行结果的截图:
[root@localhost mynginx]# docker build -t nginx:v3 . Sending build context to Docker daemon 2.048kB Step 1/2 : FROM nginx latest: Pulling from library/nginx 94ed0c431eb5: Pull complete 9406c100a1c3: Pull complete aa74daafd50c: Pull complete Digest: sha256:788fa27763db6d69ad3444e8ba72f947df9e7e163bad7c1f5614f8fd27a311c3 Status: Downloaded newer image for nginx:latest ---> b8efb18f159b Step 2/2 : RUN echo '<h1>Hello Docker!</h1>' > /usr/share/nginx/html/index.html ---> Running in a3e91bf5741d ---> d19100668ad5 Removing intermediate container a3e91bf5741d Successfully built d19100668ad5 Successfully tagged nginx:v3 [root@localhost mynginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v3 d19100668ad5 52 seconds ago 107MB nginx latest b8efb18f159b 4 weeks ago 107MB registry latest 751f286bc25e 5 weeks ago 33.2MB hello-world latest 1815c82652c0 2 months ago 1.84kB [root@localhost mynginx]#
3. 运行新版docker镜像
docker run --name webserver -d -p 80:80 nginx:v3
测试查看是否运行成功:
[root@localhost mynginx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f0182292a1a nginx:v3 "nginx -g 'daemon ..." 18 minutes ago Up 18 minutes 0.0.0.0:80->80/tcp webserver [root@localhost mynginx]# curl "http://127.0.0.1" <h1>Hello Docker!</h1> [root@localhost mynginx]#
4. 停止和开始该镜像
停止该容器docker stop webserver
开始该容器
docker start webserver
重启该容器
docker restart webserver
5. 重新构建所有的脚本
docker stop webserver docker rm webserver docker rmi $(docker images -q nginx:v3) docker build -t nginx:v3 . docker run --name webserver -d -p 80:80 nginx:v3
6. 增加健康检查
这个时候的Dockerfile变成了:FROM nginx ENV VERSION=1.0 COPY ./sources.list /tmp/sources.list RUN mv /tmp/sources.list /etc/apt/sources.list && apt-get update \ && apt-get install -y --allow-unauthenticated curl \ && rm -rf /var/lib/apt/lists/* \ && echo '<h1>Hello Docker!</h1>' > /usr/share/nginx/html/index.html COPY ./test.html /usr/share/nginx/html/test.html HEALTHCHECK --interval=5s --timeout=3s \ CMD curl -fs http://localhost/ || exit 1
其中的sources.list的内容为:
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties deb http://archive.canonical.com/ubuntu xenial partner deb-src http://archive.canonical.com/ubuntu xenial partner deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
采用docker ps可以看到该状态了
[root@localhost mynginx]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 412feea16569 nginx:v3 "nginx -g 'daemon ..." 5 minutes ago Up 5 minutes (healthy) 0.0.0.0:80->80/tcp webserver [root@localhost mynginx]#
采用docker inspect来查看消息
[root@localhost mynginx]# docker inspect --format '{{json .State.Health}}' webserver | python -m json.tool { "FailingStreak": 0, "Log": [ { "End": "2017-08-27T16:14:52.557653397+08:00", "ExitCode": 0, "Output": "<h1>Hello Docker!</h1>\n", "Start": "2017-08-27T16:14:52.418027597+08:00" } ], "Status": "healthy" } [root@localhost mynginx]#
7. 打包和导入
打包保存镜像 【docker save nginx:v3 | gzip > nginx_v3.tar.gz】[root@localhost mynginx]# docker save nginx:v3 | gzip > nginx_v3.tar.gz [root@localhost mynginx]# ls Dockerfile nginx_v3.tar.gz rebuild_all.sh sources.list test.html [root@localhost mynginx]#
导入镜像【docker load -i nginx_v3.tar.gz】
[root@localhost mynginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest b8efb18f159b 4 weeks ago 107MB hello-world latest 1815c82652c0 2 months ago 1.84kB [root@localhost mynginx]# docker load -i nginx_v3.tar.gz 539ad9b5c133: Loading layer [==================================================>] 3.584kB/3.584kB 8a4c074a807e: Loading layer [==================================================>] 17.3MB/17.3MB ebd1081b38e1: Loading layer [==================================================>] 4.096kB/4.096kB Loaded image: nginx:v3 [root@localhost mynginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx v3 9d6d322db4f2 16 minutes ago 124MB nginx latest b8efb18f159b 4 weeks ago 107MB hello-world latest 1815c82652c0 2 months ago 1.84kB [root@localhost mynginx]#
相关文章推荐
- 利用Dockerfile构建一个基于centos 7,包括java 8, tomcat 7,php ,mysql+mycat的镜像
- 基于dockerfile构建镜像
- 【Docker容器的数据管理】- 使用Dockerfile构建包含数据卷的镜像
- 使用dockerfile构建nginx镜像
- Docker--docker使用及自定义Dockerfile构建镜像
- 使用Dockerfile构建镜像并push到私有仓库
- Dockerfile:nginx镜像编写
- 如何使用Dockerfile构建镜像
- Docker学习笔记-- 如何使用Dockerfile构建镜像
- 使用Dockerfile构建镜像
- Docker--使用Dockerfile构建新镜像--RHEL7.2
- 群晖Docker通过Dockerfile构建镜像 部署Tomcat
- docker学习笔记(六)使用Dockerfile构建镜像
- Docker中镜像构建文件Dockerfile与相关命令的详细介绍
- Docker使用Dockerfile构建镜像
- 【docker】基于Dockerfile构建mysqld服务镜像
- Docker学习笔记(3)-- 如何使用Dockerfile构建镜像
- Docker教程:镜像构建和自动镜像构建dockerfile
- Dockerfile构建Docker镜像