2

I am tring to use docker because I will need it at work for laravel, and I am trying to get used to it. I created the dockerfile and pasted this from a tutorial that I found online:

FROM php:5.6.30-fpm-alpine

RUN apk update && apk add build-base

RUN apk add postgresql postgresql-dev \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
  && docker-php-ext-install pdo pdo_pgsql pgsql

RUN apk add zlib-dev git zip \
  && docker-php-ext-install zip

RUN curl -sS https://getcomposer.org/installer | php \
        && mv composer.phar /usr/local/bin/ \
        && ln -s /usr/local/bin/composer.phar /usr/local/bin/composer

COPY . /app
WORKDIR /app

RUN composer install --prefer-source --no-interaction

ENV PATH="~/.composer/vendor/bin:./vendor/bin:${PATH}"

I created the docker-compose.yml file and pasted this:

version: '2'
services:
  nginx:
    image: nginx:1.11.10-alpine
    ports:
      - 3000:80
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
  web:
    build: .
    ports:
      - 9000:9000
    volumes:
      - .:/app
      - /app/vendor
    depends_on:
      - postgres
    environment:
      DATABASE_URL: postgres://todoapp@postgres/todos
  postgres:
    image: postgres:9.6.2-alpine
    environment:
      POSTGRES_USER: todoapp

  POSTGRES_DB: todos

I do docker-compose up-d and it seems to be working, but if I try to do docker-compose ps I don'thave any ports on row one, and on state I have Exit 1

enter image description here

When I try to go to localhost:3000/todos it says that the page is not found. If i try to do curl localhost:3000/todos it says that the connection is refused.... What do I do?

EDIT:

./nginx.conf:

server {
  listen  80;
  error_log  /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;
  root /app/public/;

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

  location ~ ^/.+\.php(/|$) {
      fastcgi_pass web:9000;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

and the logs: enter image description here

text:

nginx_1     | 2019/02/13 14:30:19 [emerg] 1#1: host not found in upstream "web" in /etc/nginx/conf.d/default.conf:12
nginx_1     | nginx: [emerg] host not found in upstream "web" in /etc/nginx/conf.d/default.conf:12
  • 1
    Can you do `docker-compose logs nginx` and paste the output here? Also please paste your whole docker-compose.yaml, or at least not cut in the middle of service definition and with version? – michalhosna Feb 13 '19 at 12:26
  • 1
    Also `./nginx.conf` would be useful. – michalhosna Feb 13 '19 at 12:27
  • 1
    Which version of Laravel are you using? Since the current version of laravel does not work with PHP < 7.1.3 and your dockerfile uses 5.x – Sven Hakvoort Feb 13 '19 at 14:08
  • @michalhosna i did't realize that i cut anything. I edited the post. Here is the log from the command : `nginx_1 | 2019/02/13 14:30:19 [emerg] 1#1: host not found in upstream "web" in /etc/nginx/conf.d/default.conf:12 nginx_1 | nginx: [emerg] host not found in upstream "web" in /etc/nginx/conf.d/default.conf:12` –  Feb 13 '19 at 14:37
  • I also added them to the post so you can see better... –  Feb 13 '19 at 14:42
  • @SvenHakvoort i use the latest version. Do you think changing it will change anything? –  Feb 13 '19 at 14:43
  • 1
    It is not a fix for your current issue, but it will be something that you encounter after this issue has been resolved. Therefore I would strongly recommend to change the base image to PHP 7.2 or higher – Sven Hakvoort Feb 13 '19 at 14:44
  • 1
    Have you tried changing your docker compose version to 3 instead of 2? With 3 networking is easier (with 2 you have to link them manually to each other) – Sven Hakvoort Feb 13 '19 at 14:45
  • 1
    @Darius Biro please post the logs as text to the question. Reasons being that if people try to help you, they can copy the error, but more importantly it's searchable, so people having the same problem as you in the future can find this question more easily. – michalhosna Feb 13 '19 at 15:03

1 Answers1

0

The problems seems to be only that nginx starts before php is ready. To test that, just run docker-compose up -d one more time.

Simply adding depends_on to the web service should do the trick here as proper solution. (Also bump up version to 2.4 which is the most recent version of v2 docker-compose)

version: '2.4'
services:
  nginx:
    depends_on:
      - web

This will cause docker-compose to start php container before the nginx container. It will not however wait for postgres to be ready. In this case it shouldn't be problem, because php starts really quickly.

If you want something more solid, or depends_on doesn't do the trick, you can add entrypoint wrapping script to your container. To find out more go to https://docs.docker.com/compose/startup-order/. There are also links to tools, so you don't have to write your own script from scratch.

michalhosna
  • 7,327
  • 3
  • 21
  • 40