dockerfile 案例

文章目录

    • Nginx
    • 扫雷
      • 布置基础站点
      • 部署 dockerfile
    • 可道云

虚拟机 IP
server1 192.168.91.131
server2 192.168.91.128

Nginx

虚拟机 server1

  1. 使用xftpdnf -y install lrzsz,将宿主机中的Centos-7.repoepel-7.repo文件拖至该虚拟机中
[root@localhost ~]# ls
anaconda-ks.cfg  Centos-7.repo  epel-7.repo
  1. 部署 Dockerfile
[root@localhost ~]# vim Dockerfile

FROM centos:7		//基于基础镜像centos:7
MAINTAINER [email protected]		//维护者信息
ADD Centos-7.repo /etc/yum.repos.d/		//添加Centos-7仓库
ADD epel-7.repo /etc/yum.repos.d/		//添加epel-7仓库
RUN yum -y install nginx		//安装nginx软件包
EXPOSE 80		//暴露指定端口80
CMD ["/usr/sbin/nginx","-g","daemon off;"]
  1. 构建镜像,镜像名称为 nginx:v1
[root@localhost ~]# docker build -t nginx:v1 .
......
Successfully built f53260f22cdc
Successfully tagged nginx:v1
  1. 运行容器,容器名称,映射端口 88:80
[root@localhost ~]# docker run --name kangyimei -itd -p 88:80 nginx:v1
7ad42d3806b2c25eee3cc7743f3448c787ee4b3b7185cf7980a57f9f854994c8

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                               NAMES
7ad42d3806b2   nginx:v1   "/usr/sbin/nginx -g …"   9 seconds ago   Up 7 seconds   0.0.0.0:88->80/tcp, :::88->80/tcp   kangyimei
  1. 进入容器,删除默认网页,写入新的默认网页,网页内容
    通过浏览器,能够正常访问 nginx
[root@localhost ~]# docker exec -it kangyimei /bin/bash
[root@7ad42d3806b2 /]# cd /usr/share/nginx/html/
[root@7ad42d3806b2 html]# ls
404.html  en-US  img         nginx-logo.png
50x.html  icons  index.html  poweredby.png
[root@7ad42d3806b2 html]# rm -rf index.html 
[root@7ad42d3806b2 html]# echo kangyimei > index.html

dockerfile 案例_第1张图片

扫雷

布置基础站点

虚拟机 server2

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

[root@localhost ~]# dnf -y install httpd
[root@localhost ~]# systemctl enable --now httpd

[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
[root@localhost html]# vi /etc/selinux/config 
......
SELINUX=disabled
......

部署 dockerfile

虚拟机 server1

  1. 编辑 Dockerfile,使用基础镜像 centos:7

使用xftpdnf -y install lrzsz,将宿主机中的Centos-7.repoepel-7.repo文件拖至该虚拟机中

[root@localhost ~]# ls
anaconda-ks.cfg  Centos-7.repo  epel-7.repo

[root@localhost ~]# vim Dockerfile
FROM centos:7
MAINTAINER [email protected]
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.91.128/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
#!/bin/bash
/usr/libexec/tomcat/server start
  1. 构建镜像 saolei:v1
[root@localhost ~]# docker build -t saolei:v1 .
......
Successfully built 499c33f39570
Successfully tagged saolei:v1
  1. 运行容器,指定映射端口 8081:8080,容器名
[root@localhost ~]# docker run --name kangyimei -d -p 8081:8080 saolei:v1 
4599e8ae7272fb0fc77ac4df744570370735ef9baba108fb9d62c7f29037d02f

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                CREATED         STATUS         PORTS                                       NAMES
4599e8ae7272   saolei:v1   "/bin/bash /init.sh"   4 seconds ago   Up 3 seconds   0.0.0.0:8081->8080/tcp, :::8081->8080/tcp   kangyimei
  1. 使用浏览器访问
    dockerfile 案例_第2张图片

可道云

虚拟机 server1

使用xftpdnf -y install lrzsz,将宿主机中的Centos-7.repoepel-7.repokodexplorer4.40.zipnginx.txt文件拖至该虚拟机中

  1. 创建 /opt/dockerfile/kod 目录,将所需的软件包和文件放置该目录下
[root@localhost ~]# ls
anaconda-ks.cfg  Centos-7.repo  epel-7.repo  kodexplorer4.40.zip  nginx.txt

[root@localhost ~]# mkdir -p /opt/dockerfile/kod/
[root@localhost ~]# cd /opt/dockerfile/kod/

[root@localhost kod]# cp /root/* /opt/dockerfile/kod
[root@localhost kod]# rm -f anaconda-ks.cfg 
[root@localhost kod]# mv nginx.txt nginx.conf

[root@localhost kod]# ls
Centos-7.repo  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; 
		}
	} 
}
  1. 在该目录下编辑 dockerfile,使用基础镜像 centos:7
[root@localhost kod]# vim 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
#!/bin/bash
php-fpm -D 
echo "$1" >> /etc/nginx/nginx.conf
nginx -g 'daemon off;' 
  1. 构建镜像 kod:v1
[root@localhost kod]# docker build -t kod:v1 .
......
Successfully built 82623b59eb18
Successfully tagged kod:v1
  1. 运行容器,指定映射端口 80:80,容器名
[root@localhost kod]# docker run --name kangyimei -d -p 80:80 kod:v1 '#runtime'
a86388b6d525940ce1c0a42c8c205215bc0ee235efd8f1455533bc11e2afdc7b

[root@localhost kod]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
a86388b6d525   kod:v1    "/bin/bash /init.sh …"   45 seconds ago   Up 44 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   kangyimei

[root@localhost kod]# docker exec -it kangyimei /bin/bash
[root@a86388b6d525 code]# tail -1 /etc/nginx/nginx.conf
#runtime
  1. 使用浏览器访问
    dockerfile 案例_第3张图片

你可能感兴趣的:(docker,linux,运维)