實戰(zhàn):構(gòu)建自己的CentOS
一、創(chuàng)建自己的CentOS
(1)、編寫自己的配置文件
[root@localhost home]# mkdir dockerfile-1
[root@localhost home]# cd dockerfile-1
[root@localhost dockerfile-1]# vi docker-centos
#需要構(gòu)建的鏡像
FROM centos:7
#如果不指定版本那么默認會生成Centos8的鏡像,
#而我們現(xiàn)在用的是Centos7,安裝命令的時候yum倉庫會和Centos8的不兼容,從而導致保存無法完成鏡像的創(chuàng)建和命令的安裝
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則表示創(chuàng)建成功
Successfully tagged docker-centos:1.0
docker build -f /home/dockerfile1/docker-centos -t docker-centos:1.0 .
- 命令: docker build -f 文件路徑 -t 鏡像名:【Tag】
(3)、測試運行
- 系統(tǒng)的原生鏡像
[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的構(gòu)建流程
[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"]
#構(gòu)建鏡像
[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運行,發(fā)現(xiàn)我們啟動鏡像的時候“l(fā)s -a”命令運行生效
[root@localhost dockerfile1]# docker run e8ee368cfd82
.
..
.dockerenv
anaconda-post.log
... ...
usr
var
#追加一個命令“-l”,發(fā)現(xiàn)報錯
[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
#需要我們在后面操作一個完整的命令才能執(zhí)行
[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
... ...
#我們可以發(fā)現(xiàn)在ENTRYPOINT環(huán)境下命令是可以直接追加執(zhí)行不需要覆蓋
[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.
進入容器發(fā)現(xiàn)ipv4被禁用,容器內(nèi)部無法上網(wǎng)
解決方法:
#修改配置文件
[root@localhost ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward=1
#驗證是否成功
[root@localhost ~]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
#重啟容器和網(wǎng)絡
[root@localhost ~]# systemctl restart network
[root@localhost ~]# systemctl restart docker
(2)、
- Error: Failed to download metadata for repo ‘a(chǎn)ppstream’: Cannot prepare internal mirrorlist: No URLs in mirrorlist
yum報錯,從而導致保存無法完成鏡像的創(chuàng)建和命令的安裝
[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,但是因為系統(tǒng)版本是centos7和centos8yum倉庫不兼容導致鏡像無法創(chuàng)建,命令無法安裝。
[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創(chuàng)建images容器鏡像,run運行容器,執(zhí)行操作commit打包鏡像保存到本地。push提交到dockerhub倉庫,或者也可以pull從dockerhub倉庫下載到本地文章來源:http://www.zghlxwxcb.cn/news/detail-561063.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-561063.html
到了這里,關(guān)于【編寫DockerFile構(gòu)建自己的容器】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!