Dockerfile实例部署和测试

一、扫雷案例

1、所需软件包和环境
Centos-7.repo   epel-7.repo  saolei.zip
server1:部署docker扫雷案例


[root@localhost ~]# ls
anaconda-ks.cfg  Dockerfile   initial-setup-ks.cfg  saolei         saolei.zip
Centos-7.repo    epel-7.repo  init.sh               saolei.tar.gz
[root@localhost ~]#

[root@localhost ~]# yum -y install unzip
CentOS-8.5.2111 - Base - mirrors.aliyun.com                                  33 kB/s | 3.9 kB     00:00
CentOS-8.5.2111 - Extras - mirrors.aliyun.com                                13 kB/s | 1.5 kB     00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com                             81 kB/s | 4.3 kB     00:00
Docker CE Stable - x86_64                                                    78 kB/s | 3.5 kB     00:00
Docker CE Stable - x86_64                                                    20 kB/s |  26 kB     00:01
Package unzip-6.0-46.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost ~]# unzip saolei.zip
Archive:  saolei.zip
   creating: saolei/
   creating: saolei/imgs/
  inflating: saolei/imgs/bai22.jpg
  inflating: saolei/imgs/caidao.jpg
  inflating: saolei/imgs/hei22.jpg
  inflating: saolei/imgs/lei.jpg
  inflating: saolei/imgs/notlei.jpg
  inflating: saolei/imgs/paichu.jpg
  inflating: saolei/imgs/paicuo.jpg
  inflating: saolei/imgs/qipan.jpg
  inflating: saolei/imgs/query.jpg
  inflating: saolei/saolei.jsp
[root@localhost ~]# tar -czf saolei.tar.gz saolei/
[root@localhost ~]# cat Dockerfile
FROM centos:7
MAINTAINER zhan@example.com
ADD Centos-7.repo /etc/yum.repos.d/
ADD epel-7.repo /etc/yum.repos.d/
RUN yum -y install tomcat
WORKDIR /var/lib/tomcat/webapps/
ADD saolei.tar.gz .
RUN mv saolei ROOT

ADD init.sh /init.sh
EXPOSE 8080
CMD ["/bin/bash","/init.sh"]


[root@localhost ~]# cat init.sh
#!/bin/bash
/usr/libexec/tomcat/server start
[root@localhost ~]# docker build -t saolei:v1 .
Sending build context to Docker daemon  782.3kB
Step 1/11 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
 ---> eeb6ee3f44bd
Step 2/11 : MAINTAINER zhan@example.com
[root@localhost ~]# docker run -d -P  saolei:v1
983a43bb03154fc57c96e7fcaeee221ca0735227320f7e95b161a298da6a18d3
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                CREATED         STATUS         PORTS                                         NAMES
983a43bb0315   saolei:v1   "/bin/bash /init.sh"   6 seconds ago   Up 5 seconds   0.0.0.0:49153->8080/tcp, :::49153->8080/tcp   nervous_gates

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BFw5G0EG-1661859572552)(./1661832676186.png)]

server2:部署apache,将saolei.zip软件包上传上去

[root@localhost ~]# dnf -y install httpd
[root@localhost ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# cd /var/www/html/

//使用xftp或dnf -y install lrzsz,将宿主机中的saolei.zip文件拖至该虚拟机中

[root@localhost html]# ls
saolei.zip
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost html]# vim /etc/selinux/config
[root@localhost html]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


[root@localhost html]#

2、编辑Dockerfile,使用基础镜像centos:7

[root@localhost ~]# ls
anaconda-ks.cfg  Centos-7.repo  epel-7.repo  initial-setup-ks.cfg
[root@localhost ~]# vim Dockerfile
[root@localhost ~]# cat Dockerfile
FROM centos:7
MAINTAINER zhan@example.com
ADD Centos-7.repo /etc/yum.repos.d/
ADD epel-7.repo /etc/yum.repos.d/
RUN yum -y install tomcat unzip curl
WORKDIR /var/lib/tomcat/webapps/
RUN curl -O http://192.168.181.159/saolei.zip && \
unzip saolei.zip && \
mv saolei ROOT
ADD init.sh /init.sh
EXPOSE 8080
CMD ["/bin/bash","/init.sh"]
[root@localhost ~]# vim init.sh
[root@localhost ~]# cat init.sh
#!/bin/bash
/usr/libexec/tomcat/server start

3、构建镜像saolei:v1

[root@localhost ~]# docker build -t saolei:v1 .
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
STEP 1/10: FROM quay.io/centos/centos:7
STEP 2/10: MAINTAINER zhan@example.com
--> Using cache e9ab49c3d63a9ff04cba5be9eb4d36f6841d55ad647daba74e2bc185f1bbc50e
--> e9ab49c3d63
STEP 3/10: ADD Centos-7.repo /etc/yum.repos.d/
--> Using cache 921508bf68405505689cf81f6bb9d9c634613b6031588935251207fe9014b129
--> 921508bf684
STEP 4/10: ADD epel-7.repo /etc/yum.repos.d/
--> Using cache 5e29838a35471484e12d133a0ba688f6f71460c1d2718ee0a46172e9c653a624
--> 5e29838a354
STEP 5/10: RUN yum -y install tomcat unzip curl
--> Using cache ae76b332403ddc6608441a5849a32b4fdbf8c8591295c6e3e5574d8a5ef39d75
--> ae76b332403
STEP 6/10: WORKDIR /var/lib/tomcat/webapps/
--> Using cache 3f4730a66817d79f2d0cd48de8754b6dc7132d4f8f6b1af688fb2fe52f200491
--> 3f4730a6681
STEP 7/10: RUN curl -O http://192.168.181.159/saolei.zip && unzip saolei.zip && mv saolei ROOT
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  241k  100  241k    0     0  46.9M      0 --:--:-- --:--:-- --:--:-- 58.9M
Archive:  saolei.zip
   creating: saolei/
   creating: saolei/imgs/
  inflating: saolei/imgs/bai22.jpg
  inflating: saolei/imgs/caidao.jpg
  inflating: saolei/imgs/hei22.jpg
  inflating: saolei/imgs/lei.jpg
  inflating: saolei/imgs/notlei.jpg
  inflating: saolei/imgs/paichu.jpg
  inflating: saolei/imgs/paicuo.jpg
  inflating: saolei/imgs/qipan.jpg
  inflating: saolei/imgs/query.jpg
  inflating: saolei/saolei.jsp
--> 490bda98c6d
STEP 8/10: ADD init.sh /init.sh
--> e7127fb1f4c
STEP 9/10: EXPOSE 8080
--> 6de1c7e201e
STEP 10/10: CMD ["/bin/bash","/init.sh"]
COMMIT saolei:v1
--> 7a079385851
Successfully tagged localhost/saolei:v1
7a079385851065b0b491ff84a44054510d95c119e07ed40a1cf9215a4a8b4b57
[root@localhost ~]#

4、运行容器,指定映射端口8081:8080

[root@localhost ~]# docker run -d --name zlh -p 8081:8080 saolei:v1
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
7a149a02dc425b01695a8a0de92f36ce8e8a285985328a3dcaa37fdc0495fb55
[root@localhost ~]# docker ps
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
CONTAINER ID  IMAGE                COMMAND               CREATED        STATUS            PORTS                   NAMES
7a149a02dc42  localhost/saolei:v1  /bin/bash /init.s...  8 seconds ago  Up 8 seconds ago  0.0.0.0:8081->8080/tcp  zlh

5、使用浏览器访问http://自己docker服务器的ip/saolei.jsp,能够实现扫雷游戏的使用
在这里插入图片描述

二、可道云案例

1、所需软件包
Centos-7.repo epel-7.repo kodexplorer4.40.zip nginx.conf
2、创建/opt/dockerfile/kod目录,将所需的软件包和文件放置该目录下
3、在该目录下编辑dockerfile,使用基础镜像centos:7
4、构建镜像kod:v1
5、运行容器,指定映射端口80:80
6、使用浏览器访问http://自己docker服务器的ip/index.php,能够实现可道云的使用

创建/opt/dockerfile/kod目录,将所需的软件包和文件放置该目录下

[root@localhost ~]# mkdir /opt/dockerfile/kod -p
[root@localhost ~]# cd /opt/dockerfile/kod/
[root@localhost kod]# mv nginx.txt nginx.conf
[root@localhost kod]# ls
Centos-7.repo  Dockerfile  epel-7.repo  file.sh  kodexplorer4.40.zip  nginx.conf


在该目录下编辑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
RUN sed -i '/^user/c user=nginx' /etc/php-fpm.d/www.conf
RUN 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
RUN chown -R nginx.nginx .
ADD file.sh /file.sh
EXPOSE 80
ENTRYPOINT ["/bin/bash","/file.sh"]
[root@localhost kod]# vim file.sh
[root@localhost kod]# cat file.sh
#!/bin/bash
php-fpm -D
echo "$1" >> /etc/nginx/nginx.conf
nginx -g 'daemon off;'

构建镜像kod:v1

[root@localhost kod]# docker build -t kod:v1 .
Sending build context to Docker daemon   13.9MB
Step 1/15 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/15 : ADD Centos-7.repo /etc/yum.repos.d/
 ---> Using cache
 ---> ab49cfdb0f7d
Step 3/15 : ADD epel-7.repo /etc/yum.repos.d/
 ---> Using cache
 ---> 21f1b955d1fd
Step 4/15 : RUN yum -y install nginx php-fpm php-gd php-mbstring unzip
 ---> Using cache
 ---> 7713fcad202c
Step 5/15 : RUN sed -i '/^user/c user=nginx' /etc/php-fpm.d/www.conf
 ---> Using cache
 ---> 5af987b47d79
Step 6/15 : RUN sed -i '/^group/c group=nginx' /etc/php-fpm.d/www.conf
 ---> Using cache
 ---> 5b94f3bd3806
Step 7/15 : COPY nginx.conf /etc/nginx/nginx.conf
 ---> Using cache
 ---> a9738f6809f7
Step 8/15 : RUN mkdir /code
 ---> Using cache
 ---> b4c71fd740b9
Step 9/15 : WORKDIR /code
 ---> Using cache
 ---> d194cec84d7b
Step 10/15 : COPY kodexplorer4.40.zip .
 ---> Using cache
 ---> fbb99d1ee341
Step 11/15 : RUN unzip kodexplorer4.40.zip
 ---> Using cache
 ---> 46b72b6d68ae
Step 12/15 : RUN chown -R nginx.nginx .
 ---> Using cache
 ---> 2ef902a71c66
Step 13/15 : ADD file.sh /file.sh
 ---> bac188af9ce4
Step 14/15 : EXPOSE 80
 ---> Running in 96f8b58375f6
Removing intermediate container 96f8b58375f6
 ---> dac42da503b4
Step 15/15 : ENTRYPOINT ["/bin/bash","/file.sh"]
 ---> Running in 16300a32e168
Removing intermediate container 16300a32e168
 ---> e3a2deeb8ed9
Successfully built e3a2deeb8ed9
Successfully tagged kod:v1

运行容器,指定映射端口80:80

[root@localhost kod]# docker run -dit --name zlh -p 80:80 kod:v1
34f59e2724a6017868efedd6bfa5a5d4998f14713bc28bb9e1dc2c942ed3a7c3
[root@localhost kod]# docker ps
CONTAINER ID   IMAGE     COMMAND                CREATED         STATUS         PORTS                               NAMES
34f59e2724a6   kod:v1    "/bin/bash /file.sh"   6 seconds ago   Up 5 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   zlh
[root@localhost kod]#

浏览器访问测试

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值