What I can do to connect a remote database with a docker container with Laravel 8? Clone a laravel and dockerize it to run the project locally, but I don't know what to do to connect to a remote database, that is, I don't want to create a mysql container, to connect to said database I have the host, a user and password, I want to know, what can I do to connect my container to that database? Then I add my docker compose and my dockerfile.
docker compose
version: "3.4"
services:
project:
build:
context: ./
image: project:latest
ports:
- "80:80"
dockerfile
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"]
.env
DB_CONNECTION=mysql
DB_HOST=xxx.xxx.xxx.xxx #secret_host
DB_PORT=3306
DB_DATABASE=secret_database
DB_USERNAME=secret_username
DB_PASSWORD=secret_password
Docker can send requests to the outside world. So you don't need to do anything special. Put the credentials in env and it should work. I do this myself
Just make sure your computer is allowed to connect to the external server on port 3306 and there is a db user for external access
@Sinnbeck that´s correct I was wrong in my credentials
@Sinnbeck But How does it connect docker to laravel. Where i mention to docker-compose mysql service in .env file?
if assume i have mysql services in docker-compose-file.
what to write on my .env file DB_HOST = ?
Please sign in or create an account to participate in this conversation.