skip to Main Content

How to configure my Dockerfile or docker-compose for that my host machine can resolve a ServerName like http://myapp.env instead http://localhost:8080?!

I tried to configure my local hosts file with the ip from container, but without success, like this

172.20.0.3 goforce.env

My Dockerfile

FROM php:7.2-apache

LABEL maintainer="rIckSanchez"
LABEL project="MyApp"

COPY .docker/php/php.ini /usr/local/etc/php/
COPY .docker/apache/000-default.conf /etc/apache2/sites-available/
COPY . /srv/app

RUN apt-get update && apt-get install --no-install-recommends -y 
    wget 
    nano 
    git 
    unzip 
    iputils-ping

# Install PHP extensions deps
RUN apt-get update 
    && apt-get install --no-install-recommends -y 
        libfreetype6-dev 
        libjpeg62-turbo-dev 
        libmcrypt-dev 
        zlib1g-dev 
        libicu-dev 
        g++ 
        unixodbc-dev 
        libxml2-dev 
        libaio-dev 
        libmemcached-dev 
        freetds-dev 
        libssl-dev 
        openssl

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- 
        --install-dir=/usr/local/bin 
        --filename=composer

# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ 
    && docker-php-ext-configure pdo_dblib --with-libdir=/lib/x86_64-linux-gnu 
    && pecl install sqlsrv-4.1.6.1 
    && pecl install pdo_sqlsrv-4.1.6.1 
    && pecl install redis 
    && pecl install memcached 
    && docker-php-ext-install 
            iconv 
            mbstring 
            intl 
            gd 
            mysqli 
            pdo_mysql 
            pdo_dblib 
            soap 
            sockets 
            zip 
            pcntl 
            ftp 
    && docker-php-ext-enable 
            sqlsrv 
            pdo_sqlsrv 
            redis 
            memcached 
            opcache


RUN a2enmod rewrite negotiation

RUN chown -R www-data:www-data /srv/app

RUN service apache2 restart

My docker-compose

version: "3"
networks:
  app-tier:
    driver: bridge
services:
  app:
    image: laravel-app
    container_name: laravel-app
    networks:
      - app-tier
    build:
      context: .
      dockerfile: .docker/Dockerfile
    depends_on:
      - redis
    ports:
      - 8080:80
    volumes:
      - .:/srv/app

  redis:
    image: redis:4-alpine
    container_name: laravel-redis
    networks:
      - app-tier
    ports:
      - 16379:6379
    volumes:
      - redis:/data

volumes:
  redis:
    driver: "local"

My 000-default.conf file

<VirtualHost *:80>
    ServerName myapp.env
    ServerAdmin webmaster@localhost
    DocumentRoot /srv/app/public
    <Directory "/srv/app/public">
    AllowOverride all
    Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

2

Answers


  1. Your docker web server port 80 is mapped to your localhost port 8080. To go around the the fact that localhost can’t see the docker private network. The dns won’t be able to map traffic to certain port, but you can access goforce.env:8080 by adding following line to your /etc/hosts file:

    127.0.0.1 goforce.env 
    

    If you want to use just goforce.env, you should change your docker-compose:

    ports:
      - 8080:80 -> to 80:80
    

    or you could run apache proxy on your host machine and point port 80 to 8080 (if you must run apache on host and docker server at the same time).

    Login or Signup to reply.
  2. Try using nginx for reverse proxy of the application . Check this url

    Multiple docker containers accessible by nginx reverse proxy

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search