简体   繁体   中英

Access site served locally via Laravel Valet from Docker container

I'm trying to get my local dev environment setup using a combination of Docker which is serving a webapp that I'm developing, and Laravel Valet which is serving the API (being served on api.test).

What I can't figure out is how to hit the API from within the Docker container?

Dockerfile :

FROM php:5-apache

WORKDIR /var/www/html

COPY ./default.conf /etc/apache2/sites-available/000-default.conf

RUN apt-get update && \
  apt-get -y install curl git libicu-dev libpq-dev zlib1g-dev zip libmcrypt-dev libgd-dev && \
  docker-php-ext-install intl mbstring pcntl zip mcrypt && \
  usermod -u 1000 www-data && \
  usermod -a -G users www-data && \
  chown -R www-data:www-data /var/www && \
  curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
  a2enmod rewrite && \
  curl -sL https://deb.nodesource.com/setup_13.x | bash - && \
  apt-get install -y nodejs && \
  docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
  docker-php-ext-install gd

COPY . /var/www/html

docker-compose.yml :

version: '2'
services:
  web:
    build: .
    ports:
      - "8090:80"
    volumes:
      - .:/var/www/html
    network_mode: bridge

Ok, finally figured this out thanks to some help from a friend.

I found the IP address of the host: ping host.docker.internal which gave me 192.168.65.2 .

Then updated /etc/hosts file in the Docker container with:

192.168.65.2 api.test

(where api.test is the route that the API is being served on my localhost)

Now I can hit api.test from within the Docker container:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM