Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dniccum's avatar

MySQL not found in Docker container

I am currently working adding Docker support to one of my apps. I feel like I am really close. The Docker container builds fine with no errors but when I try to run the database migrations within the container, I get this error:

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

Any help would be appreciated.

docker-compose.yml

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: schierproducts/grease-monkey
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      APP_ENV: local
      APP_DEBUG: 1
      DB_DATABASE: greasemonkey
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network
    depends_on:
      - db

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:8.0.23
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: greasemonkey
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - app-network
    volumes:
      - dbdata:/var/lib/mysql-files
      - ./docker/mysql/my.cnf:/etc/mysql/my.cnf
      - ./docker/db/init:/docker-entrypoint-initdb.d/:ro

  #Redis Service
  redis:
    image: redis:alpine
    ports:
      - '6379:6379'
    volumes:
      - redisdata:/data
    networks:
      - app-network
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]

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

#Volumes
volumes:
  dbdata:
    driver: local
  redisdata:
    driver: local

Dockerfile

FROM php:7.4.14-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin python2 yarn libzip-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev

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

# Install extensions
RUN docker-php-ext-install mysqli pdo pdo_mysql zip exif pcntl
RUN docker-php-ext-enable pdo_mysql mysqli
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

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www
COPY docker/start.sh /usr/local/bin/start

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Updates permissions and runs start command
USER root
RUN chmod u+x /usr/local/bin/start
CMD [ "/usr/local/bin/start" ]

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

My .env file:

DB_HOST=db
DB_PORT=3306
DB_DATABASE=greasemonkey
DB_USERNAME=homestead
DB_PASSWORD=secret
0 likes
11 replies
bugsysha's avatar

In your .env file for DB_HOST set it to db.

dniccum's avatar

I added my .env file in the initial post. The host was in fact set to db.

tykus's avatar

How are you executing the command inside the container?

dniccum's avatar

This was the command that I was using:

docker-compose exec app php artisan migrate --force
tykus's avatar

Thats should be right; is the db container actually up and running? Do you get the same error message as previously?

dniccum's avatar

Yes, I can log in with the mysql -u homestead -p command and look at the tables within the container.

dniccum's avatar

I do want want to use Sail as I am looking to stand up a testing environment on Kubernetes.

sr57's avatar

@dniccum

Yes, but it can be a way of debug since Sail is easy to setup.

The pb can be 'simply' a pb of path and you can have a look by comparing the different dockers.

dniccum's avatar
dniccum
OP
Best Answer
Level 2

The MySQL client was not installed on the PHP container. So to fix my issue I added the following to my Dockerfile:

RUN apt-get install -y default-mysql-client

I then added the command property to the db service:

command: '--default-authentication-plugin=mysql_native_password'

I am successfully able to run my migrations now.

1 like
projectcamp's avatar

@dniccum facing the same issue. what i dont understand is that using the same dockerfiles work fine on my wsl2 ubuntu on local.

while deploying to aws linux 2 instance it gives me this error.

furthermore i tried your solution. i added the mysql_client service to the php container. and then added command to my db container service. still giving me the same error

Please or to participate in this conversation.