Answer a question

I have still problem with mysql installation on alpine in docker.

I need also openjdk:8u201-jdk-alpine3.9.

My dockerfile:

FROM openjdk:8u201-jdk-alpine3.9
RUN apk update && \
    apk add mysql mysql-client && \
    rm -f /var/cache/apk/* && \
    addgroup mysql mysql && \
    mkdir run/mysqld && \
    touch /var/run/mysqld/mysqld.sock && \
    touch /var/run/mysqld/mysqld.pid && \
    chown -R mysql:mysql /var/run/mysqld/mysqld.sock && \
    chown -R mysql:mysql /var/run/mysqld/mysqld.pid && \
    chmod -R 644 /var/run/mysqld/mysqld.sock && \
    apk add openrc --no-cache

But, in container: - there is no mysql service (only mariadb, but it is probably standard situation for alpine) - there is no any my.cnf file in /etc/mysql directory

When I tried mysql command, I got error:

Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (111)

When I tried mysql -h 127.0.0.1 -P 3306 -u root command, I got error:

Can't connect to MySQL server on '127.0.0.1' (115)

That same for localhost.

Answers

First of all - why do you want to install mysql-server on that specific image? I would suggest using 2 separate containers - one for the mysql database and one for the application you are developing using that openjdk image.

You can simply use this gist https://gist.github.com/karlisabele/d0bebe3d27fc44a57d1db9a9abdff45a to create a setup where your Java application can connect to mysql database using database (the service name) as hostname for mysql server.

See https://hub.docker.com/_/mysql for additional configurations on mysql image (you should define MYSQL user, password, db name etc...)


UPDATE

I updated the gist and added the environment variables necessary for the mysql to work... Replace the qwerty values with your own and you should be able to access the database from your Java application via database:3306 using the username and password provided in the environment variables

The volumes definition at the end of the file tells docker daemon that we want to create a persistent volume somewhere in the host file system and use mysql as an alias. So that when we define the database service volumes we can simply use the mysql:/var/lib/mysql and it will actually mount an obscure folder that docker created on your filesystem. This will allow the mysql database to have persistent state everytime you start the service (because it will mount the data from your host machine) and you don't have to worry about the actual location of the volume

You can see all volumes by running docker volumes ls

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐