如何配置主机以访问Docker中的Apache ServeName

时间:2018-06-04 18:29:57

标签: apache docker docker-compose dockerfile docker-for-windows

如何配置我的Dockerfile或docker-compose,因为我的主机可以解析像http://myapp.env这样的ServerName而不是http://localhost:8080

我尝试使用ip from container来配置我的本地hosts文件,但没有成功,比如

172.20.0.3 goforce.env

我的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

我的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"

我的000-default.conf文件

<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 个答案:

答案 0 :(得分:0)

您的docker Web服务器端口80映射到您的localhost端口8080.要解决localhost无法看到docker专用网络的事实。 dns无法将流量映射到某个端口,但您可以通过在/ etc / hosts文件中添加以下行来访问goforce.env:8080:

 var request = new BatchExecutionRequest() 
 {           
     GlobalParameters = new Dictionary<string, string>() {
         { "Query", @"select [age], [workclass], [fnlwgt], [education], [education-num], [marital-status], [occupation], [relationship], [race], [sex], [capital-gain], [capital-loss], [hours-per-week], [native-country], [income] from dbo.censusdata" },
         { "Table", "dbo.ScoredTable2" },
     }
 };

如果你只想使用goforce.env,你应该改变你的docker-compose:

127.0.0.1 goforce.env 

或者您可以在主机上运行apache proxy并将端口80指向8080(如果您必须同时在主机和docker服务器上运行apache)。

答案 1 :(得分:0)

尝试使用nginx进行应用程序的反向代理。检查此网址

Multiple docker containers accessible by nginx reverse proxy