Docker镜像构建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker镜像构建相关的知识,希望对你有一定的参考价值。
一、docker镜像构建
1、环境准备
环境准备
IP 主机名 操作系统 192.168.56.11 linux-node1 centos7 注意:我这里使用的是centos7,如果是使用centos5或者centos6,需要升级操作系统内核,否则Docker的许多新功能都无法使用
2、构建方式
- 手动构建 - 自动构建,(通过Dockerfile方式)
3、删除原有镜像
为了防止干扰,将以前创建的容器全部删掉。
切记 切记 切记:生产环境慎用。 否则真的会发生 从删除到跑路。。。
获取容器ID
[[email protected] ~]# docker ps -a -q #获取所有容器ID 4c37ade49a7a 1a0ec7a5b1a5 fd11374b2a58 cbc91bef8ea7 1b1113fe3b8e c4c9b4947e61 17df7e2a5667 4d5a21ea94e0 d6e7283abf0a 7ada4ff8323a c627741a7dc1 9476c8be9125
然后停掉所有的容器
[[email protected] ~]# docker kill $(docker ps -a -q) 4c37ade49a7a 1b1113fe3b8e c4c9b4947e61 17df7e2a5667 c627741a7dc1 Failed to kill container (1a0ec7a5b1a5): Error response from daemon: Cannot kill container 1a0ec7a5b1a5: Container 1a0ec7a5b1a5d931f2f887ee2fb3680d9d56b109be37044414346269f2a0bf72 is not running Failed to kill container (fd11374b2a58): Error response from daemon: Cannot kill container fd11374b2a58: Container fd11374b2a58741542c931a4eb661c6a8f3a230a13057d28a96e1cdb6c898530 is not running Failed to kill container (cbc91bef8ea7): Error response from daemon: Cannot kill container cbc91bef8ea7: Container cbc91bef8ea779668a696046c1aa905ba2dc65e64ed5b403633b3284e00ec126 is not running Failed to kill container (4d5a21ea94e0): Error response from daemon: Cannot kill container 4d5a21ea94e0: Container 4d5a21ea94e0df102198812fd899d8293198a2376dd5d952642113b76448ca65 is not running Failed to kill container (d6e7283abf0a): Error response from daemon: Cannot kill container d6e7283abf0a: Container d6e7283abf0a7e423576df645ac36c35a691153d06d63439faf72d3ce3037529 is not running Failed to kill container (7ada4ff8323a): Error response from daemon: Cannot kill container 7ada4ff8323a: Container 7ada4ff8323ad67e33fa4c52cd585a46cf9e62733505a54a7a7eb13cf33c0873 is not running Failed to kill container (9476c8be9125): Error response from daemon: Cannot kill container 9476c8be9125: Container 9476c8be91256e20da7f567f5a261f3f754b736ab6402c714180db713e3d346b is not running
删除所有容器
[[email protected] ~]# docker rm $(docker ps -a -q) 4c37ade49a7a 1a0ec7a5b1a5 fd11374b2a58 cbc91bef8ea7 1b1113fe3b8e c4c9b4947e61 17df7e2a5667 4d5a21ea94e0 d6e7283abf0a 7ada4ff8323a c627741a7dc1 9476c8be9125
二、创建镜像
1、手动创建镜像
手动创建一个nginx的镜像
[email protected] ~]# docker run --name mynginx -it centos #运行一个docker容器 [[email protected] /]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm #配置epel源 Retrieving http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm warning: /var/tmp/rpm-tmp.QKhuVj: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:epel-release-7-8 ################################# [100%] [[email protected] /]# yum install -y nginx #通过方式安装nginx 编辑nginx配置文件,让它前台运行 [[email protected] nginx]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ daemon off; #是否以守护进程的方式运行nginx,守护进程是指脱离终端并且在后头运行的进程,关闭守护进程执行的方式可以让我们方便调试nginx user nginx; worker_processes auto; error_log /var/log/nginx/error.log;
将镜像提交到本地
[[email protected] ~]# docker commit -m "my nginx" 312bc2c902da onepiece/mynginx:v1 sha256:273e37f59d87495c481ef8c23d32b33723103799aeb1194c1c632703fc70ad9f 提交到本地 -m是描述,后面跟容器ID ,onepiece是一个仓库名称
查看镜像
[[email protected] ~]# docker images #可以看到我们刚才创建的镜像 REPOSITORY TAG IMAGE ID CREATED SIZE onepiece/mynginx v1 273e37f59d87 4 minutes ago 261.9 MB
创建端口映射
[[email protected] ~]# docker run --name mynginxv1 -d -p 81:80 onepiece/mynginx:v1 nginx 3213254d75a9ab740c180cca295b427a408f8a50881cf844dfcc3f904751e53b [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3213254d75a9 onepiece/mynginx:v1 "nginx" 12 seconds ago Up 11 seconds 0.0.0.0:81->80/tcp mynginxv1 #使用之前创建的脚本登录到容器 [[email protected] ~]# ./docker_in.sh mynginxv1 [[email protected]a9 /]# cd /var/log/nginx/ [[email protected] nginx]# ls access.log error.log [[email protected] nginx]# cat access.log #查看容器的访问日志 192.168.56.1 - - [19/Sep/2016:09:02:17 +0000] "GET / HTTP/1.1" 200 3700 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/46.0.2490.86 Safari/537.36" "-" 192.168.56.1 - - [19/Sep/2016:09:02:17 +0000] "GET /nginx-logo.png HTTP/1.1" 200 368 "http://192.168.56.11:81/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36" "-"
2、基于Dockerfile创建镜像
Dockerfile:是一种文本格式
首先我们先规划一下Dockerfile的所在目录,例如/opt/dockerfile/nginx
注意:D必须是大写。当使用Dockerfile构建镜像时,它会在当前目录下去读一个名称为Dockerfile的文件。
[[email protected] /opt/dockerfile/nginx]# cat Dockerfile #base images FROM centos #除了注释的第一行,必须是FROM centos,用于设置基础镜像 #maintainer MAINTAINER JackyWang #维护信息 #Commands RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm RUN yum install -y nginx && yum clean all #RUN作为关键字,后面接要运行的命令 RUN echo "daemon off;" >> /etc/nginx/nginx.conf ADD index.html /usr/share/nginx/html/index.html #ADD是关键字, 添加一个首页文件 #index.html是放在当前目录下 EXPOSE 80 #对外开放的是80端口 CMD ["nginx"] #镜像启动时,启动的是nginx命令
写好Dockerfile后,我们开始构建
[[email protected] /opt/dockerfile/nginx]# docker build -t mynginx:v2 . Sending build context to Docker daemon 3.072 kB Step 1 : FROM centos ---> 980e0e4c79ec Step 2 : MAINTAINER JackyWang ---> Using cache ---> 33f84558f81d Step 3 : RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm ---> Using cache ---> 21a39298e95b Step 4 : RUN yum install -y nginx && yum clean all ---> Using cache ---> c16994d2c471 Step 5 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf ---> Using cache ---> 39308efba34d Step 6 : ADD index.html /usr/share/nginx/html/index.html ---> Using cache ---> 86464cc2b1d3 Step 7 : EXPOSE 80 ---> Using cache ---> 89a262db6b6c Step 8 : CMD nginx ---> Using cache ---> f735be220129 Successfully built f735be220129 从记录信息中我们可以看到docker每一步都做了什么
构建后运行容器,可以正常运行。
[[email protected] /opt/dockerfile/nginx]# docker run --name mynginxv2 -d -p 82:80 mynginx:v2 6ec8783771d6aa06ec0c8628c9fa643c47efcca8b698ab5f6a4dade5c3cb335d
本文出自 “JackyWang” 博客,请务必保留此出处http://jackyxin.blog.51cto.com/1976631/1856839
以上是关于Docker镜像构建的主要内容,如果未能解决你的问题,请参考以下文章
构建Docker镜像两种方式的比较-Dockerfile方式和S2I方式
Docker删除报错:Error response from daemon: conflict: unable to delete 08b152afcfae (must be forced)(代码片段