srcmesh-workshop / docker-workshop

0 stars 40 forks source link

劉曜宇 #39

Open yauyau93 opened 6 days ago

yauyau93 commented 6 days ago
-----linux環境建立docker compose檔案-----
編輯/建立yml檔案 => vim compose10.yml
啟動編輯模式 => i 
結束編輯 => esc
儲存離開 => :wq

-----建立compose10.yml-----
services:
   mysqldb:
    image: mysql:latest
    container_name: mysqldb_user10
    environment:
      MYSQL_ROOT_PASSWORD: 1
      MYSQL_DATABASE: db_user10
      MYSQL_USER: sa
      MYSQL_PASSWORD: 123456
    volumes:
      - mysql_data:/var/lib/mysql
    logging:
      driver: gelf
      options:
        gelf-address: udp://127.0.0.1:12201
    healthcheck:
      test:
        [
          'CMD-SHELL',
          '<health-check-command> || exit 1',
        ]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped

  wordpress:
    image: wordpress:latest
    container_name: wordpress_user10
    restart: always
    ports:
      - 8810:80
    environment:
      WORDPRESS_DB_HOST: mysqldb
      WORDPRESS_DB_USER: sa
      WORDPRESS_DB_PASSWORD: 123456
      WORDPRESS_DB_NAME: db_user10
    volumes:
      - wordpress:/var/www/html

  nginx:
    image: nginx:latest
    container_name: nginx_user10
    ports:
      - '8010:80'
    depends_on:
      - wordpress
    healthcheck:
      test: ['CMD-SHELL', 'curl -f http://localhost:8010 || exit 1']
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped
    volumes:
      - nginx10.conf:/etc/nginx/nginx.conf

volumes:
  mysql_data:
  wordpress:  
-----建立compose10.yml-----

-----建立nginx10.conf-----
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # Path to access.log & error.log
    access_log /var/log/nginx/access.log  main;
    error_log /var/log/nginx/error.log  warn;

    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    upstream backend {
        # must match the target service name
        server wordpress:80;
    }

    server {
        listen       80;
        location / {
            # $http_host is the host name that users seen on the browser URL
            # and it equals to `HTTP_HOST` request header.
            proxy_set_header Host $http_host;

            # You have to change this according to your setup.
            proxy_pass http://wordpress;

            # Modify `Location` of 301 or 302 HTTP response, so
            # that the browser will follow the correct location.
            proxy_redirect ~^http://[^/]*/(.*) http://$http_host/$1;
        }
    }
}
-----建立nginx10.conf-----

----啟用與測試-----
docker compose -f compose10.yml up -d
curl -vvv http://localhost:8010

檢查wordpress
docker exec -it wordpress_user10 bash
curl -vvv http://localhost:80