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

查看:241
本文介绍了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容器如何了解php容器?
  • 从nginx调用PHP时,哪个容器会 PHP进程在运行吗?
  • 如何将数据从nginx传递到PHP并传递回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容器如何了解php容器?
  • links下,您列出了my-php容器,除其他外,这在容器名称和/etc/hosts文件中的IP之间创建了映射.

    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并传递回PHP?
      • 通过常规套接字通信.这两个码头工人都有自己的地址,并且可以彼此通信,就像连接到网络上的任何其他计算机一样.

        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 .

            • 这些容器在主机上使用相同的卷(./).当使用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天全站免登陆