我的docker隨筆21:web 服務器部署

本文記錄幾種不同的web服務器部署,其過程大同小異。

技術總結:
1、選擇鏡像,建議體積越小越好。確認宿主機目錄,將其掛載到容器存放 html 文件的目錄。
2、可以將 html 文件拷貝到鏡像中重新運行,但此法不太方便。
3、容器內的目錄:

  • nginx:/usr/share/nginx/html
  • httpd:/usr/local/apache2/htdocs/
  • tomcat: /usr/local/tomcat/webapps/ROOT
  • php:/var/www/

nginx部署

要點:選擇nginx:alpine版本,體積小。

docker-compose 文件:

version: "2"
services:
  nginx_all:
    image: nginx:alpine
    container_name: nginx_all
    volumes:
      - $PWD/nginx:/etc/nginx
    ports:
      - 8080:80
    networks:
      - mywebsite
  web1:
    image: latelee/nginx
    container_name: web1
    volumes:
      - $PWD/html1:/usr/share/nginx/html
    ports:
      - 8081:80
    networks:
      - mywebsite

  web2:
    image: latelee/nginx
    container_name: web2
    volumes:
      - $PWD/html2:/usr/share/nginx/html
    ports:
      - 8082:80
    networks:
      - mywebsite
networks:
    mywebsite:
      driver: bridge

主頁示例:

cat XX/index.html
<html>
  <body>
    <h2>hello world</h2>
  </body>
</html>

httpd

httpd實際是apache。

version: "2"
services:
  web1:
    image: latelee/httpd
    container_name: web1
    volumes:
      - $PWD/html1:/usr/local/apache2/htdocs/
    ports:
      - 8081:80
    networks:
      - mywebsite
  web2:
    image: latelee/httpd
    container_name: web2
    volumes:
      - $PWD/html2:/usr/local/apache2/htdocs/
    ports:
      - 8082:80
    networks:
      - mywebsite
networks:
    mywebsite:
      driver: bridge

tomcat

version: '2'
services:
    tomcat:
        image: tomcat:8.0.51-jre8-slim
        container_name: tomcat
        #restart: always
        volumes:
            - $PWD/webapps:/usr/local/tomcat/webapps/ROOT
        ports:
            - "8080:8080"
$ cat webapps/index.php
<html>
  <body>
    <h2>THis is tomcat test</h2>
    <p> PHP </p>
    <p> 2018 5 5 </p>
  </body>
</html>

php

version: "2"
services:
  php:
    image: php:7.2.7-apache
    container_name: php
    restart: always
    volumes:
      - ./www:/var/www/
    ports:
      - 5000:80
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章