1

Im trying to setup a docker container on OSX with Docker - Ubuntu - Nginx - MariaDB to run a Laravel App

My docker settings are:

version: "2"
services:
  nginx:
      build:
          context: ./nginx
      ports:
          - "8080:80"
      volumes:
          - ./app:/var/app
  fpm:
      build:
          context: ./fpm
      volumes:
          - ./app:/var/app
      expose:
          - "9000"
      environment:
          - "DB_HOST=db"
          - "DB_DATABASE=laravel_db"
  db:
      image: mariadb
      ports:
          - "33061:3306"
      environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_DATABASE=laravel_db
      volumes:
          - ./database:/var/lib/mysql

And the 2 docker files:

FROM nginx
ADD ./default.conf /etc/nginx/conf.d/
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
CMD service nginx start

FROM ubuntu:latest
RUN apt-get update && apt-get install -y software-properties-common language-pack-en-base \
    && LC_ALL=en_US.UTF-8 add-apt-repository -y ppa:ondrej/php \
    && apt-get update \
    && apt-get install -y php7.0 php7.0-fpm php7.0-mysql mcrypt php7.0-gd curl \
       php7.0-curl php-redis php7.0-mbstring sendmail supervisor \
    && mkdir /run/php \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

RUN sed -i -e 's/listen = \/run\/php\/php7.0-fpm.sock/listen = 0.0.0.0:9000/g' /etc/php/7.0/fpm/pool.d/www.conf \
    && sed -i -e 's/;daemonize = yes/daemonize = no/g' /etc/php/7.0/fpm/php-fpm.conf

WORKDIR /var/app

EXPOSE 9000

CMD ["/usr/bin/supervisord"]

So far so good. I can access the Laravel App homepage as localhost:8080 and use Sequel Pro to access the MySQL DB.

But when access to the Laravel route that requires DB query, it returns "Connection refused"

Then I create a raw PHP file to make a DB connection test:

$link = mysqli_connect('127.0.0.1', 'root', 'root', 'laravel_db', 33061);
if(!$link) {
    die('failed to connect to the server: ' . mysqli_connect_error());
}

And I get connection refused error as well.

I tried using 127.0.0.1 and localhost but no hope.

Tried to google it but most answers are about ports are not published...

Thanks

2
  • Could you try $link = mysqli_connect('db', 'root', 'root', 'laravel_db', 33061);? Commented Jan 6, 2017 at 3:30
  • No, Im using exactly that code and i get connection refused Commented Jan 9, 2017 at 22:47

1 Answer 1

1

You need to use the link directive to connect various docker containers when using docker-compose. Ie, if you want to have docker container A communicate with docker container B, they need to be 'linked'

docker-compose documentation on links

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

5 Comments

I added the links in nginx service but still getting connection refused. Tried: links: fpm or db
It looks like youre using v2 of docker-compose so it actually wouldn't be a 'link directive'. You'll want to use 'depends_on: db' or whatever you'd like to connect to. The key thing is that the host will be 'db' (or whatever the container is named), it won't be 'localhost' / 127.0.0.1
I replace it with depends_on: - db - fpm but still. Do I need to rebuild or do something prior to docker-composer up -d ?
You don't need to rebuild the image; docker-compose just sets up and runs containers. Your problem is most likely that you are connecting to localhost instead of db (from your code $link = mysqli_connect('127.0.0.1', 'root', 'root', 'laravel_db', 33061);). The name of the host will be db; it won't be 127.0.0.1. From within the container you should do getent hosts db and you should see a private IP address. If you see this private IP address it means your current container is linked to the db container. If you don't see the address it means the containers aren't linked.
I can see the IP address of the db but still cannot connect. I tried the dbhost with: db, localhost, 127.0.0.1 and even the db private IP address. Still not work

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.