在容器中完成操作后制作;
制作命令:docker commit
格式:docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
--author, -a 作者信息
--pause, -p 制作时候暂停
--message, -m 创建时候注释
--change, -c 创建时应用dockerfile中的命令
示例:制作一个带有httpd服务的镜像
[root@node-65 ~]# docker run --name b1 -it docker.io/busybox
#创建网页目录
/ # mkdir /data/html -pv
created directory: '/data/'
created directory: '/data/html'
/ # vi /data/html/index.html
BUSY BOX SERVER
#启动httpd服务
/ # httpd -h /data/html/
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
#基于bi容器制作镜像
[root@node-65 ~]# docker commit -p -a "hehe " b1 docker.io/hehe/bbox:v0.1.1-httpd
或者使用-c选项指定网页路径
[root@node-65 ~]# docker commit -p -a "hehe " -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' b1 docker.io/hehe/bbox:v0.1.2-httpd
#查询本地镜像
[root@node-65 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hehe/bbox v0.1.1-httpd 786bd69b3a86 49 seconds ago 1.15 MB
docker.io/busybox latest 758ec7f3a1ee 3 days ago 1.15 MB
#使用生成的镜像创建一个容器
[root@node-65 ~]# docker run --name h1 -d docker.io/hehe/bbox:v0.1.1-httpd /bin/httpd -f -h /data/html
如果指定网页路径使用:
[root@node-65 ~]# docker run --name h1 -d docker.io/hehe/bbox:v0.1.1-httpd
#查询h1容器ip等信息
[root@node-65 ~]# docker inspect h1
.............
"IPAddress": "172.17.0.3"
#使用curl命令,测试容器启动,http服务是否一起启动
[root@node-65 ~]# curl http://172.17.0.3
BUSY BOX SERVER
示例2:配置一个nginx镜像,启动容器使nginx工作在前台
#下载centos镜像
[root@node-65 ~]# docker pull docker.io/centos
#使用centos镜像启动容器
[root@node-65 ~]# docker run --name c1 -it docker.io/centos
#报错
WARNING: IPv4 forwarding is disabled. Networking will not work.
#打开宿主机的核心转发功能
net.ipv4.ip_forward=1 >> /usr/lib/sysctl.d/00-system.conf
#重启网络
#再次使用centos镜像启动容器
[root@node-65 ~]# docker run --name c2 -it docker.io/centos
#安装epel源和nginx
[root@f68abe0c2601 /]# yum install epel-release -y
root@f68abe0c2601 /]# yum install nginx -y
#配置nginx文件,添加
[root@f68abe0c2601 /]# vi /etc/nginx/nginx.conf
...........
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
daemon off; #添加,使nginx工作在前台
#测试启动nginx
[root@f68abe0c2601 /]# nginx #此时光标停止,证明nginx已经工作在前台了
#制作镜像
[root@node-65 ~]# docker commit -p -a "hehe " -c 'CMD ["/usr/sbin/nginx"]' c2 docker.io/hehe/centos:v0.1.0-nginx
sha256:6aadb46a288a0907fb2dd4b395ad066c00cbd52b9f84051b6245f269e0404c5a
[root@node-65 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hehe/centos v0.1.0-nginx 6aadb46a288a 46 seconds ago 395 MB
##使用生成的镜像创建一个容器,-d运行在后台
[root@node-65 ~]# docker run --name h2 -d docker.io/hehe/centos:v0.1.0-nginx
20b666c4a0aa6b1ef561c66606cf94587b3fc1535131cb3e13ddb3efee28cb17
[root@node-65 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20b666c4a0aa docker.io/hehe/centos:v0.1.0-nginx "/usr/sbin/nginx" 15 seconds ago Up 14 seconds h2
#查询容器ip信息
[root@node-65 ~]# docker inspect
........
"IPAddress": "172.17.0.3",
#测试nginx
[root@node-65 ~]# curl http://172.17.0.3
nginx测试页信息
.........
..........
1、制作格式
格式
# Comment #首行注释信息
INSTRUCTION arguments #指令一般使用大写字母
环境变量ENV
#变量格式
$variable_name or ${variable_name}
#判断
${variable:-word} #variable变量非空且有值,就用本身的值,否则就赋值为word(给变量设置默认值)
${variable:+word} #variable变量非空且有值,就赋值为word,如果无值则不赋值
.dockerignore路径使用
2、制作指令
FROM
Syntax语法格式
FROM[: ] 或
FROM@
:指定作为base image的名称;
:base image的标签,为可选项,省略时默认为latest;
MAINTANIER
Syntax语法格式
MAINTAINER
可是任何文本信息,但约定俗成地使用作者名称及邮件地址
COPY
Syntax语法
COPY... 或
COPY ["",... " "]
:要复制的源文件或目录,支持使用通配符
:目标路径,即正在创建的image的文件系统路径;建议为 使用绝对路径,否则,COPY指定则以WORKDIR为其起始路径;
注意:在路径中有空白字符时,通常使用第二种格式
文件复制准则
#查看已有镜像
[root@node-65 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
.........
docker.io/busybox latest 758ec7f3a1ee 6 days ago 1.15 MB
........
#创建工作目录
[root@node-65 ~]# mkdir bbox
[root@node-65 ~]# cd bbox/
#编辑要拷贝的文件
[root@node-65 bbox]# vim index.html
#编辑dockerfile文件
[root@node-65 bbox]# vim dockerfile
FROM busybox:latest #基于那个镜像
MAINTAINER "hehe " #作者信息
COPY index.html /data/html/ #执行命令,目标文件目录不存在会自动创建
COPY data /data/ #当复制目录时注意,只递归复制data目录下所有文件,不复制data目录本身,/data/为容器中自动创建目录。
#构建镜像
[root@node-65 bbox]# docker build ./ -t docker.io/busybox:v0.2.0 #指明构建的根目录,有dockerfile的就是构建根目录,-t指明tag标签
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM busybox:latest #执行指令第一行,构建第一层镜像
---> 758ec7f3a1ee
Step 2/3 : MAINTAINER "hehe " #执行指令第二行,构建第二层镜像
---> Running in f5fc881f9f93
---> b3e8146d6485
Removing intermediate container f5fc881f9f93
Step 3/3 : COPY index.html /data/html/ #执行指令第三行,构建第三层镜像 6e71f53ff091
Removing intermediate container 03f483f87c98
Successfully built 6e71f53ff091 #构建成功
#查询镜像
[root@node-65 bbox]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox v0.2.0 6e71f53ff091 16 minutes a
ADD
Syntax语法格式
ADD... 或
ADD ["",... " "]
操作准则
#编辑dockerfile文件
[root@node-65 bbox]# vim dockerfile
FROM busybox:latest #基于那个镜像
MAINTAINER "hehe " #作者信息
ADD http://120.52.51.15/nginx.org/download/nginx-1.15.8.tar.gz /usr/src/ #指明下路径,下载到容器的那个目录下
#构建镜像
[root@node-65 bbox]# docker build ./ -t docker.io/busybox:v0.2.1
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM busybox:latest
---> 758ec7f3a1ee
Step 2/3 : MAINTAINER "hehe "
---> Using cache
---> b3e8146d6485
Step 3/3 : ADD http://120.52.51.15/nginx.org/download/nginx-1.15.8.tar.gz /usr/src/
Downloading 1.028 MB/1.028 MB #第三行执行已下载
---> 089314c3ad7e
Removing intermediate container 7ae8d8e836e0
Successfully built 089314c3ad7e #构建完成
#查询镜像
[root@node-65 bbox]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox v0.2.1 089314c3ad7e About a minu
WORKDIR
Syntax语法格式
WORKDIR
例如
l WORKDIR /var/log
l WORKDIR $STATEPATH
VOLUME
Syntax语法
VOLUME或
VOLUME [""] 多个卷由中括号括起来中间逗号隔开
#编辑dockerfile文件
[root@node-65 bbox]# vim dockerfile
FROM busybox:latest #基于那个镜像
MAINTAINER "hehe " #作者信息
ADD http://120.52.51.15/nginx.org/download/nginx-1.15.8.tar.gz /usr/src/ #指明下路径,下载到容器的那个目录下
VOLUME /data/html/ #指明容器中的挂载路径,宿主机对应路径有docker管理生成
EXPOSE
Syntax语法
EXPOSE[/ ] [ [/ ] ...]
#编辑dockerfile文件
[root@node-65 bbox]# vim dockerfile
FROM busybox:latest #基于那个镜像
MAINTAINER "hehe " #作者信息
ADD http://120.52.51.15/nginx.org/download/nginx-1.15.8.tar.gz /usr/src/ #指明下路径,下载到容器的那个目录下
VOLUME /data/html/ #指明容器中的挂载路径,宿主机对应路径有docker管理生成
EXPOSE 80/tcp #暴露80端口
ENV
Syntax语法
ENV或
ENV= ...
第一种格式中,
,因此,一次只能设置一个变量;
第二种格式可用一次设置多个变量,每个变量为一个"
键值对,如果
对
定义多个变量时,建议使用第二种方式,以便在同一层中完成所有功能
例如:
#编辑dockerfile文件
[root@node-65 bbox]# vim dockerfile
FROM busybox:latest #基于那个镜像
MAINTAINER "hehe " #作者信息
ENV nginx_file http://120.52.51.15/nginx.org/download/nginx-1.15.8.tar.gz #设定nginx_file变量值
ADD ${nginx_file} /usr/src/ #调用变量,下载到容器的那个目录下
RUN
Syntax语法
RUN或
RUN ["", " ", " "]
#编辑dockerfile文件
[root@node-65 bbox]# vim dockerfile
FROM busybox:latest #基于那个镜像
MAINTAINER "hehe " #作者信息
RUN /bin/adduser -D myuser && \
/bin/mkdir /tmp/testdir #添加一个myuser用户,并创建目录
CMD
Syntax语法格式
CMD或
CMD [“”, “ ”, “ ”] 或
CMD [""," "]
ENTRYPOINT
Syntax语法格式
ENTRYPOINT
ENTRYPOINT ["", " ", " "]
USER
Syntax语法格式
USER|
ONBUILD
Syntax语法格式
ONBUILD
#编辑dockerfile文件
[root@node-65 bbox]# vim dockerfile
FROM busybox:latest #基于那个镜像
MAINTAINER "hehe " #作者信息
ONBUILD RUN /bin/adduser -D myuser #当使用这个镜像做基础镜像时候,触发执行命令,自动添加一个myuser用户
示例使用dockerfile文件制作nginx的镜像:
#创建工作目录
[root@node-65 ~]# mkdir nginx
#创建nginx测试页
[root@node-65 ~]# vim index.html
nginx test page
[root@node-65 nginx]# vim dockerfile
FROM centos:latest #基准镜像
MAINTAINER "hehe " #作者信息
WORKDIR /usr/local/src/ #工作目录
ENV NG_VERSION nginx-1.15.8 #定义环境变量
RUN yum -y install epel-release #安装epel仓库
RUN yum -y install wget && wget http://120.52.51.15/nginx.org/download/$NG_VERSION.tar.gz && tar xzvf $NG_VERSION.tar.gz #下载nginx文件并解压
#安装编译依赖包
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel && yum install -y pcre-devel libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data
RUN yum clean all #清理仓库
RUN useradd -M -s /sbin/nologin nginx #创建nginx用户
WORKDIR /usr/local/src/$NG_VERSION #切换工作目录
#编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
ADD index.html /usr/local/nginx/html #复制测试页面到容器中
VOLUME /usr/local/nginx/html #设置容器中要挂在到宿主机的目录
ENV PATH /usr/local/nginx/sbin:$PATH #设置sbin环境变量
EXPOSE 80/tcp #暴露80端口
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
#当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g "daemon off;"而当一起连用的时候命令格式最好一致(这里选择的都是json格式的是成功的,如果都是sh模式可以试一下)
#构建镜像
[root@node-65 nginx]# docker build ./ -t docker.io/centos-nginx:v0.6.4
#根据构建的镜像运行容器
[root@node-65 nginx]# docker run --name h17 -d -p82:80 docker.io/centos-nginx:v0.6.4
a0657540fc0edc1e57e2fb494af5714f14242a4cf18bb076b4cb30099f45b46e
#查看容器
[root@node-65 nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0657540fc0e docker.io/centos-nginx:v0.6.4 "nginx -g 'daemon ..." 8 minutes ago Up 8 minutes 0.0.0.0:82->80/tcp h17
#查看卷挂载信息
[root@node-65 nginx]# docker inspect -f "{{.Mounts}}" h17
[{volume 2f4e45badaa0c21bc9211ebbb2c44c6f68d3c19b36facf74b3d9ac6be0775d64 /var/lib/docker/volumes/2f4e45badaa0c21bc9211ebbb2c44c6f68d3c19b36facf74b3d9ac6be0775d64/_data /usr/local/nginx/html local true }]
容器测试页面
#修改宿主机挂载目录
[root@node-65]# vim /var/lib/docker/volumes/2f4e45badaa0c21bc9211ebbb2c44c6f68d3c19b36facf74b3d9ac6be0775d64/_data/index.html
nginx test page
HELLO DOCKER
容器测试页面也随之更改
格式:docker save [OPTIONS] IMAGE [IMAGE...]
[root@node-65 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hehe/centos v0.1.0-nginx 6aadb46a288a 23 minutes ago 395 MB
#镜像保存到本地
[root@node-65 ~]# docker save -o /root/centos-v0.1.0-nginx.tar 6aadb46a288a
[root@node-65 ~]# ll
total 747852
-rw-------. 1 root root 1233 Sep 25 19:45 anaconda-ks.cfg
-rw------- 1 root root 406230016 Dec 30 11:14 centos-v0.1.0-nginx.tar
docker load从tar存档文件读取
格式:docker load [OPTIONS]
--input, -i 从tar存档文件读取,而不是STDIN
--quiet, -q false 工作在静默模式,不输出操作信息
[root@node-65 ~]# docker load -i /root/centos-v0.1.0-nginx.tar