Docker笔记三 Docker镜像制作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker笔记三 Docker镜像制作相关的知识,希望对你有一定的参考价值。

Docker笔记三 Docker镜像制作


1.Docker镜像制作方法:

  • docker commit 保存当前container的状态到镜像,生成image。

  • docker build 利用dockerfile自动化生成image。


2.制作方法 docker commit方式

#启动镜像名称为centos的一个容器(container) 

[[email protected] ~]#docker run -it centos /bin/bash

#在容器内安装httpd服务

[[email protected] ~]#yum install httpd

#退出容器

[[email protected] ~]#exit

#查看当前容器的ID

[[email protected] ~]# 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

[[email protected] ~]#docker commit 95a278b60b0f  centos:httpd  

[[email protected] ~]# docker commit 95a278b60b0f centos:helloworld
    sha256:30e20107209fd55cef87cdaa4b71a3ae4f64f7f051f88d523ea386aba24398f6    
[[email protected] ~]#docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              httpd                   30e20107209f        6 seconds ago       193MB
    centos              latest              3bee3060bfc8        8 days ago          193MB

基于新创建的镜像helloworld 启动一个容器

[[email protected] ~]#docker run -it centos:httpd /bin/bash
[[email protected] /]#systemctl start httpd


完成制作并启动httpd进行了验证。


3.制作方法 docker build方式

#创建工作目录

[[email protected] /]# mkdir docker-build
[[email protected] /]# cd docker-build/
[[email protected] docker-build]# ls
[[email protected] docker-build]# touch Dockerfile

#编辑Dockerfile文件

[[email protected] docker-build]# vim Dockerfile 

FROM centos     # base container
MAINTAINER frog <[email protected]>        #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
[[email protected] docker-build]# ls
Dockerfile
[[email protected] docker-build]# echo "/usr/sbin/httpd DFOREGROUND" > httpdStart.sh
[[email protected] docker-build]# echo "hello world" > index.html
[[email protected] docker-build]# chmod a+x httpStart.sh 
[[email protected] docker-build]# ls
Dockerfile  httpStart.sh  index.html

#使用build 命令创建centos:httpd 镜像

[[email protected] docker-build]# docker build -t centos:httpd .   #这里的‘.’指Dockerfile文件路径

#检查新创建的镜像文件centos:httpd

[[email protected] 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

以上是关于Docker笔记三 Docker镜像制作的主要内容,如果未能解决你的问题,请参考以下文章

Docker学习笔记 —— 镜像制作(Dockerfile)

Docker学习笔记 —— 镜像制作(Dockerfile)

Docker笔记——Docker安装及制作镜像

Docker笔记——jenkins镜像制作

Docker学习笔记 —— 镜像仓库制作(公有+私有+Harbor)

Docker学习笔记 —— 镜像仓库制作(公有+私有+Harbor)