django cant access mysql docker compose - mysql

this is my docker-compose file
version: '3'
services:
djangoSql:
restart: always
network_mode: bridge
container_name: djangoSql
image: mysql
volumes:
- .:/code
environment:
MYSQL_DATABASE: test1
MYSQL_ROOT_PASSWORD: 1234
ports:
- 6044:3306
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
interval: 2s
timeout: 20s
retries: 10
web:
build: .
network_mode: bridge
volumes:
- .:/code
ports:
- "8000:8000"
image: django
environment:
WAIT_HOSTS: djangoSql:3306
WAIT_HOSTS_TIMEOUT: 300
WAIT_SLEEP_INTERVAL: 30
WAIT_HOST_CONNECT_TIMEOUT: 30
depends_on:
- djangoSql
links:
- djangoSql:mysql
this is my error
django.db.utils.OperationalError: (1045, 'Plugin caching_sha2_password could not be loaded: /usr//usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory')
the url has two usr i don't know why
this is docker file
FROM python:3.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD ./wait-for-it.sh /code/wait-for-it.sh
ADD . /code/
CMD ["/code/wait-for-it.sh", "djangoSql:3306", "-t", "6000", "--`","python","manage.py", "runserver", "0.0.0.0:8000"]`
and this is requirement.txt
Django>=2.0,<3.0
djangorestframework
mysqlclient
tensorflow==1.15
setuptools>=41.0.0
pandas
Keras

according to mysql official image you need to add this command to your mysql service in the compose file:
command: --default-authentication-plugin=mysql_native_password
and do not forget to create a new container of your mysql image after adding this command.

Related

Dumping database after migration using Prisma inside docker container and docker compose (Proper flow)

I am building a nestjs application where I have used Prisma. I am trying to figure out docker workflow for it. You can see on the code below, Currently, I am first running MySQL container and after healthcheck I am running prisma migrate command from my backend. This process is correct if the volume is removed as it will run migration after the database is ready. But Later I also want to dump a SQL file(insert commands) to the MySQL container once tables are migrated. So here on the below code it runs the dump even before tables are migrated. Can anyone help me on how to dump only after table migration has been created.
mysql:
platform: linux/x86_64
container_name: mysql
command: --default-authentication-plugin=mysql_native_password
image: mysql:5.7.36
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: #####
MYSQL_PASSWORD: password
volumes:
- ./mysqldump:/docker-entrypoint-initdb.d
- mysql:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
networks:
- pushboard
reverse-proxy:
image: nginx:1.22.1
container_name: web_server
depends_on:
- backend
- frontend
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
networks:
- pushboard
backend:
build: ./api
container_name: api
ports:
- "3000:3000"
depends_on:
mysql:
condition: service_healthy
networks:
- pushboard
env_file:
- ./api/.env
volumes:
- ./api:/app
- /app/node_modules
- /app/prisma
stdin_open: true
restart: "on-failure:2"
tty: true
command: sh -c "yarn prisma migrate dev --name init && yarn start:dev"

docker-compose error when creating MySQL database container

I have the following docker-compose.yml and Dockerfile in my Spring Boot app but when I run docker-compose up -d, I get the following error:
"failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount967315702/Dockerfile: no such file or directory"
As I use Windows10, I think I need some tweaks related to the WORKDIR, etc. I have WSL where Docker installed). But even I tried different combinations, I have not managed to fix the problem.
Here is the docker-compose.yml and Dockerfile in my root project folder (springboot-backend):
docker-compose.yml: I am not sure about
version: "3.8"
services:
mysqldb:
image: mysql:5.7
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
volumes:
- db:/var/lib/mysql
app:
depends_on:
- mysqldb
build: ./springboot-backend
restart: on-failure
env_file: ./.env
ports:
- $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
"spring.jpa.hibernate.ddl-auto" : "update"
}'
volumes:
- .m2:/root/.m2
stdin_open: true
tty: true
volumes:
db:
Dockerfile:
FROM maven:3.8.2-jdk-8
WORKDIR /springboot-backend
COPY . .
RUN mvn clean install
CMD mvn spring-boot:run
.env file:
MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=******
MYSQLDB_DATABASE=employee_management_system
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306
SPRING_LOCAL_PORT=6868
SPRING_DOCKER_PORT=8080
Any idea?
Here is folder structure:
app:
depends_on:
- mysqldb
build: ./springboot-backend
Docker compose yaml try to build springboot-backend but there is no such directory in the same path of compose yaml. Just simply you can move docker-compose yaml to outside of springboot-backend project folder.

connect ECONNREFUSED 127.0.0.1:3306 in adonis 5 docker mysql [duplicate]

This question already has an answer here:
Node.js connect to MySQL Docker container ECONNREFUSED
(1 answer)
Closed last year.
I'm using Adonis 5 and Mysql database , after following some tutorials i added this code to connect mysql. but it shows me "connect ECONNREFUSED 127.0.0.1:3306" error. Here is my code below:
My Dockerfile:
FROM node:14
RUN mkdir -p /home/node/app/node_modules
WORKDIR /home/node/app
COPY package*.json ./
# RUN apk add --no-cache git
COPY . /home/node/app/
RUN chown -R node:node /home/node
RUN npm install
USER node
EXPOSE 3333
ENTRYPOINT ["node","ace","serve","--watch"]
my docker-compose.yml file:
# For more information: https://github.com/julien-r44/adonis-sail
version: '3'
services:
mysql:
image: 'mysql/mysql-server:8.0'
container_name: mysql
expose:
- "3307"
ports:
- '${MYSQL_PORT:-3307}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${MYSQL_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${MYSQL_DB_NAME:?err}'
MYSQL_USER: '${MYSQL_USER:?err}'
MYSQL_PASSWORD: '${MYSQL_PASSWORD?:err}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
phpmyadmin:
image: 'phpmyadmin/phpmyadmin'
links:
- 'mysql:mysql'
ports:
- 8080:80
environment:
MYSQL_USERNAME: "${MYSQL_USER}"
MYSQL_ROOT_PASSWORD: "${MYSQL_PASSWORD}"
PMA_HOST: mysql
networks:
- sail
app:
links:
- mysql
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/home/node/app
- /home/node/app/node_modules
ports:
- 3333:3333
depends_on:
- mysql
- phpmyadmin
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: "local"
My .env file:
HOST=0.0.0.0
PORT=3333
APP_URL=http://${HOST}:${PORT}
NODE_ENV=development
APP_KEY=wH94pOFV47NO0dJtE_S6-TDRgcjgZXGM
SESSION_DRIVER=cookie
CACHE_VIEWS=false
DB_CONNECTION=mysql
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=adonis
MYSQL_PASSWORD=adonis
MYSQL_DB_NAME=social
Error:
it shows this error
What is my mistake?
Change from MYSQL_HOST=localhost to MYSQL_HOST=mysql
When your app is running inside a docker container, localhost points no longer to your laptop (or server) but to the container itself.
As each service is running in separated containers when one application is trying to access the database service you cannot use localhost. As localhost points to that container, and the database is not installed there.
You should use the container name instead localhost when specificying connection urls.
You can also check Docker Compose network documentation for detailed explanations.

Docker Compose Mysql init file error (Windows, Dockertoolbox)

I´m trying to setup two docker container with docker-compose, using this yml:
version: '3.5'
services:
db:
image: mysql:5.7
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: abc
MYSQL_DATABASE: shopsystem
MYSQL_USER: shoppingsystem
MYSQL_PASSWORD: abc
volumes:
- ./test.sql:/docker-entrypoint-initdb.d/test.sql
- db_data:/var/lib/mysql
backend:
build:
context: ./nodeserver
dockerfile: Dockerfile
image: node-test
container_name: servernode-1
ports:
- 8084:8082
volumes:
db_data: {}
The test.sql is on the root level like the docker-compose.yml
When I start the docker-compose
docker-compose up
I get the following error:
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/test.sql
ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device.
I already read some threads about that problem, but I can not solved it.
I clean the volumes with the following command:
docker-compose down -v
I`m using Windows with dockertoolbox to run docker-compose.
Can someone help me?
ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device.
your should to map volume as directory
and put test.sql file in there
volumes:
- myvolumes:/docker-entrypoint-initdb.d
- db_data:/var/lib/mysql
or option 2 (if you want just a single file )
version: "3.2"
services:
app:
image: app:latest
volumes:
- type: bind
source: ./bla.yaml
target: /location/bla.yaml
ref : https://docs.docker.com/storage/bind-mounts/
ref : Mount single file from volume using docker-compose

Rails app using docker-compose wont connect to the mysql container

Trying to get a rails app running using docker-compose. I run docker-compose build and it completes with no errors. I then run docker-compose up and both of the containers start. Then I run docker-compose run web rake db:create db:migrate and run into an error:
rake aborted!
Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
My Dockerfile:
FROM ruby:2.5.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs mysql-client sqlite3 zlib1g-dev libxslt-dev git && \
gem install bundler
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
Docker-compose
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
links:
- db:db
restart: always
volumes:
my-db: {}
web-app: {}
Database.yml
development:
adapter: mysql2
encoding: utf8
database: online_community_development
pool: 5
username: root
password: slumland
socket: /var/run/mysqld/mysqld.sock
host: db
New to docker so not even sure if I am issuing the commands correctly. I need to create and seed the database and have the app container connect to it. I think the issue is related to it trying to connect via localhost, but even when I changed the host to db I still got the same results. Any help would be greatly appreciated.
tl;dr:
Try this docker-compose.yml:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
DATABASE_URL: 'mysql2://db'
RAILS_ENV: 'development'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
In detail:
First of all your docker-compose-file is kinda misconfigured.
Therefore I will do a small instruction on docker-compose.yml-files before heading over to your question.
The links:-thing is a legacy feature of docker which you should strictly avoid:
https://docs.docker.com/compose/compose-file/#links
Moreover, it is not even required when you're working with docker-compose
Also, you're using expose: and ports: when defining your database-service.
The ports-key is opening up a port on your host-machine and forwards it to your docker-service:
https://docs.docker.com/compose/compose-file/#ports
Using ports-key on database-definitions makes your database accessible to the www - which is not what you want in most cases.
The expose-key opens the port locally, in the created docker network.
https://docs.docker.com/compose/compose-file/#expose
This is what we want for databases.
Editing your docker-compose.yml-file results in the following:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
Getting back to your primary problem:
rake aborted! Mysql2::Error::ConnectionError: Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")
It looks like rake expects the database to be available at localhost - therefore I think that the cause of this issue is a misconfiguration.
Unfortunately I'm not very familiar with rake but nevertheless your database.yaml looks correct.
I did some research and came across this post:
Rake tasks seem to ignore database.yml configuration
Maybe you could try to set the environment-variable DATABASE_URL in your docker-compose-file: DATABASE_URL=mysql://db
I'd also recommend to set the correct context-environment-variable: RAILS_ENV=development
When you do alle these changes you should come up with the following docker-compose.yml:
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'online_community_development'
MYSQL_ROOT_PASSWORD: '******'
MYSQL_HOST: 'localhost'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- 'my-db:/var/lib/mysql'
container_name: datab
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
DATABASE_URL: 'mysql2://db'
RAILS_ENV: 'development'
volumes:
- web-app:/myapp
ports:
- "3000:3000"
depends_on:
- db
restart: always
volumes:
my-db: {}
web-app: {}
Hope this helps.
Regards,
Hermsi
your db seems to be configured to listen on localhost only. Unfortunately this will not work since your app is technically another network host (container) within the compose network. Try making your db listen on all network interfaces.