docker续3:

一、使用Dockerfile创建应用镜像

在Docker file中定义所需要执⾏的指令,使⽤ docker build创建镜像,过程中会按照dockerfile所定义的内容进⾏打开临时性容器,把docker file中命令全部执⾏完成,就得到了⼀个容器应⽤镜像,每⼀⾏命令都会出现容器,都会使⽤docker commit进⾏提交⼀个临时性的镜像

1.Docker file关键字

1.FORM 指定基础镜像为该镜像的最后修改版本
2.FROM <image :tag>指定基础镜像为该镜像的⼀个tag版本
3.MAINTAINER  指定镜像创建者,企业内部不⽤指定,对外发布也可以不指定
3.RUN 运⾏命令,安装软件
4.CMD 设置container启动时进⾏的操作,如果容器镜像中有这
个命名,启动容器时,不要⼿动让容器执⾏其他命令
5.ENTRYPORINT(⼊⼝点)cmd每次只能执⾏⼀个指令,entrypoint可以多⾏执⾏。
6.USER设置容器进程的⼀些⽤户
7.EXPOSE 暴露端⼝ 指定容器需要映射到宿主机的端⼝
8.ENV 设置环境变量 -e
9.ADD 从宿主机src复制⽂件到容器的dest路径
10.volumn 指定挂载点
11.WROKDIR 切换⽬录
12.ONBUILD在⼦镜像中执⾏指令

2.dockerfile应用

(1)httpd

[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   2 years ago   231MB
[root@localhost ~]# mkdir httpd0                                //创建⼀个⽬录,⽤于存储Docker file所使⽤的⽂件
[root@localhost ~]# cd httpd0/                        // 在此⽬录中创建Docker file⽂件,以及镜像制作所使⽤的⽂件
[root@localhost httpd0]# vim abc.sh                   //编辑启动脚本
#!/bin/bash
rm -rf /run/*httpd*
exec /sbin/httpd -D FOREGROUND
[root@localhost httpd0]# echo "httpd server is running" > index.html            //编辑index.html文件
[root@localhost httpd0]# vim Dockerfile                           //首字母必须大写
FROM    centos:latest

MAINTAINER    "我是你爸爸"

RUN    rm -rf /etc/yum.repos.d/* && curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo 

RUN     yum clean all && yum makecache

RUN     yum -y install epel-release

RUN     yum -y install httpd

ADD     index.html /var/www/html/index.html

ADD     abc.sh /abc.sh

RUN     chmod -v +x /abc.sh

EXPOSE   80

WORKDIR /

CMD    ["/bin/bash","/abc.sh"]
[root@localhost httpd0]# ls                                    //这里看到三个文件已经被创建成功
abc.sh  Dockerfile  index.html
[root@localhost httpd0]# docker build -t centos:httpd0 .   //使用docker build创建镜像,后面有个点,别忘了
[root@localhost httpd0]# docker images                                          //查看镜像
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       httpd0    47671b410a9b   12 seconds ago   280MB
centos       latest    5d0da3dc9764   2 years ago      231MB
[root@localhost httpd0]# docker run -it --name c1 -p80:80 centos:httpd0 /bin/bash           //使用创建的镜像启动容器
bfe452a31fc8f29819ca706165ea18ff2b167bbc956dd92081bb31443a5c566e
[root@7d4bca85353f /]# httpd -k start
[root@7d4bca85353f /]# curl localhost
httpd server is running
ctrl+p+q退出

(2)nginx

[root@localhost httpd0]# vim Dockerfile  
FROM    centos:latest

MAINTAINER    "nginx"

RUN    rm -rf /etc/yum.repos.d/* && curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo 

RUN     yum clean all && yum makecache

RUN     yum -y install epel-release

RUN     yum -y install nginx

RUN     echo "daemon off;" >> /etc/nginx/nginx.conf             #这个指令的作用是告诉 Nginx 以非守护进程(foreground,前台)模式运行,而不是默认的守护进程(daemon,后台)模式。在前台运行 Nginx 在调试时非常有用,因为这样可以直接看到输出和错误信息,而不需要查看后台的日志文件。

EXPOSE   80

WORKDIR /

CMD /usr/sbin/nginx
[root@localhost httpd0]# docker build -t centos:nginx .                    //创建镜像
[root@localhost httpd0]# docker run -d --name c0 -p80:80 -v /opt/:/usr/share/nginx/html/ centos:nginx         //隐藏启动并映射端口,挂载文件
2dd6dc32ee8085993681a69fa9d7850a66ce04586129e2724cec7558de582958
[root@localhost httpd0]# docker ps --all
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS          PORTS                               NAMES
2dd6dc32ee80   centos:nginx   "/bin/sh -c /usr/sbi…"   11 seconds ago   Up 10 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   c0
[root@localhost httpd0]# curl 192.168.1.76
真机浏览器访问192.168.1.76

二、创建私有镜像仓库

[root@localhost httpd0]# docker images                         //现在有两个自建的镜像(nginx,httpd0)
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
centos       nginx     189a0a502919   41 minutes ago      401MB
centos       httpd0    b91c1a8e080f   About an hour ago   338MB
centos       latest    5d0da3dc9764   2 years ago         231MB
[root@localhost httpd0]# docker pull registry                  //拉取registry(安装仓库镜像)
[root@localhost httpd0]# cd
[root@localhost ~]# mkdir /regist                             //创建挂载目录
[root@localhost ~]# docker run -d --name r2 -v /regist/:/var/lib/registry -p5000:5000 registry:latest         //启动容器,映射端口,挂载目录 
ddad689d0dab4fa6c2c199e28fc08b2830ab42e6ba082528bfacd523808cff58
[root@localhost ~]# docker ps --all
CONTAINER ID   IMAGE             COMMAND                   CREATED         STATUS                     PORTS                                       NAMES
ddad689d0dab   registry:latest   "/entrypoint.sh /etc…"   4 seconds ago   Up 4 seconds               0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   r2
[root@localhost ~]# curl http://192.168.1.76:5000/v2/_catalog                   //访问仓库
{"repositories":[]}
[root@localhost ~]# vim /etc/docker/daemon.json                             //添加仓库
{
    "registry-mirrors": [
    "https://do.nark.eu.org",
    "https://dc.j8.work",
    "https://docker.m.daocloud.io",
    "https://dockerproxy.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.nju.edu.cn"
    ],

        "hosts": [
        "tcp://0.0.0.0:2375",
        "unix:///var/run/docker.sock"
    ],         
    "insecure-registries":[
        "http://192.168.1.76:5000"
    ]         
}
[root@localhost ~]# systemctl restart docker                      //重启docker
[root@localhost ~]# docker start r2                                     //启动registry容器
[root@localhost ~]# curl http://192.168.1.76:5000/v2/_catalog                //重新访问仓库测试
{"repositories":[]}
[root@localhost ~]# docker tag centos:httpd0 192.168.1.76:5000/centos:httpd      //为要上传的镜像打标记
[root@localhost ~]# docker images
REPOSITORY                 TAG       IMAGE ID       CREATED             SIZE
centos                     nginx     189a0a502919   About an hour ago   401MB
192.168.1.76:5000/centos   httpd     b91c1a8e080f   2 hours ago         338MB
centos                     httpd0    b91c1a8e080f   2 hours ago         338MB
registry                   latest    cfb4d9904335   10 months ago       25.4MB
centos                     latest    5d0da3dc9764   2 years ago         231MB
[root@localhost ~]# docker push 192.168.1.76:5000/centos:httpd                     //推送(上传)出去
[root@localhost ~]# curl http://192.168.1.76:5000/v2/_catalog        //访问仓库,这里看到镜像已经上传成功了
{"repositories":["centos"]}

  • 21
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值