dockerfile案例
nginx
1.将Centos7 . repo 、 epel7 . repo 文件拖进虚拟机
[root@localhost ~]# ll
total 12
-rw-------. 1 root root 1093 Jul 21 00:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 2523 Aug 29 11:00 Centos-7.repo
-rw-r--r--. 1 root root 664 Aug 29 11:00 epel-7.repo
2.部署dockerfile
[root@localhost ~]# vim dockerfile
[root@localhost ~]# cat dockerfile
FROM centos:7
MAINTAINER liuaojie@example.com
ADD Centos-7.repo /etc/yum.repos.d/
ADD epel-7.repo /etc/yum.repos.d/
RUN yum install -y nginx
EXPOSE 80
CMD ["/usr/sbin/nginx","-g","daemon off;"]
3.构建镜像,镜像名称为nginx:z1
[root@localhost ~]# docker build -t nginx:z1 .
Successfully built 3cb2317e59ad
Successfully tagged nginx:z1
4.运行容器 ,映射端口为88:80
[root@localhost ~]# docker run -d --name liuaojie -p 88:80 nginx:z1
7268df6c288ce89d4d226691c12d97ef811275fceeafd29b8a3adf6106560944
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7268df6c288c nginx:z1 "/usr/sbin/nginx -g …" About a minute ago Up About a minute 0.0.0.0:88->80/tcp, :::88->80/tcp liuaojie
[root@localhost ~]# docker exec -it liuaojie /bin/bash
[root@7268df6c288c /]# cd /usr/share/nginx/html/
[root@7268df6c288c html]# ll
total 12
-rw-r--r--. 1 root root 3650 Oct 18 2021 404.html
-rw-r--r--. 1 root root 3693 Oct 18 2021 50x.html
lrwxrwxrwx. 1 root root 20 Aug 30 18:35 en-US -> ../../doc/HTML/en-US
drwxr-xr-x. 2 root root 27 Aug 30 18:35 icons
lrwxrwxrwx. 1 root root 18 Aug 30 18:35 img -> ../../doc/HTML/img
lrwxrwxrwx. 1 root root 25 Aug 30 18:35 index.html -> ../../doc/HTML/index.html
-rw-r--r--. 1 root root 368 Oct 18 2021 nginx-logo.png
lrwxrwxrwx. 1 root root 14 Aug 30 18:35 poweredby.png -> nginx-logo.png
[root@7268df6c288c html]#
5.进入容器,删除默认网页,写入新的默认网页
[root@7268df6c288c html]# rm -rf index.html
[root@7268df6c288c html]# ls
404.html 50x.html en-US icons img nginx-logo.png poweredby.png
[root@7268df6c288c html]# echo liuaojie > index.html
[root@7268df6c288c html]# ls
404.html 50x.html en-US icons img index.html nginx-logo.png poweredby.png
[root@7268df6c288c html]#
扫雷
做前准备
1.在一号虚拟机下载httpd并设置开机自启
[root@hh1 ~]# mount /dev/cdrom /mnt/
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@hh1 ~]# yum -y install httpd
Last metadata expiration check: 2:40:15 ago on Tue 30 Aug 2022 09:29:24 PM CST.
Dependencies resolved.
=====================================================================================
Package Arch Version Repo Size
=====================================================================================
Installing:
httpd x86_64 2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1 appstream 1.4 M
Installing dependencies:
apr x86_64 1.6.3-12.el8 appstream 129 k
apr-util x86_64 1.6.1-6.el8 appstream 105 k
centos-logos-httpd noarch 85.8-2.el8 appstream 75 k
httpd-filesystem noarch 2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1 appstream 41 k
httpd-tools x86_64 2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1 appstream 108 k
mailcap noarch 2.1.48-3.el8 baseos 39 k
mod_http2 x86_64 1.15.7-5.module_el8.6.0+1111+ce6f4ceb appstream 155 k
Installing weak dependencies:
apr-util-bdb x86_64 1.6.1-6.el8 appstream 25 k
apr-util-openssl x86_64 1.6.1-6.el8 appstream 27 k
Enabling module streams:
httpd 2.4
Transaction Summary
=====================================================================================
Install 10 Packages
Total download size: 2.1 M
Installed size: 5.6 M
Downloading Packages:
(1/10): apr-util-1.6.1-6.el8.x86_64.rpm 558 kB/s | 105 kB 00:00
(2/10): apr-util-bdb-1.6.1-6.el8.x86_64.rpm 129 kB/s | 25 kB 00:00
(3/10): apr-util-openssl-1.6.1-6.el8.x86_64.rpm 759 kB/s | 27 kB 00:00
(4/10): apr-1.6.3-12.el8.x86_64.rpm 527 kB/s | 129 kB 00:00
(5/10): centos-logos-httpd-85.8-2.el8.noarch.rpm 953 kB/s | 75 kB 00:00
(6/10): httpd-filesystem-2.4.37-47.module_el8.6.0+11 744 kB/s | 41 kB 00:00
(7/10): httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb 8.8 MB/s | 1.4 MB 00:00
(8/10): httpd-tools-2.4.37-47.module_el8.6.0+1111+ce 884 kB/s | 108 kB 00:00
(9/10): mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4 1.6 MB/s | 155 kB 00:00
(10/10): mailcap-2.1.48-3.el8.noarch.rpm 1.3 MB/s | 39 kB 00:00
-------------------------------------------------------------------------------------
Total 1.4 MB/s | 2.1 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : apr-1.6.3-12.el8.x86_64 1/10
Running scriptlet: apr-1.6.3-12.el8.x86_64 1/10
Installing : apr-util-bdb-1.6.1-6.el8.x86_64 2/10
Installing : apr-util-openssl-1.6.1-6.el8.x86_64 3/10
Installing : apr-util-1.6.1-6.el8.x86_64 4/10
Running scriptlet: apr-util-1.6.1-6.el8.x86_64 4/10
Installing : httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x8 5/10
Installing : mailcap-2.1.48-3.el8.noarch 6/10
Running scriptlet: httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb 7/10
Installing : httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb 7/10
Installing : centos-logos-httpd-85.8-2.el8.noarch 8/10
Installing : mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4ceb.x86_64 9/10
Installing : httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64 10/10
Running scriptlet: httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64 10/10
Verifying : apr-1.6.3-12.el8.x86_64 1/10
Verifying : apr-util-1.6.1-6.el8.x86_64 2/10
Verifying : apr-util-bdb-1.6.1-6.el8.x86_64 3/10
Verifying : apr-util-openssl-1.6.1-6.el8.x86_64 4/10
Verifying : centos-logos-httpd-85.8-2.el8.noarch 5/10
Verifying : httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64 6/10
Verifying : httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb 7/10
Verifying : httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x8 8/10
Verifying : mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4ceb.x86_64 9/10
Verifying : mailcap-2.1.48-3.el8.noarch 10/10
Installed products updated.
Installed:
apr-1.6.3-12.el8.x86_64
apr-util-1.6.1-6.el8.x86_64
apr-util-bdb-1.6.1-6.el8.x86_64
apr-util-openssl-1.6.1-6.el8.x86_64
centos-logos-httpd-85.8-2.el8.noarch
httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.noarch
httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64
mailcap-2.1.48-3.el8.noarch
mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4ceb.x86_64
Complete!
[root@hh1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@hh1 ~]#
2.上传扫雷的软件包
[root@hh1 ~]# cd /var/www/html/
[root@hh1 html]# yum install -y lrzsz
Last metadata expiration check: 2:44:10 ago on Tue 30 Aug 2022 09:29:24 PM CST.
Dependencies resolved.
=====================================================================================
Package Architecture Version Repository Size
=====================================================================================
Installing:
lrzsz x86_64 0.12.20-43.el8 baseos 84 k
Transaction Summary
=====================================================================================
Install 1 Package
Total download size: 84 k
Installed size: 190 k
Downloading Packages:
lrzsz-0.12.20-43.el8.x86_64.rpm 338 kB/s | 84 kB 00:00
-------------------------------------------------------------------------------------
Total 89 kB/s | 84 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : lrzsz-0.12.20-43.el8.x86_64 1/1
Running scriptlet: lrzsz-0.12.20-43.el8.x86_64 1/1
Verifying : lrzsz-0.12.20-43.el8.x86_64 1/1
Installed products updated.
Installed:
lrzsz-0.12.20-43.el8.x86_64
Complete!
[root@hh1 html]# rz -[root@hh1 html]# ^C
[root@hh1 html]# rz -E
rz waiting to receive.
[root@hh1 html]# ll
total 244
-rw-r--r--. 1 root root 247278 Aug 29 11:02 saolei.zip
[root@hh1 html]#
3.防火墙
[root@hh1 html]# systemctl stop firewalld
[root@hh1 html]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@hh1 html]# vim /etc/selinux/config
SELINUX=disabled
部署Dockerfile
1.在二号虚拟机上下载lrzsz,将Centos7 . repo 、 epel7 . repo 文件拖进虚拟机
[root@hh2 ~]# yum install -y lrzsz
Last metadata expiration check: 2:58:33 ago on Tue 30 Aug 2022 09:25:28 PM CST.
Dependencies resolved.
=====================================================================================
Package Architecture Version Repository Size
=====================================================================================
Installing:
lrzsz x86_64 0.12.20-43.el8 baseos 84 k
Transaction Summary
=====================================================================================
Install 1 Package
Total download size: 84 k
Installed size: 190 k
Downloading Packages:
lrzsz-0.12.20-43.el8.x86_64.rpm 453 kB/s | 84 kB 00:00
-------------------------------------------------------------------------------------
Total 32 kB/s | 84 kB 00:02
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : lrzsz-0.12.20-43.el8.x86_64 1/1
Running scriptlet: lrzsz-0.12.20-43.el8.x86_64 1/1
Verifying : lrzsz-0.12.20-43.el8.x86_64 1/1
Installed products updated.
Installed:
lrzsz-0.12.20-43.el8.x86_64
Complete!
[root@hh2 ~]# rz -E
rz waiting to receive.
[root@hh2 ~]# rz -E
rz waiting to receive.
[root@hh2 ~]# ll
total 12
-rw-------. 1 root root 1093 Jul 21 00:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 2523 Aug 29 11:00 Centos-7.repo
-rw-r--r--. 1 root root 664 Aug 29 11:00 epel-7.repo
[root@hh2 ~]#
2.编辑dockerfile
[root@hh2 ~]# vim Dockerfile
[root@hh2 ~]# cat Dockerfile
FROM centos:7
MAINTAINER liuaojie@example.com
ADD Centos-7.repo /etc/yum.repos.d/
ADD epel-7.repo /etc/yum.repos.d/
RUN yum install -y tomcat unzip curl
WORKDIR /var/lib/tomcat/webapps/
RUN curl -O http://192.168.139.131/saolei.zip && \
unzip saolei.zip && \
mv saolei ROOT
ADD init.sh /init.sh
EXPOSE 8080
CMD ["/bin/bash","/init.sh"]
[root@hh2 ~]# vim init.sh
[root@hh2 ~]# cat init.sh
#!/bin/bash
/usr/libexec/tomcat/server start
[root@hh2 ~]#
3.构建镜像
[root@hh2 ~]# docker build -t saolei:z1 .
Sending build context to Docker daemon 20.99kB
Step 1/10 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
---> eeb6ee3f44bd
Removing intermediate container 06bf8fa3a532
---> 98d4fb4d98b1
Successfully built 98d4fb4d98b1
Successfully tagged saolei:z1
4.运行容器
[root@localhost ~]# docker run -d --name liuaojie -p 8081:8080 saolei:z1
f7fedc387803cb16a9e4da6ec76812ffc2a986a19bb4fdce9d02f194aa3fbf68
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7fedc387803 saolei:z1 "/bin/bash /init.sh" 9 seconds ago Up 8 seconds 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp liuaojie
[root@localhost ~]#
可道云
1.将kodexplo re r4.40.zi p 和 nginx . txt 文件拖至该虚拟机中
root@localhost ~]# rz -E
rz waiting to receive.
[root@localhost ~]# rz -E
rz waiting to receive.
[root@localhost ~]# ll
total 13580
-rw-------. 1 root root 1093 Jul 21 00:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 13894810 Aug 29 11:02 kodexplorer4.40.zip
-rw-r--r--. 1 root root 523 Aug 29 11:02 nginx.txt
[root@localhost ~]#
2.创建/opt/dockerfile/kod目录,将所需的软件包和文件放置该目录下
[root@localhost ~]# mkdir -p /opt/dockerfile/kod/
[root@localhost ~]# cd /opt/dockerfile/kod/
[root@localhost kod]# cp /root/* /opt/dockerfile/kod/
[root@localhost kod]# mv nginx.txt nginx.conf
[root@localhost kod]# ls
anaconda-ks.cfg kodexplorer4.40.zip nginx.conf
[root@localhost kod]#
3.在该目录下编辑dockerfile,使用基础镜像centos:7
[root@localhost kod]# vim dockerfile
[root@localhost kod]# cat dockerfile
FROM centos:7
ADD Centos-7.repo /etc/yum.repos.d/
ADD epel-7.repo /etc/yum.repos.d/
RUN yum -y install nginx php-fpm php-gd php-mbstring unzip && \
sed -i '/^user/c user=nginx' /etc/php-fpm.d/www.conf && \
sed -i '/^group/c group=nginx' /etc/php-fpm.d/www.conf
COPY nginx.conf /etc/nginx/nginx.conf
RUN mkdir /code
WORKDIR /code
COPY kodexplorer4.40.zip .
RUN unzip kodexplorer4.40.zip && \
chown -R nginx.nginx .
ADD init.sh /init.sh
EXPOSE 80
ENTRYPOINT ["/bin/bash","/init.sh"]
[root@localhost kod]# vim init.sh
[root@localhost kod]# cat init.sh
#!/bin/bash
php-fpm -D
echo "$1" >> /etc/nginx/nginx.conf
nginx -g 'daemon off;'
[root@localhost kod]#
4.构建镜像kod:v1
[root@localhost kod]# docker build -t kod:v1 .
Successfully built 69fe1dd9b2c8
Successfully tagged kod:v1
5.运行容器,指定映射端口80:80
[root@localhost kod]# docker run -d --name liuaojie -p 80:80 kod:v1
24c83e38ad771dd83a688b00206c34355ba0b1f21fa0c724c9f9ee1f3b03ac04
[root@localhost kod]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24c83e38ad77 kod:v1 "/bin/bash /init.sh" 7 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp liuaojie
```![请添加图片描述](https://img-blog.csdnimg.cn/694471a608364305a1b80d17f64bfb7d.jpeg)