duandiaoqian5795 2017-01-06 00:09
浏览 212

Docker - Ubuntu - Nginx - MariaDB - 连接被拒绝

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

  • 写回答

1条回答 默认 最新

  • duanna2026 2017-01-06 00:11
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘