Closed borellif closed 1 year ago
I'll also attach the dockerfile and the docker-compose file that were created as part of the script:
Dockerfile:
FROM node:18-alpine
# Installing libvips-dev for sharp Compatibility
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev nasm bash vips-dev
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/
COPY package.json package-lock.json ./
RUN npm config set network-timeout 600000 -g && npm install
WORKDIR /opt/app
COPY . .
ENV PATH /opt/node_modules/.bin:$PATH
RUN chown -R node:node /opt/app
USER node
RUN ["npm", "run", "build"]
EXPOSE 1337
CMD ["npm", "run", "develop"]
docker-compose.yml
version: '3'
services:
strapi:
container_name: strapi
build: .
image: strapi:latest
restart: unless-stopped
env_file: .env
environment:
DATABASE_CLIENT: ${DATABASE_CLIENT}
DATABASE_HOST: strapiDB
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USERNAME: ${DATABASE_USERNAME}
DATABASE_PORT: ${DATABASE_PORT}
JWT_SECRET: ${JWT_SECRET}
ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
NODE_ENV: ${NODE_ENV}
volumes:
- ./config:/opt/app/config
- ./src:/opt/app/src
- ./package.json:/opt/package.json
- ./package-lock.json:/opt/package-lock.json
- ./.env:/opt/app/.env
- ./public/uploads:/opt/app/public/uploads
ports:
- '1337:1337'
networks:
- strapi
depends_on:
- strapiDB
strapiDB:
container_name: strapiDB
platform: linux/amd64 #for platform error on Apple M1 chips
restart: unless-stopped
env_file: .env
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_USER: ${DATABASE_USERNAME}
MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_DATABASE: ${DATABASE_NAME}
volumes:
- strapi-data:/var/lib/mysql
#- ./data:/var/lib/postgresql/data/ # if you want to use a bind folder
ports:
- '3306:3306'
networks:
- strapi
strapiAdminer:
container_name: strapiAdminer
image: adminer
restart: unless-stopped
ports:
- '9090:8080'
environment:
- ADMINER_DEFAULT_SERVER=strapiDB
networks:
- strapi
depends_on:
- strapiDB
volumes:
strapi-data:
networks:
strapi:
name: Strapi
driver: bridge
Seems the issue is from here
ER_HOST_NOT_PRIVILEGED: Host '172.24.0.4' is not allowed to connect to this MySQL server strapi | Error: ER_HOST_NOT_PRIVILEGED: Host '172.24.0.4' is not allowed to connect to this MySQL server
What's the docker compose file look like if I can ask? Your env looks correct etc though it seems it's the authentication in MySQL that's the issue.
After digging a bit deeper it seems that the docker mysql server will only accept connections from 'root' unless specified otherwise in the docker compose
.
Here is some documentation from MySQL when setting that up - https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-more-topics.html#docker_var_mysql-root-host
I have a potential solution in the strapiDB docker-compose configuration:
strapiDB:
container_name: strapiDB
platform: linux/amd64 #for platform error on Apple M1 chips
restart: unless-stopped
env_file: .env
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_USER: ${DATABASE_USERNAME}
MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_PASSWORD: ${DATABASE_PASSWORD}
MYSQL_DATABASE: ${DATABASE_NAME}
MYSQL_ROOT_HOST: '%'
Where MYSQL_ROOT_HOST: '%'
is new. I guess this can be added to the database.js in the /config/env
folder or in the .env
file
Seems the issue is from here
ER_HOST_NOT_PRIVILEGED: Host '172.24.0.4' is not allowed to connect to this MySQL server strapi | Error: ER_HOST_NOT_PRIVILEGED: Host '172.24.0.4' is not allowed to connect to this MySQL server
What's the docker compose file look like if I can ask? Your env looks correct etc though it seems it's the authentication in MySQL that's the issue.
Saw this too late, but the docker compose file is in my second comment. I added the environment variable of MYSQL_ROOT_HOST
. This probably can be done programmatically. Docker IPs tend to be 172.x.x.x but I did just see a 192.x.x.x range as well. This also, however, looks like it can be configured.
Thank you will add a small fix to it with % seems this is on the newest version of MySQL that it was changed so. Thank you for a detailed report and glad you got it fixed.
I have added MYSQL_ROOT_HOST: '%'
into the last update so closing this issue.
🐛 Bug Report
Hey all - I'm having trouble connecting my strapi container to my strapiDB container. My hunch is telling me it's a version issue or an environment variable not set but I'd love your help.
🤷♀️ What did you do
Ran dockerize script using default configs except with MySQL, see details below:
🎉 You, and 937 other people have used this tool this month
🍿 JavaScript project detected
📦 NPM detected
🚀 Strapi 4.9.0 detected
✔ Do you want to create a docker-compose file? 🐳 … No / Yes ✔ What environments do you want to configure? › Both ✔ Whats the name of the project? … strapi ✔ What database do you want to use? › MySQL ✔ Database Host … localhost ✔ Database Name … strapi ✔ Database Username … strapi ✔ Database Password … **** ✔ Database Port … 3306 🐳 Checking for existing Docker files...
🐳 Found: .dockerignore, Dockerfile, Dockerfile.prod in project directory.
🪄 Moving .dockerignore to backup directory...
🪄 Moving Dockerfile to backup directory...
🪄 Moving Dockerfile.prod to backup directory...
📦 Backed up .dockerignore, Dockerfile, Dockerfile.prod
🐳 Added docker-compose file with MYSQL configuration
🕵️ Added Dockerize variables in .env
💾 Added MYSQL configuration to database.js
📦 Cleaned up old dependencies
📦 MYSQL dependencies installed with NPM
🐳 Checking for existing Docker files...
💁 No Dockerfiles found in the root directory. Skipping backup.
🐳 Dockerfile for development and production added
⭐️ Star the project on GitHub if you liked this tool 🙏
🎉 We now have got 312 🌟 and counting...
👉 https://github.com/strapi-community/strapi-tool-dockerize 👈
☕️ Feeling generious, feel free to buy me a ☕️ 👇
🙏 https://opencollective.com/strapi/projects/strapi-tool-dockerize
⛔️ Error log
The error comes in when the containers try to start, specifically the strapi container:
The error states that the connection is refused and the server restarts.
🕵️♀️ Stack trace
🙇♀️ Expected behavior/code
Have the strapi and strapiDB containers be able to speak to each other and connect
👩💻 Environment
Dev environment
💡 Possible Solution
N/A
📺 Additional context/Screenshots
I did have this setup with mysql locally and the container complained that the port 3306 was already in use. I turned off my local non-docker mysql instance.
I also checked my mysql environment variables:
and my strapi env variables (before the container exits):
Finally - I changed the node version of my dockerfiles to use
node:18-alpine
Please let me know if you have questions, I appreciate your help.