In your .env file for DB_HOST set it to db.
Feb 4, 2021
11
Level 2
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
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
Please or to participate in this conversation.