How to correctly link php-fpm and Nginx Docker containers?
Don't hardcode ip of containers in nginx config, docker link adds the hostname of the linked machine to the hosts file of the container and you should be able to ping by hostname.
EDIT: Docker 1.9 Networking no longer requires you to link containers, when multiple containers are connected to the same network, their hosts file will be updated so they can reach each other by hostname.
Every time a docker container spins up from an image (even stop/start-ing an existing container) the containers get new ip's assigned by the docker host. These ip's are not in the same subnet as your actual machines.
see docker linking docs (this is what compose uses in the background)
but more clearly explained in the docker-compose
docs on links & expose
links
links:
- db
- db:database
- redis
An entry with the alias' name will be created in /etc/hosts inside containers for this service, e.g:
172.17.2.186 db
172.17.2.186 database
172.17.2.187 redis
expose
Expose ports without publishing them to the host machine - they'll only be accessible to linked services. Only the internal port can be specified.
and if you set up your project to get the ports + other credentials through environment variables, links automatically set a bunch of system variables:
To see what environment variables are available to a service, run
docker-compose run SERVICE env
.
name_PORT
Full URL, e.g. DB_PORT=tcp://172.17.0.5:5432
name_PORT_num_protocol
Full URL, e.g.
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
name_PORT_num_protocol_ADDR
Container's IP address, e.g.
DB_PORT_5432_TCP_ADDR=172.17.0.5
name_PORT_num_protocol_PORT
Exposed port number, e.g.
DB_PORT_5432_TCP_PORT=5432
name_PORT_num_protocol_PROTO
Protocol (tcp or udp), e.g.
DB_PORT_5432_TCP_PROTO=tcp
name_NAME
Fully qualified container name, e.g.
DB_1_NAME=/myapp_web_1/myapp_db_1
When running Nginx + PHP-FPM in two different containers, can that configuration ever work without sharing a code volume?
I've never seen a setup where Nginx and PHP were running in separate ECS tasks that scaled independently. I've always seen it where they are both running in the same ECS task, with a shared folder.
I wouldn't worry too much about this being "wasteful". You're adding a tiny amount of CPU usage to each Fargate ECS task by adding Nginx to each task. I would focus more on the fact that you are keeping latency as low as possible by running them both in the same task, so Nginx can pass requests to PHP over localhost
.
Docker Nginx connect to PHP-FPM on private docker network
Ok, so my above question IS the correct answer. I was just having trouble with the order of starting containers (I also have reverse proxies and others in play, it got a bit confusing).
For anyone who wants to connect docker nginx to a docker php, this is correct setup:
My php container command (run first):
docker run -d \
--name php7 \
-v /php7:/usr/local/etc \
-v /www_data:/www \
--network=priv-bridge-net \
-p 9000:9000 \
php:7.0.24-fpm
My nginx command:
docker run -d \
--name nginx \
-v /nginx_conf:/etc/nginx \
-v /www_data:/usr/share/nginx/html \
--network=priv-bridge-net \
nginx:1.13.5
My nginx config:
server {
index index.php index.html;
server_name test;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /usr/share/nginx/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php7:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Related Topics
Call to Undefined Method Domdocument::Getelementsbyclassname()
Multiple Replace (Probably Preg_Replace) of Same String with Array
Laravel 3:Looking for Explanation How to Use the Model
Mysqli_Query(): Couldn't Fetch MySQLi Error
Is There a Built-In Function or Plugin to Handle Date Formatting in JavaScript
What Does New Self(); Mean in PHP
How to Output in Cli During Execution of PHP Unit Tests
Algorithm to Get the Excel-Like Column Name of a Number
Change the Date Format in Laravel View Page
How to Get &Curren to Display Literally, Not as an HTML Entity
How the Usort() Sorting Algorithm Works
How to Run the PHP Script at Scheduled Time
How to Retrieve Comments from Within an Xml Document in PHP
How to Know If a User Has Paid for a Subscription
Determine If Business Is Open/Closed Based on Business Hours