0

I've taken on a project built on Docker containers and I'm having it to run smoothly.

My containers build successfully, but when I try getting to the website, nginx gives me a 502 with this error in the logs:

connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.17.0.6:9000", host: "localhost:2000"

Which, from what I've read would be a problem with the link between my two containers.

I've tried changing the listen parameter of php-fpm directly to 0.0.0.0:9000 as seen on Nginx+PHP-FPM: connection refused while connecting to upstream (502) but this gave rise to a new error I don't fully understand either:

*11 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.17.0.6:9000", host: "localhost:2000"

Does anyone has an idea of what is failing and how to fix it ?

The docker-compose part regarding theses two services is:

elinoi-webserver:
  build: .
  dockerfile: docker/Dockerfile.nginx.conf
  container_name: elinoi-webserver
  volumes:
      - .:/var/www/elinoi.com
  ports:
   - "2000:80"
  links:
   - elinoi-php-fpm

elinoi-php-fpm:
  build: .
  dockerfile: docker/Dockerfile.php-fpm.conf
  container_name: elinoi-php-fpm
  volumes:
    - .:/var/www/elinoi.com
    - /var/docker_volumes/elinoi.com/shared:/var/www/elinoi.com/shared
  ports:
   - "22001:22"
  links:
    - elinoi-mailhog
    - elinoi-memcached
    - elinoi-mysql
    - elinoi-redis

The nginx conf file is:

server {
    listen 80 default;

    root /var/www/elinoi.com/current/web;

    rewrite ^/app\.php/?(.*)$ /$1 permanent;

    try_files $uri @rewriteapp;

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    # Deny all . files
    location ~ /\. {
        deny all;
    }

    location ~ ^/(app|app_dev)\.php(/|$) {
        fastcgi_pass elinoi-php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index app.php;
        send_timeout 1800;
        fastcgi_read_timeout 1800;
    }

    # Statics
        location /(bundles|media) {
        access_log off;
        expires 30d;
        try_files $uri @rewriteapp;
    }

}

The Dockerfile for the elinoi-php-fpm service is :

FROM phpdockerio/php7-fpm:latest
# Install selected extensions
RUN apt-get update \
    && apt-get -y --no-install-recommends install php7.0-memcached php7.0-mysql php7.0-redis php7.0-gd php7.0-imagick php7.0-intl php7.0-xdebug php7.0-mbstring \
    && apt-get -y --no-install-recommends install nodejs npm nodejs-legacy vim ruby-full git build-essential libffi-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN npm install -g bower
RUN npm install -g less
RUN gem install sass

# If you're using symfony and the vagranted environment, I strongly recommend you change your AppKernel to use the following temporary folders
# for cache, logs and sessions, otherwise application performance may suffer due to these being shared over NFS back to the host
RUN mkdir -p "/tmp/elinoi/cache" \
    && mkdir -p "/tmp/elinoi/logs" \
    && mkdir -p "/tmp/elinoi/sessions" \
    && chown www-data:www-data -R "/tmp/elinoi"

RUN apt-get update \
    && apt-get -y --no-install-recommends install openssh-server \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22
ADD docker/.ssh /root/.ssh
RUN chmod 700 /root/.ssh/authorized_keys

CMD ["/usr/sbin/sshd", "-D"]

WORKDIR "/var/www/elinoi.com"

The Dockerfile for the elinoi-webserver is:

FROM smebberson/alpine-nginx:latest

COPY /docker/nginx.conf /etc/nginx/conf.d/default.conf

WORKDIR "/var/www/elinoi.com"

1 Answer 1

1

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

The original Dockerfile ends with:

CMD /usr/bin/php-fpm

and the Dockerfile of the elinoi-php-fpm service ends with the following CMD layer:

CMD ["/usr/sbin/sshd", "-D"]

So, only sshd is started after container creates. php-fpm is not started there.

That's why nginx constantly returns 502 error, because php backend is not working at all.

You can fix your issue the following ways:

1. Docker Alpine linux running 2 programs

2. Simply delete sshd part from the elinoi-php-fpm service.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Nickolay, I was beginning to give up on this issue. You were right, removing this line solved the problem (leading to another but more familiar to me so I'll dig into it ;) ). I'll look into your doc link if I think I need to actually use sshd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.