赞
踩
(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 .
[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 ==>命令可以使用
[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 #指定这个容器启动的时候要运行的命令。可以追加命令。
#编写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
[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
#修改配置文件
[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
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
dockerfile创建images容器镜像,run运行容器,执行操作commit打包镜像保存到本地。push提交到dockerhub仓库,或者也可以pull从dockerhub仓库下载到本地
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。