克隆一个laravel并将其dockerize以在本地运行该项目,但我不知道如何连接到远程数据库,也就是说,我不想创建一个mysql容器,以连接到我拥有的所述数据库主机、用户和密码,我想知道,我该怎么做才能将我的容器连接到该数据库?然后我添加我的 docker compose 和我的 dockerfile。
docker 撰写
version: "3.4"
services:
project:
build:
context: ./
image: project:latest
ports:
- "80:80"
docker文件
FROM php:8.0-fpm
# Set working directory
WORKDIR /var/www
# Add docker php ext repo
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
# Install php extensions
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions mbstring pdo_mysql zip exif pcntl gd memcached
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
unzip \
git \
curl \
lua-zlib-dev \
libmemcached-dev \
nginx
# Install supervisor
RUN apt-get install -y supervisor
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy code to /var/www
COPY --chown=www:www-data . /var/www
# add root to www group
# RUN chmod -R ug+w /var/www/storage
RUN chmod -R 777 /var/www/storage/
# Copy nginx/php/supervisor configs
RUN cp docker/supervisor.conf /etc/supervisord.conf
RUN cp docker/php.ini /usr/local/etc/php/conf.d/app.ini
RUN cp docker/nginx.conf /etc/nginx/sites-enabled/default
# PHP Error Log Files
RUN mkdir /var/log/php
RUN touch /var/log/php/errors.log && chmod 777 /var/log/php/errors.log
# Deployment steps
RUN composer install --optimize-autoloader --no-dev
RUN chmod +x /var/www/docker/run.sh
EXPOSE 80
ENTRYPOINT ["/var/www/docker/run.sh"]
您可以通过 docker-compose 使用环境变量传递数据库配置
version: "3.4"
services:
project:
build:
context: ./
image: project:latest
ports:
- "80:80"
environment:
DB_CONNECTION=${DB_CONNECTION}
DB_HOST=${DB_HOST}
DB_PORT=${DB_PORT}
DB_DATABASE=${DB_DATABASE}
DB_USERNAME=${DB_DB_USERNAME}
DB_PASSWORD=${DB_DB_PASSWORD}
您还可以(为了方便起见)添加本地 .env 文件(或者您用于数据库信用的文件:
version: "3.4"
services:
project:
build:
context: ./
image: project:latest
ports:
- "80:80"
env_file:
- .env.local
无论如何,如果你有一个定义的环境,会让生活变得更轻松:)
对我来说,当我写下我的本地网络IP - 192.168.0.XXX时,它就工作了。
postgres://postgres:[email protected]:5432/docker_db_name_prod
。