前言
上一章介紹了Docker經過多條命令建立啓動運行Docker容器,因而可知這樣一個個去建立單獨的容器也是至關麻煩的,好比要在某個複雜項目中用DB、緩存、消息等等,這樣咱們還要去一個個再建立,爲此這時候須要用上咱們三劍客中的一員大將自動擋的(DockerCompose).Compose是一個用於定義和應用多個容器的工具(鏡像、啓動命令、端口映射等),一條命令即可配置咱們引用中的全部服務.php
Base
Image
指定鏡像名稱或者鏡像Id,若是該鏡像不存在,會嘗試pull下來。css
build
指定Dockerfile文件的路徑html
build: ./dir
mysql
也能夠是一個對象。nginx
build: context: ./dir dockerfile: Dockerfile-alternate args: buildno: 1
git
command
覆蓋容器啓動後默認執行的命令。github
command: bundle exec thin -p 3000
web
該命令也能夠是列表,相似於 dockerfile:redis
command: ["bundle", "exec", "thin", "-p", "3000"]
sql
links
連接到其餘服務器中的容器,能夠指定服務名稱和連接的別名使用SERVICE:ALIAS的形式,或者只指定服務名稱
links: - db - db:database - redis
external_links
表示連接到docker-compose.yml外部的容器,甚至並不是Compose管理的容器,特別是對於那些提供共享容器或共同服務。格式跟links相似
external_links: - redis_1 - project_db_1:mysql - project_db_1:postgresql
ports
暴露端口信息。使用宿主端口:容器端口的格式,或者僅僅指定容器的端口(此時宿主機將會隨機指定端口),相似於docker run -p
ports: - "3000" - "3000-3005" - "8000:8000" - "9090-9091:8080-8081" - "49100:22" - "127.0.0.1:8001:8001" - "127.0.0.1:5000-5010:5000-5010"
expose
暴露端口,只將端口暴露給鏈接的服務,而不暴露給宿主機
expose: - "3000" - "8000"
volumes
卷掛載路徑設置。能夠設置宿主機路徑 (HOST:CONTAINER) 或加上訪問模式 (HOST:CONTAINER:ro)
# Just specify a path and let the Engine create a volume - /var/lib/mysql # Specify an absolute path mapping - /opt/data:/var/lib/mysql # Path on the host, relative to the Compose file - ./cache:/tmp/cache # User-relative path - ~/configs:/etc/configs/:ro # Named volume - datavolume:/var/lib/mysqlvolumes:
volumes_from
從另外一個服務或者容器掛載卷。能夠指定只讀或者可讀寫,若是訪問模式沒有指定,則默認是可讀寫。
volumes_from: - service_name - service_name:ro - container:container_name - container:container_name:rw
environment
設置環境變量。可使用數組或者字典兩種方式。只有一個key的環境變量能夠在運行Compose的機器上找到對應的值,這有助於加密的或者特殊主機的值。
environment: RACK_ENV: development SHOW: 'true' SESSION_SECRET: environment: - RACK_ENV=development - SHOW=true - SESSION_SECRET
env_file
從文件中獲取環境變量,能夠爲單獨的文件路徑或列表。若是經過 docker-compose -f FILE 指定了模板文件,則 env_file 中路徑會基於模板文件路徑。若是有變量名稱與 environment 指令衝突,則以envirment 爲準。
env_file: .env env_file: - ./common.env - ./apps/web.env - /opt/secrets.env
extends
繼承另外一個服務,基於已有的服務進行擴展。
net
設置網絡模式。
net: "bridge" net: "host" net: "none" net: "container:[service name or container name/id]"
dns
配置dns服務器。能夠是一個值,也能夠是一個列表。
dns: 8.8.8.8 dns: - 8.8.8.8 - 9.9.9.9
dns_search
配置DNS的搜索域,能夠是一個值,能夠是一個列表。
dns_search: example.com dns_search: - dc1.example.com - dc2.example.com
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build WORKDIR /src COPY DockerComposeDemo/DockerComposeDemo.csproj DockerComposeDemo/ RUN dotnet restore "DockerComposeDemo/DockerComposeDemo.csproj" COPY . . WORKDIR "/src/DockerComposeDemo" RUN dotnet build "DockerComposeDemo.csproj" -c Release -o /app FROM build AS publish RUN dotnet publish "DockerComposeDemo.csproj" -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "DockerComposeDemo.dll"]
編輯 docker-compose.yml
version: '3.4' services: dockercomposedemoo: image: ${DOCKER_REGISTRY-}dockercomposedemo build: context: . dockerfile: DockerComposeDemo/Dockerfile redis: container_name: redis image: redis ports: # 端口映射 - 6379:6379 nginx: container_name: nginx image: nginx ports: # 端口映射 - 8081:80 volumes: - /Users/fenghui/projects/nginx/nginx.conf:/etc/nginx/nginx.conf
加入了redis和nginx鏡像,直接用nginx進行部署該項目。
上面也體現了端口的映射以及文件的映射,演示的示例是nginx.conf文件映射(切記必定要共享文件夾)
nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #include /etc/nginx/conf.d/*.conf; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #access_log logs/access.log main; #error_log logs/error.log; location / { proxy_pass http://172.17.0.1:8082; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $http_host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
建立一個項目,演示訪問次數,存儲直接用redis存儲
package
using ServiceStack.Redis;
redis test
private readonly RedisManagerPool redisManger = new RedisManagerPool("172.17.0.1:6379"); public void OnGet() { using (var db = redisManger.GetClient()) { ViewData["num"] = db.IncrementValue("count"); } }
aspx
@ViewData["num"]
Run
構建容器
而後打開瀏覽器輸入8081,8081爲nginx代理的端口,同時能夠經過wappalyzer看見咱們的web服務器是nginx。
redis測試成功沒問題
使用dockercompose編排工具進行構建容器是否是方便了好多。
概要