本篇博客主要就Docker 镜像构建、私有仓库简易版、数据卷和数据容器介绍以及端口映射等操作
docker镜像主要有三种自定义构建方式
① dockerfile :基于源镜像修改配置,似乎用dockerfile生成所需的镜像,用于新业务
② 基于已有镜像容器进行操作,经常用于业务升级、迁移、镜像打包
③ 基于本地模板进行构建(使用较少)
Dockerfile文件时以组指令组成,文件结构分为四部分
① 基础镜像信息
② 维护者信息
③ 镜像操作指令
④ 容器启动时执行指令
dockerfile操作指令详解:
[root@docker ~]# mkdir /apache
[root@docker ~]# cd /apache/
[root@docker apache]# vim Dockerfile
Dockerfile run.sh
#基于基础镜像
FROM centos:7
#维护镜像的用户信息
MAINTAINER this is test
#镜像操作指令安装apache
RUN yum -y update
RUN yum -y install httpd
#开启80端口
EXPOSE 80
#复制网站首页文件
ADD index.html /var/www/html/index.html
#将执行脚本复制到镜像中
ADD run.sh /run.sh
RUN chmod 775 /run.sh
#启动容器时执行脚本
CMD ["/run.sh"]
[root@docker apache]# vim run.sh
#!/bin/bash
#清除HTTPD的缓存
rm -rf /run/httpd/*
#启动Apache进程
exec /usr/sbin/apachectl -D FOREGROUND
[root@docker apache]# echo "this is apache web" > index.html
[root@docker apache]# ls
Dockerfile index.html run.sh
[root@docker apache]# docker build -t httpd:new .
Successfully built e66546f52890
Successfully tagged httpd:new
...省略部分内容
[root@docker apache]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd new e66546f52890 About a minute ago 524MB
centos 7 5e35e350aded 5 months ago 203MB
[root@docker apache]# docker run -d --name HTTPD1 -p 2389:80 httpd:new
0dc94b6feaa02258296b0e9af35b002d6f219e7545f7a5d752a4877b9e1bd700
[root@docker apache]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0dc94b6feaa0 httpd:new "/run.sh" 7 seconds ago Up 6 seconds 0.0.0.0:2389->80/tcp HTTPD1
将容器中云心的程序及其环境打包生成新的镜像
格式:docker commit [选项] 容器ID/名称 仓库名称:[标签]
-m :说明信息
-a :作者信息
示例:
#现有的容器
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0dc94b6feaa0 httpd:new "/run.sh" 12 minutes ago Up 12 minutes 0.0.0.0:2389->80/tcp HTTPD1
#创建一个centos容器
[root@docker ~]# docker create -it centos:7 /bin/bash
7ac5f67e67c8e0442c8f73456a47c2c6e365fc700f5707a15c80d48152aac802
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ac5f67e67c8 centos:7 "/bin/bash" 18 seconds ago Created amazing_hofstadter
0dc94b6feaa0 httpd:new "/run.sh" 15 minutes ago Up 15 minutes 0.0.0.0:2389->80/tcp HTTPD1
[root@docker ~]# docker commit -m "test" -a "test2" 7ac5f67e67c8 test:centos
sha256:e6a200efa441d5cc1cef64738ff16c6acfb4c63d87e9db9126aa7b97b5ec4c85
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test centos e6a200efa441 9 seconds ago 203MB
httpd new e66546f52890 22 minutes ago 524MB
centos 7 5e35e350aded 5 months ago 203MB
#这里":"不要用中文格式
[root@docker ~]# cat debian-7.0-x86-minimal.tar.gz | docker import - docke:new
sha256:47eda903d349a5c965927b8bf00676584c384104eb83d6012583a061efeed6ca
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docke new 47eda903d349 About a minute ago 215MB
test centos e6a200efa441 10 minutes ago 203MB
httpd new e66546f52890 33 minutes ago 524MB
centos 7 5e35e350aded 5 months ago 203MB
[root@docker ~]# docker pull registry
[root@docker ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
486039affc0a: Pull complete
ba51a3b098e6: Pull complete
8bb4c43d6c8e: Pull complete
6f5f453e5f2d: Pull complete
42bc10b72f42: Pull complete
Digest: sha256:7d081088e4bfd632a88e3f3bcd9e007ef44a796fddfe3261407a3f9f04abe1e7
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docke new 47eda903d349 8 minutes ago 215MB
test centos e6a200efa441 18 minutes ago 203MB
httpd new e66546f52890 40 minutes ago 524MB
registry latest 708bc6af7e5e 2 months ago 25.8MB
centos 7 5e35e350aded 5 months ago 203MB
[root@docker ~]# vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.226.167:5000"], #因为仓库位置就是在本地,所以直接指向本地,如果在其他服务器,地址指向其他服务器IP即可
"registry-mirrors": ["https://jqqwsp8f.mirror.aliyuncs.com"]
}
[root@docker ~]# docker create -it registry /bin/bash
901dbc4bb771b8056dfa71857028fa61ae4c5056807aecfb1114250c1a894b63
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
901dbc4bb771 registry "/entrypoint.sh /bin…" 4 seconds ago Created nice_cray
7ac5f67e67c8 centos:7 "/bin/bash" 28 minutes ago Created amazing_hofstadter
0dc94b6feaa0 httpd:new "/run.sh" 43 minutes ago Exited (137) 57 seconds ago HTTPD1
[root@docker ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
f654b5c0629a4d99e8a73bfca7c5fd5b0d29c7ab8fed5caece278c2d63b2f30c
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f654b5c0629a registry "/entrypoint.sh /etc…" 8 minutes ago Up 8 minutes 0.0.0.0:5000->5000/tcp crazy_morse
.....省略部分内容
[root@docker ~]# docker pull nginx
[root@docker ~]# docker tag nginx:latest 192.168.226.167:5000/nginx
[root@docker ~]# docker push 192.168.226.167:5000/nginx
The push refers to repository [192.168.226.167:5000/nginx]
be91fceb796e: Pushed
919b6770519b: Pushed
b60e5c3bcef2: Pushed
latest: digest: sha256:6b3b6c113f98e901a8b1473dee4c268cf37e93d72bc0a01e57c65b4ab99e58ee size: 948
[root@docker ~]# curl -XGET http://192.168.226.167:5000/v2/_catalog
{"repositories":["nginx"]}
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docke new 47eda903d349 43 minutes ago 215MB
test centos e6a200efa441 52 minutes ago 203MB
httpd new e66546f52890 About an hour ago 524MB
registry latest 708bc6af7e5e 2 months ago 25.8MB
centos 7 5e35e350aded 5 months ago 203MB
#nginx镜像已删除
[root@docker ~]# docker pull 192.168.226.167:5000/nginx
Using default tag: latest
latest: Pulling from nginx
123275d6e508: Pull complete
6cd6a943ce27: Pull complete
a50b5ac4a7fb: Pull complete
Digest: sha256:6b3b6c113f98e901a8b1473dee4c268cf37e93d72bc0a01e57c65b4ab99e58ee
Status: Downloaded newer image for 192.168.226.167:5000/nginx:latest
192.168.226.167:5000/nginx:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docke new 47eda903d349 45 minutes ago 215MB
test centos e6a200efa441 55 minutes ago 203MB
httpd new e66546f52890 About an hour ago 524MB
192.168.226.167:5000/nginx latest e791337790a6 3 days ago
registry私有仓库设置步骤小结:
① 下载registry镜像
② 生成registry容器,开放5000端口
③ 客户都安设置daemon.jsopn文件。指定私有仓库
④ 镜像打标签, docker tag 原镜像名 仓库IP:端口/镜像名
⑤ 上传镜像 docker push 仓库IP:端口/镜像名
⑥ 下载镜像 docker pull —> docker images 查看
一般生产环境使用barbor较多(图形化)
[root@docker ~]# docker run -v /var/www:/data1 --name web11 -it centos:7 /bin/bash
[root@01d1bac2b74f /]# ls
anaconda-post.log data1 etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
[root@01d1bac2b74f /]# cd data1/
[root@01d1bac2b74f data1]# touch test1
[root@01d1bac2b74f data1]# ls
test1
[root@docker ~]# ls /var/www/
test1
[root@docker ~]# docker run --name web100 -v /data1 -v /data2 -it centos:7 /bin/bash
[root@6b432f4fcfc7 /]# ls
anaconda-post.log data1 dev home lib64 mnt proc run srv tmp var
bin data2 etc lib media opt root sbin sys usr
[root@docker ~]# docker run -it --volumes-from web100 --name db1 centos:7 /bin/bash
[root@c0c357c27cae /]# cd data1/
[root@c0c357c27cae data1]# touch demo01
[root@c0c357c27cae data1]# cd ../data2
[root@c0c357c27cae data2]# touch demo02
[root@6b432f4fcfc7 /]# cd data1
[root@6b432f4fcfc7 data1]# ls
demo01
[root@6b432f4fcfc7 data1]# ls ../data2
demo02
#以上,实现了容器之间的共享
-P 随机生成端口号
示例:
[root@docker ~]# docker run -d -P httpd:latest
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab7581da0ef5 httpd:latest "httpd-foreground" 17 seconds ago Up 16 seconds 0.0.0.0:32768->80/tcp eager_ganguly
#可观测到随机生成的端口号为32768
-p 指定端口号
示例:
[root@docker ~]# docker run -d -p 123:80 httpd:latest
77b069f4e83b226d33eda152ac564e0c4e66dcbd16090b08b8a81552fd5f3ff7
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77b069f4e83b httpd:latest "httpd-foreground" 9 seconds ago Up 8 seconds 0.0.0.0:123->80/tcp tender_kala
#端口号为指定的123
#创建并运行web1容器,端口号自动映射
[root@docker ~]# docker run -itd -P --name web1 centos /bin/bash
8656ad979422b4cbd04e3e88741a92caf40c7ac3b66a242dcc239b565eccb761
#创建并运行web2容器,链接到web1和其通信。
[root@docker ~]# docker run -itd -P --name web2 --link web1:web1 centos:7 /bin/bash
8e26b5851010f28508d0608fe16ca42903c87120d2316cc3555b47be862a81a0
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e26b5851010 centos:7 "/bin/bash" 4 seconds ago Up 3 seconds web2
8656ad979422 centos "/bin/bash" About a minute ago Up About a minute web1
#进入web2容器 ping web1
[root@docker ~]# docker exec 8e26b5851010 /bin/bash
[root@docker ~]# docker exec -it 8e26b5851010 /bin/bash
[root@8e26b5851010 /]# ping web1
PING web1 (172.17.0.2) 56(84) bytes of data.
64 bytes from web1 (172.17.0.2): icmp_seq=1 ttl=64 time=0.124 ms
64 bytes from web1 (172.17.0.2): icmp_seq=2 ttl=64 time=0.053 ms
本篇博客主要介绍了docker容器的一些常用操作,其中dockerfile有位重要
容易出错的点一般在于:
① 镜像创建 docker build -t httpd:new . (在最后会有一个空格+".") 容易忘记
② dockerfile编写过程中 ADD命令 假如复制的是压缩包的话。默认会先进行解压缩然后复制
之后会继续对docker进行介绍~~