Reputation: 97
I have laravel project and must create a docker container for this. I am done doing this but for MySQL I must run some commands
docker-compose exec app php artisan key:generate
docker-compose exec db bash
mysql -u root -p
Login Using password Library!23
GRANT ALL ON laravel.* TO 'root'@'%' IDENTIFIED BY '123';
FLUSH PRIVILEGES;
EXIT;
exit
docker-compose exec app php artisan migrate
In the line 2 app switch the bash and I must exit to run commands but I need to open MySQL bash login and give permission to the user after that run artisan migrate and its my docke-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: xxxxxxx/lumen:Library
container_name: Library
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: LibraryWebserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: Librarydb
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: library
MYSQL_ROOT_PASSWORD: Library!23
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
volumes:
- dbdata:/var/lib/mysql
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
and my Dockerfile
FROM php:7.4-fpm
COPY composer.lock composer.json /var/www/
WORKDIR /var/www
RUN apt-get update --fix-missing && apt-get install -y \
build-essential \
libssl-dev \
zlib1g-dev \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
zlibc \
mariadb-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
zip
RUN docker-php-ext-install opcache && docker-php-ext-enable opcache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install pdo_mysql exif pcntl
#RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install bcmath
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
EXPOSE 9000
CMD ["php-fpm"]
can anyone help me???
and i want to run composer install
in app bash
Upvotes: 1
Views: 6376
Reputation: 370
for step 2 - 8 you have to ways like our friends says create bootstrap file with sql commands and run it in your dockerfile or you can create user and pass and grant admin access to user in docker-compose.yml
db:
image: mysql:5.7.22
container_name: Librarydb
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: library
MYSQL_ROOT_PASSWORD: TheRootPassword
MYSQL_USER: root
MYSQL_PASSWORD: PasswordForLoginAndUse
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
volumes:
- dbdata:/var/lib/mysql
and about generating a key with artisan its not necessary
app think you run it in one system because of docker
and i prefer you to run migration manually
its better
Upvotes: 2
Reputation: 2845
instead of executing the MySQL commands directly (or manually). You can bootstrap MySQL containers with all the needed data and configurations using the following approach.
1- Create a bootstrap file : sql-scripts.sql
GRANT ALL ON laravel.* TO 'root'@'%' IDENTIFIED BY '123';
FLUSH PRIVILEGES;
2- Create a custom MySQL Docker image: mysql.Dockerfile
FROM mysql:8.0.1
COPY ./sql-scripts.sql /docker-entrypoint-initdb.d/
3- build MySQL docker image and use it in your docker-compose file. You need to before the below change on your docker-compose. The bootstrap file will be executed automatically the FIRST TIME you execute docker-compose up
. you need to remove the MySQL volume to make this works for your stack docker volume rm dbdata
.
db:
build:
context: .
dockerfile: mysql.Dockerfile
Upvotes: 1