3

Does anybody knows how to install mysql-server via dockerfile? I have written a Dockerfile, but the build ends with an error: /bin/sh: 1: /usr/bin/mysqld: not found

USER root

RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server-5.7

# Remove pre-installed database
RUN rm -rf /var/lib/mysql/*

RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf

ENV DB_USER example
ENV DB_PASSWORD example
ENV DB_NAME example
ENV VOLUME_HOME "/var/lib/mysql"

EXPOSE 3306 

RUN cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf
RUN /usr/bin/mysqld  && sleep 5 && \
     mysql -uroot -e "CREATE USER '${DB_USER}'@'%' IDENTIFIED BY '${DB_PASSWORD}'" && \
     mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${DB_USER}'@'%' WITH GRANT OPTION" &&\
     mysql -uroot -e "CREATE DATABASE ${DB_NAME}" && \
     mysqladmin -uroot shutdown
adbo
  • 767
  • 1
  • 8
  • 23
  • What base image are you using? – Jay Dorsey Nov 16 '17 at 15:07
  • 1
    FROM ubuntu:16.04 – adbo Nov 16 '17 at 15:10
  • Why not use the official image or use their Dockerfile as a base? – Shawn C. Nov 16 '17 at 15:15
  • I have configured my Jenkins to run our Build Jobs and functional Tests in a docker container. For example, when I click on the "Build Now"-Button - Jenkins will build the Dockerfile which is in Git and run the container so the Buildsteps (Jenkinsfile) can be done in this container. And know I need a mysql-server installed in this too. – adbo Nov 16 '17 at 15:22

1 Answers1

1

For an ubuntu:16.04 base image, mysqld is found in /usr/sbin, not /usr/bin

If you can add a step RUN which mysqld before your final RUN command that will show you where the mysqld executable is found. It may vary depending on which base image/distro you're using.

You can also use RUN mysqld ... without a full path, if the file is in your $PATH

You may also need to update your RUN sed line as below, adding spaces around the quoted string:

RUN sed -i -e "s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

Otherwise, you may see the following error:

The command '/bin/sh -c sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf' returned a non-zero code: 1
Jay Dorsey
  • 3,563
  • 2
  • 18
  • 24
  • 1
    you are right mysqld is found in usr/sbin. But now i get an non-zero-code: 1. Can you see anything wrong in my "Script"? – adbo Nov 16 '17 at 15:25
  • @adbo Yes, you also need to edit your `RUN sed` line, I believe. Will update my answer to note the sed error as well – Jay Dorsey Nov 16 '17 at 15:27
  • 1
    This line succeeds with no failure - I am getting the non-zero code in: RUN /usr/bin/mysqld && sleep 5 && \ mysql -uroot -e "CREATE USER '${DB_USER}'@'%' IDENTIFIED BY '${DB_PASSWORD}'" && \ mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${DB_USER}'@'%' WITH GRANT OPTION" &&\ mysql -uroot -e "CREATE DATABASE ${DB_NAME}" && \ mysqladmin -uroot shutdown – adbo Nov 16 '17 at 15:29
  • @adbo This is drifting into mysql errors, rather than docker errors but hopefully someone can jump in with some mysql experience. With a fresh/clean container I can run `mysqld --initialize`, log in, change the root user password and get everything to work. To script that out you might look here: https://stackoverflow.com/q/2995054/2892779). Also, you might look at the mysql docker image itself, versus starting this up yourself. There's a convention for setting the user/pass via ARG/ENV variables that might be easier to setup – Jay Dorsey Nov 16 '17 at 16:39