'How to install mysql-server in a Dockerfile?

I have a complex Dockerfile which install much more than just mysql-server so I cannot start from an existing mysql container.

When removing all the extra-stuff I get this Dockerfile

FROM ubuntu:latest

ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_ROOT_USER=root
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y mysql-server

RUN usermod -d /var/lib/mysql/ mysql
RUN service mysql start 

Unfortunately, mysql does not want to start:

 ---> 57a66bd64c2c
Step 8/9 : RUN usermod -d /var/lib/mysql/ mysql
 ---> Running in 596df248c2e4
 ---> ee78442bcc56
Step 9/9 : RUN service mysql start
 ---> Running in 0d9e5803cf33
 * Starting MySQL database server mysqld
   ...fail!
The command '/bin/sh -c service mysql start' returned a non-zero code: 1

What is my mistake?



Solution 1:[1]

Looks like you've removed the most important parts of your docker file. Here is the Official MySQL repo Docker file.

    FROM oraclelinux:7-slim
    ENV PACKAGE_URL https://repo.mysql.com/yum/mysql-8.0-community/docker/x86_64/mysql-community-server-minimal-8.0.2-0.1.dmr.el7.x86_64.rpm
    
    # Install server
    RUN rpmkeys --import http://repo.mysql.com/RPM-GPG-KEY-mysql \
      && yum install -y $PACKAGE_URL \
      && yum install -y libpwquality \
      && rm -rf /var/cache/yum/*
    RUN mkdir /docker-entrypoint-initdb.d
    
    VOLUME /var/lib/mysql
    
    COPY docker-entrypoint.sh /entrypoint.sh
    ENTRYPOINT ["/entrypoint.sh"]
    
    EXPOSE 3306 33060
    CMD ["mysqld"]

You need to include a proper source with correct version to pull the image from. and expose right ports, separate out volumes for MySQL to run. your container maybe failing due to any of this. I'd say remove the MySQL part out of your dockerfile and run the rest of the container. Use the official mySQL image and install it in separate container. and then you can connect the Database with other apps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lee Goddard