简体   繁体   English

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

[英]How to configure host machine to access Apache ServeName in Docker

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 ?! 如何为我的主机配置我的Dockerfile或docker-compose可以解析像http://myapp.env这样的ServerName而不是http:// localhost:8080 ?!

I tried to configure my local hosts file with the ip from container, but without success, like this 我试图使用来自容器的ip配置我的本地主机文件,但没有成功,就像这样

172.20.0.3 goforce.env 172.20.0.3 goforce.env

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

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. 您的Docker Web服务器端口80映射到您的localhost端口8080。要绕过localhost无法看到Docker专用网络的事实。 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: dns无法将流量映射到某些端口,但是您可以通过在/ etc / hosts文件中添加以下行来访问goforce.env:8080:

127.0.0.1 goforce.env 

If you want to use just goforce.env, you should change your docker-compose: 如果只想使用goforce.env,则应更改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). 或者您可以在主机上运行apache代理并将端口80指向8080(如果必须同时在主机和docker服务器上运行apache)。

Try using nginx for reverse proxy of the application . 尝试使用nginx进行应用程序的反向代理。 Check this url 检查这个网址

Multiple docker containers accessible by nginx reverse proxy Nginx反向代理可访问多个Docker容器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 配置Apache以在访问和错误日​​志中记录主机 - Configure Apache to record a host in access & error logs 在 80 端口的主机和 docker 容器中运行 apache - Run apache in both host machine and docker container on 80 port 如何在apache中配置虚拟主机,以允许用户仅使用某些文件扩展名访问这些资源? - How to configure the virtual host in apache to let the users access those resources with certain file extensions only? 配置apache2和主机以使用nginx将流量传递到docker容器 - Configure apache2 and host to pass traffic to a docker container with nginx 在Apache上配置虚拟主机 - Configure virtual host on Apache 如何配置Apache在同一台机器上与FE和BE一起使用? - How to configure apache to work with FE and BE on same machine? 如何使用移动设备访问Apache Virtual Host? - How access Apache Virtual Host with a mobile device? 如何在apache虚拟主机中访问laravel api? - How to access a laravel api in apache virtual host? 如何在Apache虚拟主机上使用别名配置多个SSL证书? - How to configure multiple SSL certs on Apache virtual host with aliases? 如何配置两个不同的Web应用程序以在同一主机/端口上使用Apache? - How to configure two different webapps to work with Apache on the same host/port?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM