沙滩星空的博客沙滩星空的博客

docker-compose.yml 配置文件详解

在docker-compose.yml文件中command执行多条命令

command:
  - /bin/sh
  - -c
  - |
      bundle config mirror.https://rubygems.org https://gems.ruby-china.org
      bundle exec rake redmine:plugins:migrate RAILS_ENV=production
      bundle exec rake tmp:cache:clear tmp:sessions:clear RAILS_ENV=production
      /docker-entrypoint.sh passenger start

或:

command: /bin/sh -c "cd /www/vest/ && php easyswoole start"
https://segmentfault.com/q/1010000014461396
https://github.com/docker-library/redmine/issues/52

version: '3'

networks:
  cm_net:
#    driver: bridge
#    driver_opts:
#      com.docker.network.enable_ipv6: "true"
    ipam:
      driver: default
      config:
         - subnet: 172.19.0.0/16
 #       gateway: 172.17.0.1

services:
  cm_mysql57:
    image: mysql:5.7
    command:
      - /bin/sh
      - -c
      - |
        docker-entrypoint.sh mysqld
        mysql_upgrade
    networks:
      cm_net:
        ipv4_address: 172.19.0.11
        aliases:
           - mysql57net
    environment:
       - MYSQL_ROOT_PASSWORD=root
    ports:
       - "3306:3306"
    volumes:
       - "/mnt/www:/mnt/www"
  cm_phpfpm72:
    image: "php:7.2-fpm"
    command: docker-php-entrypoint php-fpm
#    build: 
#     context: ./php/
    networks:
      cm_net:
        ipv4_address: 172.19.0.12
        aliases:
           - phpfpm72net
#在nginx中可使用别名phpfpm72net访问此容器
    extra_hosts:
       - "api.catmes.test:172.19.0.13"
       - "symfony.test:172.19.0.13"
    ports:
       - "9000:9000"
    volumes:
       - "/mnt/www:/mnt/www"
#    links:
#     - cm_mysql57:mysql
    depends_on:
       - cm_mysql57
  cm_nginx:
    image: "nginx"
    networks:
      cm_net:
        ipv4_address: 172.19.0.13
        aliases:
           - nginxnet  
#       - api.catmes.test
#       - symfony.test
#这边自定义两个域名作为别名,方便本地测试时,PHP能正确访问Nginx
    ports:
       - "80:80"
    volumes:
       - "/mnt/www:/mnt/www"
       - "/mnt/vhost:/etc/nginx/conf.d"
#    links:
#     - cm_phpfpm72:phpfpm
    depends_on:
       - cm_phpfpm72
#上面的mysql php nginx 三个服务,必须都加入自定义网络networks

Nginx虚拟主机配置文件:

server {
    listen 80;
    server_name test.woodsmanfood.com;
    root "/mnt/www/test_woodsmanfood_com/public_html";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/homestead.test-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_index  index.php;
        fastcgi_pass   phpfpm56net:9000;
#        fastcgi_pass   127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
#如果你的根目录和php容器的根目录不一样,这里的$document_root需要换成你php下的根目录,不然php就找不到文件了
        include        fastcgi_params;

        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }
}

给容器指定具体IP时报错,提示必须先配置子网连接的解决方式:

user specified IP address is supported only when connecting to networks with user configured subnets

docker-compose方式,networks参照如下配置:

networks:
  cm_net:
#    driver: bridge
#    driver_opts:
#      com.docker.network.enable_ipv6: "true"
    ipam:
      driver: default
      config:
         - subnet: 172.19.0.0/16
 #       gateway: 172.17.0.1

原始的docker命令方式:
创建一个新的局域网虚拟网络:

docker network create --driver bridge --subnet 172.19.0.0/16 cm_net

新增容器时,加入网络参数--network=cm_net

docker run -d --name cm_nginx -p 80:80 --network=cm_net nginx:last

docker-compose自定义网络,固定容器ip地址
https://blog.csdn.net/hechaojie_com/article/details/83625265

Docker为网络bridge模式指定容器ip的方法http://www.cppcns.com/jiqiao/fuwuqi/168920.html

未经允许不得转载:沙滩星空的博客 » docker-compose.yml 配置文件详解

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址