Docker-Compose

Posted by Adam on August 24, 2022
```yml version: '3.1' services: dev-tomcat: image: tomcat:9.0.62-jdk11-openjdk container_name: dev-tomcat restart: always network_mode: "bridge" environment: - LC_ALL=zh_TW.utf8 - TZ=Asia/Taipei - TOMCAT_USERNAME=root - TOMCAT_PASSWORD=changetoyourpassword - SPRING_PROFILE=dev ports: - 192.168.0.254:8081:8080 volumes: - /home/adam/var/tomcat/webapps-dev:/usr/local/tomcat/webapps - /etc/localtime:/etc/localtime:ro #docker-compose -f app.yml -p app up -d ``` ### Nginx Let's Encrypt ```yml version: '3.1' services: note-nginx: image: nginx:latest container_name: note-nginx restart: always networks: - network ports: - "80:80" - "443:443" volumes: - ./nginx/conf.d:/etc/nginx/conf.d - ./nginx/html:/usr/share/nginx/html - ./certbot/conf:/etc/letsencrypt - ./certbot/www:/var/www/certbot # certbot 會新增檔案至 /var/www/certbot 中,並判斷是否能在 your.domain.com 中取得,若順利取得時,便會提供憑證,所以要先建立 nginx 再執行 certbot note-certbot: image: certbot/certbot container_name: note-certbot command: certonly --webroot --webroot-path=/var/www/certbot --email your@email.com --agree-tos --no-eff-email -d your.domain.com networks: - network volumes: - ./certbot/conf:/etc/letsencrypt - ./certbot/www:/var/www/certbot networks: network: # docker-compose -f note.yml -p note up -d ``` 命令方式 ```bash # 申請憑證 docker run -it --rm \ -v "/certbot/conf:/etc/letsencrypt" \ -v "/certbot/www:/var/www/certbot" \ certbot/certbot certonly --webroot --webroot-path=/var/www/certbot --email your@email.com --agree-tos --no-eff-email -d your.domain.com # 更新憑證 docker run -it --rm \ -v "/certbot/conf:/etc/letsencrypt" \ -v "/certbot/www:/var/www/certbot" \ certbot/certbot renew ``` certonly 是用 Certbot 工具申請 Let's Encrypt SSL 憑證的指令,以下是各參數的意義: certonly: 告訴 Certbot 只要產生憑證,不要幫忙安裝憑證。 --webroot: 告訴 Certbot 使用 webroot 方式進行驗證,透過在指定的網站目錄下放置認證文件來進行驗證。 --webroot-path: 告訴 Certbot 認證檔案所在的目錄,例如這裡指定為 /var/www/html。 --email: 設定 Let's Encrypt 的通知信箱。 --agree-tos: 告訴 Certbot 同意 Let's Encrypt 的服務條款。 -d: 告訴 Certbot 要申請 SSL 憑證的網域名稱,可以指定多個,例如這裡指定為 yourdomain.com 和 www.yourdomain.com。 ### 使用已存在的 Network ```yml version: '3' services: app: image: my-app networks: - my-network networks: my-network: external: true ``` ```yml version: '3' services: myapp: image: myapp-image networks: - otherprojectnetwork networks: otherprojectnetwork: external: name: otherproject_default ``` #### 使用外部已存在的 Network ```yml version: '3' services: webapp: image: nginx networks: - existing_network networks: existing_network: external: name: your_existing_network_name ``` ### 設定容器的 log 大小 ```yml version: '3' services: my_service: image: your_image logging: options: max-size: 10m max-file: "7" ``` ### [Ways to set environment variables with Compose](https://docs.docker.com/compose/environment-variables/set-environment-variables/) ``` $ cat .env.dev TAG=v1.5 $ cat compose.yml services: web: image: "webapp:${TAG}" ``` ``` docker compose --env-file ./config/.env.dev up ```