Docker笔记三 Docker镜像制作
1.Docker镜像制作方法:
docker commit 保存当前container的状态到镜像,生成image。
docker build 利用dockerfile自动化生成image。
2.制作方法 docker commit方式
#启动镜像名称为centos的一个容器(container) [root@frog ~]#docker run -it centos /bin/bash #在容器内安装httpd服务 [root@95a278b60b0f ~]#yum install httpd #退出容器 [root@95a278b60b0f ~]#exit #查看当前容器的ID [root@frog ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95a278b60b0f centos "/bin/bash" 2 minutes ago Exited (0) 53 seconds ago #基于当前运行的容器(container),制作新镜像 httpd [root@frog ~]#docker commit 95a278b60b0f centos:httpd [root@frog ~]# docker commit 95a278b60b0f centos:helloworld sha256:30e20107209fd55cef87cdaa4b71a3ae4f64f7f051f88d523ea386aba24398f6 [root@frog ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos httpd 30e20107209f 6 seconds ago 193MB centos latest 3bee3060bfc8 8 days ago 193MB 基于新创建的镜像helloworld 启动一个容器 [root@frog ~]#docker run -it centos:httpd /bin/bash [root@4f50f361c028 /]#systemctl start httpd 完成制作并启动httpd进行了验证。
3.制作方法 docker build方式
#创建工作目录 [root@frog /]# mkdir docker-build [root@frog /]# cd docker-build/ [root@frog docker-build]# ls [root@frog docker-build]# touch Dockerfile #编辑Dockerfile文件 [root@frog docker-build]# vim Dockerfile FROM centos # base container MAINTAINER frog <frgtwo@163.com> #owner RUN yum -y install httpd #belong shell command ADD httpStart.sh /usr/local/bin/httpStart.sh #copy local file to new image location right: 755 uid:0 gid:0 ADD index.html /var/www/html/index.html #当前目录创建 httpStart.sh、index.html文件 #说明:/usr/sbin/httpd DFOREGROUND相当于 systemctl start httpd [root@frog docker-build]# ls Dockerfile [root@frog docker-build]# echo "/usr/sbin/httpd DFOREGROUND" > httpdStart.sh [root@frog docker-build]# echo "hello world" > index.html [root@frog docker-build]# chmod a+x httpStart.sh [root@frog docker-build]# ls Dockerfile httpStart.sh index.html #使用build 命令创建centos:httpd 镜像 [root@frog docker-build]# docker build -t centos:httpd . #这里的‘.’指Dockerfile文件路径 #检查新创建的镜像文件centos:httpd [root@frog docker-build]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos httpd 3062c6b1a8e8 3 minutes ago 318MB centos index b11dfe564274 About an hour ago 193MB centos helloworld 30e20107209f About an hour ago 193MB centos latest 3bee3060bfc8 9 days ago 193MB
本文出自 “Frog的技术归档” 博客,请务必保留此出处http://frogtwo.blog.51cto.com/3805708/1936910
原文地址:http://frogtwo.blog.51cto.com/3805708/1936910