Louis L
Louis L

Reputation: 81

Docker - Ubuntu - Nginx - MariaDB - Connection refused

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

Upvotes: 1

Views: 888

Answers (1)

renlo
renlo

Reputation: 21

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

Upvotes: 1

Related Questions