Docker 容器:它们如何协同工作? [英] Docker containers: how do they work together?

查看:44
本文介绍了Docker 容器:它们如何协同工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用 docker 并构建了一个工作示例,如 https://codeable 中所示.io/wordpress-developers-intro-docker.由于部署将在嵌入式系统上进行,因此我需要非常小的 docker 容器占用空间.

I have started working with docker and built a working example as seen in https://codeable.io/wordpress-developers-intro-docker. I need a quite small footprint of the docker containers since the deployment will be on an emebedded system.

但我不知道这是如何组合在一起的.

But I have no clue about how this fits together.

有两个 Dockerfile,一个用于 Nginx:

There are two Dockerfiles, one for Nginx:

FROM nginx:1.9-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf

nginx.conf 定义为:

server {
  server_name _;
  listen 80 default_server;

  root   /var/www/html;
  index  index.php index.html;

  access_log /dev/stdout;
  error_log /dev/stdout info;

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

  location ~ .php$ {
    include fastcgi_params;
    fastcgi_pass my-php:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

另一个 Dockerfuile 用于 PHP:

The other Dockerfuile is for PHP:

Dockerfile.php-fpm:
FROM php:7.0.6-fpm-alpine
RUN docker-php-ext-install -j$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) 
    iconv gd mbstring fileinfo curl xmlreader xmlwriter spl ftp mysqli
VOLUME /var/www/html

最后所有东西都在 docker-compose.yml 中:

Finally everything comes together in a docker-compose.yml:

version: '2'
services:
  my-nginx:
    build: .
    volumes:
      - .:/var/www/html
    ports:
      - "8080:80"
    links:
      - my-php
  my-php:
    build:
      context: .
      dockerfile: Dockerfile.php-fpm
    volumes:
      - .:/var/www/html
    ports:
      - "9000:9000"

使用 docker 容器启动

The docker containers are started up using

$ docker-compose build
$ docker-compose up

一切正常 - 这是一种魔法!

And everything works - it's a kind of magic!

这里是(一些)我的问题,以了解正在发生的事情:

Here are (some of) my questions to understand what's going on:

  • nginx-container 如何知道 php-container?
  • 从 nginx 调用 PHP 时,哪个容器执行PHP 进程运行在什么地方?
  • 数据如何从 nginx 传递到 PHP 并返回?
  • 这是 docker 用法吗(3 个容器用于一个简单的Web 服务器应用程序)使用 docker 的正确方法或这是对容器的过度杀伤吗?
  • 如何扩展此 docker 架构以增加加载?我可以将其用于生产吗?
  • 容器在主机上使用相同的卷 (./).当使用 PHP 框架作为 Yii2 时,会不会更好将卷移动到 PHP 或 Nginx 容器?
  • How does the nginx-container know about the php-container?
  • When PHP is invoked from nginx, which container does the PHP process run in?
  • How is the data passed from nginx to PHP and back?
  • Is this docker usage (3 containers for a simple web server application) the right way to use docker or is this an overkill of containers?
  • How can this docker architecture be scaled for increasing load? Can I use it for production?
  • The containers use the same volume (./) on the host. When using a PHP Framework as Yii2, wouldn't it better to move the volume to either the PHP or Nginx container?

推荐答案

  • nginx-container 如何知道 php-container?
  • links 下,您列出了 my-php 容器,这会在容器名称和 中的 IP 之间创建映射/etc/hosts 文件.

    Under links you listed the my-php container, this, among other things, creates a mapping between the name of the container and it's IP in the /etc/hosts file.

    • 从 nginx 调用 PHP 时,PHP 进程运行在哪个容器中?
    • 如您所料,任何 php 代码都将在 my-php 容器中运行,这是在 nginx 配置文件中定义的,它将请求的处理传递给在 my-php:9000 上运行的 php 引擎.

      As you would expect, any php code will run in the my-php container, this is defined in the nginx config file, which passes the processing of requests to the php engine running on my-php:9000.

      • 数据如何从 nginx 传递到 PHP 并返回?
      • 通过常规的套接字通信.两个 docker 都有自己的地址,并且可以像连接到网络的任何其他计算机一样相互通信.

        Over regular socket communication. Both dockers have their addresses, and they can communicate with each other, like any other computer connected to the network.

        • 这种 docker 用法(一个简单的 Web 服务器应用程序使用 3 个容器)是使用 docker 的正确方法还是容器的过度使用?
        • 我在这里只看到 2 个容器.有些人会说一个容器应该只运行一个进程(就像这里,所以你已经构建了最小的系统),还有一些人说每个容器应该运行服务需要的任何东西.(不过这个是个人喜好问题,众说纷纭)

          I only see 2 containers here. There are some who would say a container should only run one process (like here, so you have built the minimal system), and there are some who say each container should run whatever the service needs. (this however is a matter of preference, and there are different opinions on the matter)

          • 如何扩展此 docker 架构以增加负载?我可以将其用于生产吗?
          • 是的,您可以将其用于生产.它可以轻松扩展,但为了实现扩展,您缺少一些部分来平衡负载.(例如,一个负载均衡器可以向一个不忙的实例发送新请求.一个非常常用的工具是 HAProxy.

            Yes, you could use it for production. It can scale easily, but in order to achieve scale you are missing some pieces to balance the load. (e.g. a load balancer that can send new requests to an instance which isn't already busy. A very common tool for this is HAProxy.

            • 容器在主机上使用相同的卷 (./).使用 PHP 框架作为 Yii2 时,将卷移动到 PHP 或 Nginx 容器不是更好吗?
            • 由于 PHP 容器在这种情况下完成所有处理,因此仅将卷挂载在 my-php 上应该是安全的.

              As the PHP container does all the processing in this case, it should be safe to only mount the volume on my-php.

              这篇关于Docker 容器:它们如何协同工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆