Bartosz Ziembański
Bartosz Ziembański

Reputation: 9

Deploying docker images to heroku

I have 2 docker images (one for nginx and one for php). I'm pushing and releasing them to docker using Docker CLI. Push and release action give no error, but when I look at logs I can see that nginx crushes immediately.

logs:

2021-01-12T21:01:01.531944+00:00 heroku[web.1]: Starting process with command `nginx -g daemon\ off\;`
2021-01-12T21:01:03.415460+00:00 heroku[php.1]: Starting process with command `php-fpm`
2021-01-12T21:01:04.046397+00:00 heroku[php.1]: State changed from starting to up
2021-01-12T21:01:04.169516+00:00 app[web.1]: 2021/01/12 21:01:04 [warn] 3#3: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
2021-01-12T21:01:04.169529+00:00 app[web.1]: nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
2021-01-12T21:01:04.222513+00:00 app[web.1]: 2021/01/12 21:01:04 [emerg] 3#3: host not found in upstream "php" in /etc/nginx/conf.d/default.conf:16
2021-01-12T21:01:04.222514+00:00 app[web.1]: nginx: [emerg] host not found in upstream "php" in /etc/nginx/conf.d/default.conf:16
2021-01-12T21:01:04.302526+00:00 heroku[web.1]: Process exited with status 1
2021-01-12T21:01:04.347702+00:00 heroku[web.1]: State changed from starting to crashed

Dockerfile.php

FROM php:7.4.3-fpm-alpine3.11

ENV RUN_DEPS \
    zlib \
    libzip \
    libpng \
    libjpeg-turbo \
    postgresql-libs

ENV BUILD_DEPS \
    zlib-dev \
    libzip-dev \
    libpng-dev \
    libjpeg-turbo-dev \
    postgresql-dev

ENV PHP_EXTENSIONS \
    opcache \
    zip \
    gd \
    bcmath \
    pgsql \
    pdo_pgsql

RUN apk add --no-cache --virtual .build-deps $BUILD_DEPS \
    && docker-php-ext-configure gd --with-jpeg \
    && docker-php-ext-install -j "$(nproc)" $PHP_EXTENSIONS \
    && apk del .build-deps

RUN apk add --no-cache --virtual .run-deps $RUN_DEPS
# Copy the application code
COPY . /app

VOLUME ["/app"]

Dockerfile.web

FROM nginx:1.17.8-alpine

# Copy the public directory
COPY ./public/ /app/public/
COPY . /app/

# Copy the nginx config file
COPY ./docker/nginx/nginx.conf /etc/nginx/conf.d/default.conf

nginx.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    server_tokens off;

    root /app/;
    index index.php;

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

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

I honestly have no idea why is it happening. This is my first time using Heroku and Docker. Locally I use docker-compose file to build it, but I know they are not supported by Heroku.

Upvotes: 0

Views: 607

Answers (1)

Beppe C
Beppe C

Reputation: 13983

The problem is here: host not found in upstream "php"

The incoming NGINX requests are proxied to php:9000 which is fine on your local Docker-Compose as the 2 containers share the same network.
On Heroku each container is a Web Dyno: dynos cannot communicate via an internal network but they only accept HTTPS requests from outside (public internet).

You can look at different options:

  1. use a buildpack instead of Docker: Customizing Web Server and Runtime Settings for PHP
  2. create a single image with PHP and NGINX (DockerHub has a few)
  3. Use the URL of the PHP app dyno (https://php.herokuapp.com) in the fastcgi_pass configuration

Upvotes: 1

Related Questions