I am currently working on an api application together with Symfony.
My first step is to run a MySQL container to create the needed database.
I am using Docker-Compose in Version 3.9 and I want to install MySQL version 8+.
I wrote the needed environment arguments inside my .env:
MYSQL_DATABASE=swagger_api
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_ROOT_PASSWORD=root
MYSQL_PORT=3306
This is my docker-compose:
version: '3.9'
services:
### MySQL Container
mysql:
container_name: mysql_container
hostname: mysql_container_host
command: mysqld --default-authentication-plugin=mysql_native_password
build:
context: ./
args:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- "${MYSQL_PORT}:3306"
networks:
default:
aliases:
- mysql_alias
This is my Dockerfile:
FROM mysql:latest
MAINTAINER XXX <xxx@gmail.com>
RUN chown -R mysql:root /var/lib/mysql
ARG MYSQL_DATABASE
ARG MYSQL_USER
ARG MYSQL_PASSWORD
ARG MYSQL_ROOT_PASSWORD
ENV MYSQL_DATABASE = $MYSQL_DATABASE
ENV MYSQL_USER = $MYSQL_USER
ENV MYSQL_PASSWORD = $MYSQL_PASSWORD
ENV MYSQL_ROOT_PASSWORD = $MYSQL_ROOT_PASSWORD
#if we need to import data from sql file
#ADD data.sql /etc/mysql/data.sql
#RUN sed -i 's/MYSQL_DATABASE/'$MYSQL_DATABASE'/g' /etc/mysql/data.sql
#RUN cp /etc/mysql/data.sql /docker-entrypoint-initdb.d
EXPOSE 3306
The container is up and running. I am using a machine with MacOS Version Ventura 13.0.1.
However, when I try to log in with the set credentials inside my database client, I receive this error:
When I enter the bash of my mysql_container and echo my before set enviroment variables, everything looks fine:
What did I miss here? Any improvements or feedback? I basically want to use MySQL 8+ inside a container so I am able to connect from a client on the same machine
I used now a different approach. For my usages, I don't need a Dockerfile, since I just want to set a default user and password (for now).
I changed my docker-compose.yml to this:
version: '3.9'
services:
### MySQL Container
mysql:
container_name: mysql_container
image: mysql:latest
hostname: mysql_container_host
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports:
- "${MYSQL_PORT}:3306"
networks:
default:
aliases:
- mysql_alias
For anyone using MacOS and don't need to customize the official image, this approach works as intended: