0

I have created a docker image, and I have problem to start automatically or even start mysql. The problem is echo "$@" in entrypoint.sh, I expect to run mysqld_safe, but It doesn't. If a change the line echo "$@" to mysqld_safe --user=mysql it starts successfully. I don't know what is the correct form for run automatically as mysql_safe --user=mysql. What should to write in dockerfile CMD[] to run entrypoint.sh correctly.

My code is next:

-----------
Dockerfile:
-----------


## OS part
## -------
FROM debian:buster-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mysql && useradd -r -g mysql mysql

#RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr

# Add packages for testing
RUN apt-get install iproute2 iputils-ping net-tools vim traceroute less -y

## MYSQL part
##-----------
ENV PATH /usr/local/mysql/bin:$PATH
#ENV MYSQLDATA /usr/local/mysql/data

VOLUME /usr/local/mysql/

ADD  mysql-standard-4.0.27-pc-linux-gnu-i686 /usr/local/mysql
ADD  my.cnf /etc/

#RUN chown -R mysql /usr/local/mysql/ && chgrp -R mysql /usr/local/mysql/

COPY entrypoint.sh /usr/local/bin/
#RUN chmod +x /entrypoint.sh
EXPOSE 3306

ENTRYPOINT ["entrypoint.sh"]
CMD ["mysqld_safe"]


---------------
Entrypoint.sh:
---------------


#!/bin/bash
set -e

# global variable
setup_env() {
        MYSQLDATA="/usr/local/mysql/data"
        MYSQLBASE="/usr/local/mysql"
        MYSQL_ROOT_PASSWORD="password"
}

# init database
init__database_dir() {
        echo "Change rights ..."
        ls -la  "$MYSQLDATA"
        chown -R mysql "$MYSQLDATA"
        chgrp -R mysql "$MYSQLDATA"
        echo "Initializing database..."
        cd $MYSQLBASE
        scripts/mysql_install_db --user=mysql
}

temp_server_start() {
        mysqld_safe --user=mysql  &
}

temp_server_stop() {
        mysqladmin shutdown -uroot
        sleep 30
}

setup_db() {
        sleep 20
        echo "start password setting"
        mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION"
        echo "password ready"
}

# start mysql if it is exist.
main() {
        setup_env
        if [ -z "$(ls -A "$MYSQLDATA/mysql/")" ]; then
                init__database_dir
                temp_server_start
                setup_db
                temp_server_stop
        fi
        echo "start mysqld_safe"
        echo "$@"
}

main

dejanualex
  • 3,872
  • 6
  • 22
  • 37
  • I mean exec "$@" despite echo – szabolcssandor Oct 01 '20 at 13:40
  • 1
    Why not just run the Docker Hub `mysql` image? – David Maze Oct 01 '20 at 14:04
  • At my company running separately 17 mysql 4 and these mysql servers are part of a bigger integral system. These mysql servers are running separately on old xen virtual hosts. I plan to move them into docker host, so I managed to reduce the separately running xen hosts, and I can run these mysql on an supported host. – szabolcssandor Oct 02 '20 at 19:53

1 Answers1

1

I think you forgot to pass the parameters in your entrypoint.sh. The last line should be

main "$@"
David Maze
  • 130,717
  • 29
  • 175
  • 215
Stefano
  • 4,730
  • 1
  • 20
  • 28