Home > Software design >  What I can do to connect a remote database with a docker container?
What I can do to connect a remote database with a docker container?

Time:09-02

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"]

CodePudding user response:

You can pass your database configuration using environment variables through 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} 

CodePudding user response:

You can also (for convenience) add your local .env file (or what ever your are using for db creds:

version: "3.4"

services:

  project:
    build:
      context: ./
    image: project:latest
    ports:
      - "80:80"
    env_file:
      - .env.local

Makes life easier if you are having an environmant defined anyways :)

  • Related