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
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
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
Related
it's my first time using docker so sorry if I'm making dumb mistakes. I'm trying to setup a docker container for laravel development. These are my files:
Dockerfile:
FROM php:8-fpm
COPY . /app
WORKDIR /app
COPY composer.lock composer.json ./
RUN apt-get update -y && apt-get install -y sendmail libpng-dev
RUN apt-get update && apt-get install -y \
build-essential \
curl
RUN curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s \
gd \
zip
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer update
RUN composer install
docker-compose.yaml:
version: '3.9'
name: caas-portal
services:
front:
build:
context: ./webapp
target: builder
ports:
- 4200:4200
volumes:
- ./webapp:/project
- /project/node_modules
mysql:
container_name: mysql
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=db
ports:
- 3307:3306/tcp
networks:
- laravel
elasticsearch:
container_name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.7
environment:
- discovery.type=single-node
ports:
- 9200:9200/tcp
- 9300:9300/tcp
volumes:
- 'elasticsearch:/usr/share/elasticsearch/data'
networks:
- laravel
app:
container_name: back
build: .
ports:
- 8000:80
networks:
- laravel
networks:
laravel:
driver: bridge
volumes:
elasticsearch:
driver: local
And this is the relevant part of my .env:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3307
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=password
When I run docker exec -it <container> sh and inside shell I run php artisan migrate I get the following error:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = db and table_name = migrations and table_type = 'BASE TABLE')
I've seen similar posts, and tried to follow the answers given but still no luck. Where is the mistake, and how can it be corrected?
In your .env, try DB_PORT=3306.
According to Docker on Networking in Compose,
It is important to note the distinction between HOST_PORT and CONTAINER_PORT. [...] Networked service-to-service communication uses the CONTAINER_PORT.
If we apply this concept to your docker-compose.yaml, we know that the mysql service has a host port of 3307 while having a container port 3306.
Now if you want a your app service to connect to your mysql service (service-to-service communication), then within your app container you should connect using the mysql container port 3306, not 3307.
I don't know how unique my problem is but I see similar issues throughout the web but I have the scenario that doesn't match any of the problems I've seen.
I have a pretty standard Express app that uses SequelizeJS and MySQL 8. Sequlize has migration which I run using entrypoint.sh that runs sequelize migration command.
# Run migrations
npx sequelize-cli db:migrate
# Preload initial data
npx sequelize-cli db:seed:all
When I run docker compose command the project builds without any issues and sequelize migration and seeds runs. Database connection from sequelize command line works perfectly!
But when I load my homepage the SQL query from the application throws error. The error itself is quite common
{
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client',
sqlState: '08004',
fatal: true
}
I have seen this error while mysql 8 and the package mysql used together with sequlizejs. The common solution is to use mysql2 which I am using in my project. I've also tried to run command --default-authentication-plugin=mysql_native_password inside mysql container which didn't help.
Docker Compose
version: '3.9'
services:
linkfy-dashboard:
container_name: linkfy-dashboard
restart: always
depends_on:
- linkfy-mysql
build:
context: .
dockerfile: Dockerfile.dev
ports:
- '10200:3000'
environment:
APP_NAME: Linkfy
DB_HOST: linkfy-mysql
DB_USER: linkfy
DB_PASS: password
DB_NAME: linkfy
COOKIE_SECRET: secret
NONCE_SECRET: secret
COOKIE_SECURE: 'false'
volumes:
- /app/node_modules
- .:/app
networks:
- linkfy
linkfy-mysql:
container_name: linkfy-mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: linkfy
MYSQL_USER: linkfy
MYSQL_PASSWORD: password
volumes:
- database:/var/lib/mysql
networks:
- linkfy
adminer:
image: adminer
restart: always
ports:
- 10210:8080
networks:
- linkfy
networks:
linkfy:
name: linkfy
volumes:
database:
Dockerfile
FROM node:14.17-alpine
# source directory
WORKDIR /app
# global dependancy
RUN yarn global add sequelize-cli nodemon
# dependency file (package.json and package-lock.json)
COPY package.json .
RUN apk add --update python2 make g++ && rm -rf /var/cache/apk/*
RUN yarn install
# copy the sources
COPY . .
# listener
EXPOSE 3000
ENTRYPOINT ["./entrypoint.sh"]
# command to run
CMD [ "npm", "run", "dev" ]
I need some direction how to troubleshoot this.
I am running WordPress with wp-env and docker configuration,
I want to connect to mysql via cli but I keep getting this error:
zsh: command not found: mysql
I have tried: mysql -h 127.0.0.1:3308 -u root -p and in post 3306 as well
this is my docker-compose.yaml file
phpmyadmin works great
version: '3.1'
services:
mysql:
image: mysql:5.7
ports:
- 3308:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress_test
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: mysql
volumes:
testsuite:
`
What I did was to execute the following command:
docker exec -it {container name} bash -l
I have a laravel api project with react SPA front-end.
Folder structure:
app
...
Dockerfile
api
...
Dockerfile
docker-compose.yml
On laravel (api) .env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=myapp
DB_USERNAME=root
DB_PASSWORD=secret
On api (laravel) Dockerfile
FROM php:7.4
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo_mysql
WORKDIR /myapp/api
COPY . .
RUN composer install
RUN php artisan config:clear
RUN cp .env.example .env
RUN php artisan key:generate
RUN php artisan migrate
RUN php artisan db:seed
EXPOSE 8000
CMD php artisan serve --host=0.0.0.0
On app (react) Dockerfile
FROM node:alpine
WORKDIR /myapp/app
COPY . .
RUN npm install
EXPOSE 3000
CMD npm start
On docker-compose.yml
version: '3.7'
# Services
services:
# MySQL Database
db:
image: mysql:5.7.22
container_name: db
restart: always
environment:
MYSQL_DATABASE: 'myapp'
MYSQL_USER: 'root'
MYSQL_PASSWORD: 'secret'
MYSQL_ROOT_PASSWORD: 'secret'
MYSQL_ROOT_USER: root
ports:
- 3308:3306
volumes:
- ./db/:/var/lib/mysql/
networks:
- app
# Laravel REST API
api:
build:
context: ./api
dockerfile: Dockerfile
container_name: api
environment:
DB_HOST: db
DB_DATABASE: 'myapp'
DB_USERNAME: 'root'
DB_PASSWORD: 'secret'
depends_on:
- db
ports:
- 8000:8000
volumes:
- ./api:/myapp/api
networks:
- app
# React SPA
app:
build:
context: ./app
dockerfile: Dockerfile
container_name: app
tty: true
ports:
- 3000:3000
volumes:
- ./app:/myapp/app
depends_on:
- api
networks:
- app
# Networks
networks:
app:
driver: bridge
Then running docker using
docker-compose down
docker system prune -a
docker-compose up --build
This gives error on php artisan migrate
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
(SQL: select * from information_schema.tables where table_schema = myapp and table_name = migrations and table_type = 'BASE TABLE')
Update:
I am getting these logs if I remove migrate & seed artisan commands. So something to deal with sock?
Hi I'm trying to learn Docker.
As a starting point, I've followed DigitalOcean's setup tutorial for Laravel here.
I've managed to get all containers up and even finished building my Laravel app. Now I want to setup a 2nd DB for testing.
I duplicated the settings of the db part of docker-compose, and changed the container name and ports.
All containers setup fine, but whenever I try do run docker exec in either of the db containers, I get a 2002 error, which I did not encounter while I had only 1 DB container running:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Dockerfile:
FROM php:7.2-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 \
build-essential \
mysql-client \
libpng-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 mbstring zip 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 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 existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
docker-compose.yml:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: todo_app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
web:
image: nginx:alpine
container_name: todo_web
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: todo_db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: todo
MYSQL_ROOT_PASSWORD: ent3r1n
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
db_test:
image: mysql:5.7.22
container_name: todo_db_test
restart: unless-stopped
tty: true
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: todo
MYSQL_ROOT_PASSWORD: ent3r1n
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Output of docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5ccebc57f9d digitalocean.com/php "docker-php-entrypoi…" 15 seconds ago Up 13 seconds 9000/tcp todo_app
7cc53434d7d7 nginx:alpine "nginx -g 'daemon of…" 15 seconds ago Up 13 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp todo_web
7aab5318e7af mysql:5.7.22 "docker-entrypoint.s…" 15 seconds ago Up 13 seconds 0.0.0.0:3307->3306/tcp todo_db_test
5b72306f63a4 mysql:5.7.22 "docker-entrypoint.s…" 15 seconds ago Up 13 seconds 0.0.0.0:3306->3306/tcp todo_db
I have ever got the problem like this, because of image you use: image: mysql:5.7.22
You should use: image: mysql/mysql-server:5.7.
Hope it can help you.