thelara
thelara

Reputation: 175

'mysql: not found', trying to connect a laravel, mysql and nginx in docker container

I'm trying to figure out why i'm getting an error related to mysql. The steps i did:

in the last one i get the following error:

enter image description here

Migration table created successfully.
Loading stored database schema: /var/www/database/schema/mysql-schema.dump

   Symfony\Component\Process\Exception\ProcessFailedException 

  The command "mysql  --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"" failed.

Exit Code: 127(Command not found)

Working directory: /var/www

Output:
================


Error Output:
================
sh: 1: mysql: not found

  at vendor/symfony/process/Process.php:257
    253▕      */
    254▕     public function mustRun(callable $callback = null, array $env = []): self
    255▕     {
    256▕         if (0 !== $this->run($callback, $env)) {
  ➜ 257▕             throw new ProcessFailedException($this);
    258▕         }
    259▕ 
    260▕         return $this;
    261▕     }

      +19 vendor frames 
  20  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

My files:

docker-compose.yml

version: '3.7'

services:

  #  The Application
  app:
    build:
      context: .
      dockerfile: docker-files/app.dockerfile
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    volumes:
      - ./:/var/www
      - ./docker-files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    env_file: '.env'
    networks:
      - app-network

  # The Web Server
  nginx:
    container_name: nginx
    restart: unless-stopped
    tty: true
    build:
      context: .
      dockerfile: docker-files/web.dockerfile
    volumes:
      - ./:/var/www
      - ./docker-files/nginx/conf.d/:/etc/nginx/conf.d/
    ports:
      - "8990:80"
      - "443:443"
    networks:
      - app-network
  
  # The Database
  db:
    container_name: db
    restart: unless-stopped
    tty: true
    image: mysql:5.7
    volumes:
      - dbdata:/var/lib/mysql/
      - ./docker-files/mysql/my.cnf:/etc/mysql/my.cnf
    environment:
      MYSQL_DATABASE: maindbs
      MYSQL_USER: root
      MYSQL_PASSWORD: 123456
      MYSQL_ROOT_PASSWORD: 123456
    ports:
      - "3306:3306"
    networks:
      - app-network
  
#Volumes
volumes:
  dbdata:
    driver: local

#Docker Networks
networks:
  app-network:
    driver: bridge

app.dockerfile

FROM php:7.4-fpm

COPY composer.lock composer.json /var/www/

COPY database /var/www/database

WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libzip-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Copy application folder
COPY . /var/www

RUN chown -R www-data:www-data \
    /var/www/storage \
    /var/www/bootstrap/cache

RUN php artisan optimize

EXPOSE 9000
CMD ["php-fpm"]

web.dockerfile

FROM nginx:alpine

ADD docker-files/nginx/conf.d/app.conf /etc/nginx/conf.d/default.conf

COPY public /var/www/public

.env

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=maindbs
DB_USERNAME=root
DB_PASSWORD=123456

I can connect to laravel app using 127.0.0.1:8990

Upvotes: 1

Views: 5148

Answers (1)

Tania Petsouka
Tania Petsouka

Reputation: 1424

You don't have mysql client in the container that tries to execute mysql command. Add mysql via the Dockerfile.

RUN apt-get update && apt-get install -y mysql-client && rm -rf /var/lib/apt

Upvotes: 4

Related Questions