【编写DockerFile构建自己的容器】

实战:构建自己的CentOS

一、创建自己的CentOS

(1)、编写自己的配置文件

[root@localhost home]# mkdir dockerfile-1
[root@localhost home]# cd dockerfile-1
[root@localhost dockerfile-1]# vi docker-centos

#需要构建的镜像
FROM centos:7  

#如果不指定版本那么默认会生成Centos8的镜像,
#而我们现在用的是Centos7,安装命令的时候yum仓库会和Centos8的不兼容,从而导致保存无法完成镜像的创建和命令的安装

MAINTAINER grj<3374864596@qq.com>  ==>镜像作者的信息

WORKDIR /usr/local ==>工作目录

#需要安装的命令
RUN yum -y install vim
RUN yum -y install net-tools

#映射80端口
EXPOSE 80

#命令提示符输出
CMD echo "---End---"
CMD /bin/bash

(2)、生成目标镜像

[root@localhost dockerfile1]# docker build -f /home/dockerfile1/docker-centos -t docker-centos:1.0 .
Sending build context to Docker daemon  2.048kB
Step 1/8 : FROM centos
 ---> 5d0da3dc9764
Step 2/8 : MAINTAINER grj<3374864596@qq.com>

... ...

Successfully built 3f478b749d2e ==>当看到了这两个Successfully则表示创建成功

Successfully tagged docker-centos:1.0 

docker build -f /home/dockerfile1/docker-centos -t docker-centos:1.0 .

  • 命令: docker build -f 文件路径 -t 镜像名:【Tag】

(3)、测试运行

  • 系统的原生镜像
[root@localhost dockerfile1]# docker run -it eeb6ee3f44bd /bin/bash 
[root@08a6a834848a /]# 
[root@08a6a834848a /]# pwd  ==>默认工作目录为/
/
[root@08a6a834848a /]# 
[root@08a6a834848a /]# ifconfig  ==>一些基础的命令无法使用
bash: ifconfig: command not found
[root@08a6a834848a /]# 
[root@08a6a834848a /]# vim 1.txt  ==>一些基础的命令无法使用
bash: vim: command not found
  • 我们制作的镜像
[root@localhost dockerfile1]# docker run -it 3f478b749d2e /bin/bash
[root@515ac8e584d0 local]# pwd  ==>默认工作目录为/usr/local
/usr/local
[root@515ac8e584d0 local]# ifconfig ==>命令可以使用
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 14  bytes 1218 (1.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 439 (439.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@515ac8e584d0 local]# vim 1.txt ==>命令可以使用

(4)、查看docker的构建流程

[root@localhost ~]# docker history eeb6ee3f44bd
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
eeb6ee3f44bd   7 months ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      7 months ago   /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      7 months ago   /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4…   204MB 

docker history eeb6ee3f44bd

二、CMD和ENTRYPOINT

  • CMD
CMD         #指定这个容器启动的时候需要运行的命令,只有最后一个会生效,可替代
ENTRYPOINT  #指定这个容器启动的时候要运行的命令。可以追加命令。

#编写dockerfile文件
[root@localhost dockerfile1]# vi docker-centos-1 
FROM centos:7
CMD ["ls","-a"]

#构建镜像
[root@localhost dockerfile1]# docker build -f docker-centos-1 -t cmdtest .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 1770ec84cf7d
Removing intermediate container 1770ec84cf7d
 ---> e8ee368cfd82
Successfully built e8ee368cfd82
Successfully tagged cmdtest:latest

#run运行,发现我们启动镜像的时候“ls -a”命令运行生效
[root@localhost dockerfile1]# docker run e8ee368cfd82
.
..
.dockerenv
anaconda-post.log

... ...

usr
var

#追加一个命令“-l”,发现报错
[root@localhost dockerfile1]# docker run e8ee368cfd82 -l
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0001] error waiting for container: context canceled 

#需要我们在后面操作一个完整的命令才能执行
[root@localhost dockerfile1]# docker run e8ee368cfd82 ls -al
total 12

... ...
drwxrwxrwt.   7 root root   132 Nov 13  2020 tmp
drwxr-xr-x.  13 root root   155 Nov 13  2020 usr
drwxr-xr-x.  18 root root   238 Nov 13  2020 var
  • ENTRYPOINT
[root@localhost dockerfile1]# vi docker-centos-ENTRYPOINT
FROM centos
ENTRYPOINT ["ls","-a"]

[root@localhost dockerfile1]# docker build -f docker-centos-ENTRYPOINT -t entrypoint-tesy .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM centos
... ...

#我们可以发现在ENTRYPOINT环境下命令是可以直接追加执行不需要覆盖
[root@localhost dockerfile1]# docker run 4acf07817552 -l
total 0
... ...
drwxr-xr-x.  12 root root 144 Sep 15  2021 usr
drwxr-xr-x.  20 root root 262 Sep 15  2021 var


Tips:

(1)、

  • ocker:IPv4 forwarding is disabled. Networking will not work.
    进入容器发现ipv4被禁用,容器内部无法上网

解决方法:

#修改配置文件
[root@localhost ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward=1

#验证是否成功
[root@localhost ~]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1

#重启容器和网络
[root@localhost ~]# systemctl restart network
[root@localhost ~]# systemctl restart docker

(2)、

  • Error: Failed to download metadata for repo ‘appstream’: Cannot prepare internal mirrorlist: No URLs in mirrorlist

yum报错,从而导致保存无法完成镜像的创建和命令的安装

[root@localhost dockerfile1]# docker build -f docker-centos -t docker-centos:1.0 .
Sending build context to Docker daemon  4.096kB
Step 1/8 : FROM centos
 ---> 5d0da3dc9764
Step 2/8 : MAINTAINER grj<3374864596@qq.com>
 ---> Running in f87ee9c348b4
Removing intermediate container f87ee9c348b4
 ---> 31d964a88c41
Step 3/8 : WORKDIR /usr/local
 ---> Running in 3f54f1494fe6
Removing intermediate container 3f54f1494fe6
 ---> 4a89bd58e3f1
Step 4/8 : RUN yum -y install vim
 ---> Running in 041958a9f9c0
CentOS Linux 8 - AppStream                       71  B/s |  38  B     00:00    
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
The command '/bin/sh -c yum -y install vim' returned a non-zero code: 1

解决方法:

修改dockerfile文件,指定一个centos版本,如果不指定dockerhub默认按照最新的centos8,但是因为系统版本是centos7和centos8yum仓库不兼容导致镜像无法创建,命令无法安装。

[root@localhost dockerfile1]# vi docker-centos

FROM centos:7
MAINTAINER grj<3374864596@qq.com>

WORKDIR /usr/local

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80
CMD echo "---End---"
CMD /bin/bash

三、Docker流程

在这里插入图片描述

dockerfile创建images容器镜像,run运行容器,执行操作commit打包镜像保存到本地。push提交到dockerhub仓库,或者也可以pull从dockerhub仓库下载到本地

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值