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.
Related
I was training on writing integration tests on a simple app and I had some of the working. After some errors I terminated the yarn command running the tests in watch mode and when restarting the same command I got the error in the object:
Error: P1017: Server has closed the connection
I am using prisma, nestjs and mySql as a database, containerized in docker.
I used a code from a tutorial as a base and that one works (it is identical to the following one except for the ports, since the tutorial is using postgres)
This it the docker-compose file
version: '3.8'
services:
dev-db:
image: mysql:5.7
#platform: linux/amd64
ports:
- 3308:3306
environment:
MYSQL_ROOT_PASSWORD: 123
MYSQL_DB: dbfornest
test-db:
image: mysql:5.7
#platform: linux/amd64
ports:
- 3305:3306
environment:
MYSQL_ROOT_PASSWORD: 123
MYSQL_DB: dbfornest
These are the scripts in the json file
"db:restart": "docker compose down && docker compose up -d && sleep 1",
"pretest:int": "yarn db:restart && dotenv -e .env.test -- prisma migrate reset --force",
"test:int": "dotenv -e .env.test -- jest -i --no-cache --watch --config jest-int.json"
this is the .env file
DATABASE_URL=mysql://root:123#localhost:3308/dbfornest
this is the .env.test file
DATABASE_URL=mysql://root:123#localhost:3305/dbfornest
the error was in the docker-compose file. The correct way of naming the database for mySQL is MYSQL_DATABASE
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?
I have a problem with mysql 5.7 container denying access to my nodeJS container. I'm using docker-compose and I'm running docker on Ubuntu 18.04 lts.
I apologize for the mistakes in English. This is not my mother language.
I replaced all confidential information with *** and in the images I put a red line
Here's my docker-compose.yml
version: '3.1'
services:
db:
container_name: '***-db'
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=***
- MYSQL_USER=***
- MYSQL_PASSWORD=***
- TZ=America/Sao_Paulo
command: mysqld --sql_mode="" --character-set-server=utf8 --collation-server=utf8_slovenian_ci --init-connect='SET NAMES UTF8;' --innodb-flush-log-at-trx-commit=0
ports:
- 3306:3306
volumes:
- ./db_data:/var/lib/mysql
network_mode: "host"
server:
image: server
restart: always
ports:
- "2000:22"
- "3000:3000"
network_mode: "host"
front:
image: portal
restart: always
links:
- server:server
ports:
- 4200:80
I am using a volume with the database for my mysql container. In this volume I have my user and password set and I also have my schema with every table. I copy everything from this path:
/var/lib/mysql
that I have on my configuration (I am not using docker on this one) in another computer with the same version of mysql and that I know it is working.
Here is my server Dockerfile:
FROM ubuntu:18.04
LABEL maintainer = "***"
LABEL build_date="2020-04-24"
RUN apt-get update && apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get install -y nodejs
#COPY AN CONFIGURATION OF APP
COPY ***-server /home/root/***-server
WORKDIR /home/root/***-server
RUN npm install
EXPOSE 22
EXPOSE 3000
CMD ["npm", "start"]
Here is my nodeJS file to connect with the mysql:
"use strict";
module.exports = {
config: {
connectionLimit : 200,
host: 'localhost',
user : '***',
password : '***',
database: '***'
}
}
this is the error mesage that docker compose send to me, when I try to connect:
And this is image of the container of the mysql that I am trying to access. I make access to the mysql inside the container using the same user that I am using on the server.
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
I have my app running on container. I have mysql server started and having trouble to connect my app with mysql server. This is the screenshot of the error
my docker-compose.yml file:
version: '3'
services:
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=new_password
- MYSQL_USER=composetest
- MYSQL_PASSWORD=new_password
- MYSQL_DATABASE=sample_db
- MYSQL_HOST=localhost
web:
build: .
command: bash -c "python check_db.py --service-name mysql --ip db --port 3306 &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
ports:
- "8000:8000"
You forgot to link the web container to the db container.
Add the following to the web configuration and it should be all:
links:
- db
I would love to write my docker-compose as bellow.
No need to set host and should link the service with depends_on
version: '3'
services:
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=new_password
- MYSQL_USER=composetest
- MYSQL_PASSWORD=new_password
- MYSQL_DATABASE=sample_db
web:
build: .
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
I have removed the commands from the docker-compose which you can add to the bash script and add to run it at entrypoint. The script will look something like bellow. I am doing cd to go the respective directory where the applications is installed.
run.sh
#!/bin/sh
#getting into the directory where the command should run.
cd /app
python check_db.py --service-name mysql --ip db --port 3306
python manage.py migrate
python manage.py runserver 0.0.0.0:8000
In the database setting part where you set the hostname put the
service name in your case its db .
These is in overall what I do in laravel app deploy must be similar
in your case too