构建扫雷小游戏
先准备软件包环境
**下载centos 7 软件包 可以使用mv移动到所使用的当前目录中**
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
**epel-7 包**
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
这里的 Centos-7.repo epel-7.repo 只需用mv重命名修改下既可
[root@localhost ~]# ls
anaconda-ks.cfg Centos-7.repo Dockerfile epel-7.repo pass
//在/root目录下构建程序
[root@localhost ~]# vim Dockerfile
//具体不懂的参数可参见上一篇文章
[root@localhost ~]# cat Dockerfile
FROM centos:7
MAINTAINER chenyu@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.47.136/saolei.zip && \
unzip saolei.zip && \
mv saolei ROOT
ADD init.sh /init.sh
EXPOSE 8080
CMD ["/bin/bash","/init.sh"]
创建运行脚本,以便容器识别
[root@docker ~]# cat init.sh
#!/bin/bash
/usr/libexec/tomcat/server start
[root@localhost ~]# docker build -t saolei:v1 /root //解析Dockerfile程序,并命名镜像为saolei:v1
Sending build context to Docker daemon 45.57kB
Step 1/10 : FROM centos:7
---> eeb6ee3f44bd
Step 2/10 : MAINTAINER chenyu@example.com
---> Using cache
---> 39f11e1c124c
Step 3/10 : ADD Centos-7.repo /etc/yum.repos.d/
---> Using cache
---> 4def3aa95a05
Step 4/10 : ADD epel-7.repo /etc/yum.repos.d/
---> Using cache
---> 4766a89ae59b
Step 5/10 : RUN yum -y install tomcat unzip curl
---> Using cache
---> 60f1f0c1598d
Step 6/10 : WORKDIR /var/lib/tomcat/webapps/
---> Using cache
---> f16f913166e6
Step 7/10 : RUN curl -O http://192.168.47.136/saolei.zip && unzip saolei.zip && mv saolei
---> Running in 8e8242fc6a44
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 241k 100 241k 0 0 13.9M 0 --:--:-- --:--:-- --:--:-- 14.7M
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
Removing intermediate container 8e8242fc6a44
---> 232c92833aff
Step 8/10 : ADD init.sh /init.sh
---> f5d3c34fc3a1
Step 9/10 : EXPOSE 8080
---> Running in a62e98f6691c
Removing intermediate container a62e98f6691c
---> 0cd6983e64ac
Step 10/10 : CMD ["/bin/bash","/init.sh"]
---> Running in 7bb90727ee2c
Removing intermediate container 7bb90727ee2c
---> 9ac43a75fdf3
Successfully built 9ac43a75fdf3
Successfully tagged saolei:v1
[root@localhost ~]# ls
anaconda-ks.cfg Centos-7.repo Dockerfile epel-7.repo init.sh pass
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
saolei v1 9ac43a75fdf3 About a minute ago 640MB
centos 7 eeb6ee3f44bd 11 months ago 204MB
运行容器,并印射8081端口
[root@localhost ~]#
docker run -it --name liutianyang -p 8081:8080 9ac43a75fdf3
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a9cdbcbb5f8 9ac43a75fdf3 "/bin/bash /init.sh" 2 minutes ago Up About a minute 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp liutianyang
[root@localhost ~]# docker exec -it liutianyang /bin/bash
[root@1a9cdbcbb5f8 webapps]# ls
ROOT saolei.zip
[root@1a9cdbcbb5f8 webapps]# cd ROOT/
[root@1a9cdbcbb5f8 ROOT]# ls
imgs saolei.jsp
通过宿主机ip加端口加服务名访问
搭建私人有道云网页
创建存放服务目录
[root@localhost ~]# mkdir -p /opt/dockerfile/kod
[root@localhost kod]# ls
Centos-7.repo Dockerfile epel-7.repo kodexplorer4.40.zip
//编写nginx服务
[root@localhost kod]# vim nginx.conf
[root@localhost kod]# ls
Centos-7.repo Dockerfile epel-7.repo kodexplorer4.40.zip nginx.conf
[root@localhost kod]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /code;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /code$fastcgi_script_name;
include fastcgi_params;
}
}
}
存放脚本
[root@localhost kod]# cat init.sh
#!/bin/bash
php-fpm -D
echo "$1" >> /etc/nginx/nginx.conf
nginx -g 'daemon off;'
编写Dockerfile文件
[root@localhost kod]# cat Dockerfile
FROM centos:7
MAINTAINER chenyu@example.com
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 init.sh /init.sh
EXPOSE 80
ENTRYPOINT ["/bin/bash","/init.sh"]
查看docker build 构建镜像的具体过程
[root@localhost kod]# docker history kod:v1
IMAGE CREATED CREATED BY SIZE COMMENT
c48cfefd1057 14 minutes ago /bin/sh -c #(nop) ENTRYPOINT ["/bin/bash" "… 0B
fb0128eb850c 14 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
5069ac75d17c 14 minutes ago /bin/sh -c #(nop) ADD file:806290505d698c672… 85B
d13ebe9f627f 14 minutes ago /bin/sh -c chown -R nginx.nginx . 46.4MB
eb0438e79619 15 minutes ago /bin/sh -c unzip kodexplorer4.40.zip 32.5MB
a427a4a5fb15 15 minutes ago /bin/sh -c #(nop) COPY file:4112a246afbb27df… 13.9MB
954e4692cf7a 15 minutes ago /bin/sh -c #(nop) WORKDIR /code 0B
9366023d9a6c 15 minutes ago /bin/sh -c mkdir /code 0B
0f82ac2cbbb7 15 minutes ago /bin/sh -c #(nop) COPY file:d4f0c5bb0f9d0556… 467B
adb065872685 15 minutes ago /bin/sh -c sed -i '/^group/c group=nginx' /e… 10kB
acc057a5dd59 15 minutes ago /bin/sh -c sed -i '/^user/c user=nginx' /etc… 10kB
eed0794eb5d0 15 minutes ago /bin/sh -c yum -y install nginx php-fpm php-… 302MB
4766a89ae59b About an hour ago /bin/sh -c #(nop) ADD file:d0da97a219d8eef1a… 664B
4def3aa95a05 About an hour ago /bin/sh -c #(nop) ADD file:b9851c841fb743c7b… 2.52kB
39f11e1c124c About an hour ago /bin/sh -c #(nop) MAINTAINER chenyu@example… 0B
eeb6ee3f44bd 11 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 11 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 11 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
//构建完成镜像
[root@localhost kod]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kod v1 c48cfefd1057 54 seconds ago 599MB
[root@localhost ~]# docker run -d --name liutianyang1 -p 80:80 c48cfefd1057
[root@localhost kod]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b35adc3d904 c48cfefd1057 "/bin/bash /init.sh" 40 minutes ago Up 40 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp liutianyang1
[root@localhost kod]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8081 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
//进入容器
[root@localhost ~]# docker exec -it liutianyang1 /bin/bash
[root@5b35adc3d904 code]# ls
ChangeLog.md app data kodexplorer4.40.zip static
README.MD config index.php plugins
[root@5b35adc3d904 code]# car index.php
bash: car: command not found
[root@5b35adc3d904 code]# cat index.php
<?php
ob_start();
include ('config/config.php');
$app = new Application();
init_config();
$app->run();
?>
[root@5b35adc3d904 code]# ll kodexplorer4.40.zip
-rw-r--r-- 1 nginx nginx 13894810 Aug 30 13:37 kodexplorer4.40.zip
访问网页