Docker containers: how do they work together?

前提是你 提交于 2019-12-10 07:16:04

问题


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.

There are two Dockerfiles, one for Nginx:

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

The nginx.conf is defined as:

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;
  }
}

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

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"

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:

  • 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?

回答1:


  • How does the nginx-container know about the php-container?

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.

  • When PHP is invoked from nginx, which container does the PHP process run in?

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.

  • How is the data passed from nginx to PHP and back?

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

  • 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?

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)

  • How can this docker architecture be scaled for increasing load? Can I use it for production?

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.

  • 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?

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



来源:https://stackoverflow.com/questions/38358933/docker-containers-how-do-they-work-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!