我是docker新手,我想在我的docker机器上设置一个虚拟主机,以便以这种方式访问它http://myapp.dev
或*.myapp.dev
(接受任何子域)。
我尝试了几种方法,但似乎都不起作用
docker-compose.yml
在文件中设置 VIRTUAL_HOST 环境变量- 在我的项目根目录创建一个 apache2 配置文件,并将其作为 docker compose 中的卷链接起来(例如:
./php-apache.conf:/et/apache2/site-enabled/000-default.conf
并添加127.0.0.1 myapp.dev
到我的主机/etc/hosts
文件中
这听起来很常见,所以我一定遗漏了一些东西。任何帮助都将不胜感激,这是我的配置:
docker-compose.yml
:
version: '3.5'
services:
php-apache:
build:
context: ./php-apache
args:
uid: ${UID}
container_name: laravel-app
environment:
- VIRTUAL_HOST=myapp.dev
- VIRTUAL_PORT=8000
- APACHE_RUN_USER=#${UID}
- APACHE_RUN_GROUP=#${UID}
volumes:
- ./:/var/www/html
# tried with the above line w/o success
- ./php-apache.conf:/etc/apache2/site-enabled/000-default.conf
ports:
- 8000:80
networks:
backend:
aliases:
- laravel-app
mysql-db:
image: mysql:5.7
container_name: mysql-db
volumes:
- ./run/var:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=my_rootpwd
- MYSQL_DATABASE=my_db
- MYSQL_USER=my_user
- MYSQL_PASSWORD=my_pwd
networks:
backend:
aliases:
- db
networks:
backend:
name: backend-network
Dockerfile
:
RUN apt-get update
# 1. development packages
RUN apt-get install -y \
git \
zip \
curl \
sudo \
unzip \
libicu-dev \
libbz2-dev \
libpng-dev \
libjpeg-dev \
libmcrypt-dev \
libreadline-dev \
libfreetype6-dev \
g++
# 2. apache configs + document root
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# 3. mod_rewrite for URL rewrite and mod_headers for .htaccess extra headers like Access-Control-Allow-Origin-
RUN a2enmod rewrite headers
RUN docker-php-ext-install \
bz2 \
intl \
iconv \
bcmath \
opcache \
calendar \
mbstring \
pdo_mysql \
zip
# 5. composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# 6. we need a user with the same UID/GID with host user
# so when we execute CLI commands, all the host file's ownership remains intact
# otherwise command from inside container will create root-owned files and directories
ARG uid
RUN useradd -G www-data,root -u $uid -d /home/devuser devuser
RUN mkdir -p /home/devuser/.composer && \
chown -R devuser:devuser /home/devuser
/etc/hosts
(来自主机)
127.0.0.1 localhost
127.0.1.1 anon-870Z5G-880Z5F
127.0.0.1 myapp.dev
# i also tried with the docker ip like 127.20.0.3 myapp.dev
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters