I have a Dockerfile (seen below). The image generates without issue. With all the items installed that I would expect. However the MYSQL users and the DB that I'm "restoring" don't appear to be there. I have to exec into the instance and manually start the mysql service again, add the users, create the DB and then run the gunzip line again. I'd like this to happen when the image generates then I'd like mysql to start on the run command. Any help is appreciated.
UPDATED
So I've learned that the original version I had where I was trying to create mysql users, dbs and restoring a db doesn't work. So in research it looks like I'm supposed to put .sh or .sql files etc in /docker-entrypoint-initdb.d/ and that they should run the first time I run the container. However, that appears to not work for me so obviously I'm missing something. I'm including my .sh file below as well.
#Download base image ubuntu 16.04
FROM ubuntu:16.04
# Let the conatiner know that there is no tty
ENV DEBIAN_FRONTEND noninteractive
# Update Software repository
RUN apt-get update
# Add Language Packs
RUN apt-get install -y language-pack-en-base
# Set the locale
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
# Add Current PHP Repository
RUN apt-get install software-properties-common -y
RUN add-apt-repository -y ppa:ondrej/php
RUN add-apt-repository -y ppa:ondrej/mysql-5.6
RUN apt-get update
# Basic Requirements
RUN apt-get -y install pwgen python-setuptools curl git nano sudo unzip openssh-server openssl vim htop
RUN apt-get -y install php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-soap php7.4-zip php7.4-bcmath php7.4-memcache php7.4-mysql
RUN apt-get -y install mysql-server-5.6 mysql-client-5.6 nginx
RUN apt-get install -y supervisor && \
rm -rf /var/lib/apt/lists/*
# mysql config
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/explicit_defaults_for_timestamp = true\nbind-address = 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf
#Define the ENV variable
ENV nginx_vhost /etc/nginx/sites-available/default
ENV php_conf /etc/php/7.4/fpm/php.ini
ENV nginx_conf /etc/nginx/nginx.conf
ENV supervisor_conf /etc/supervisor/supervisord.conf
# Enable php-fpm on nginx virtualhost configuration
COPY default ${nginx_vhost}
RUN sed -i -e 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' ${php_conf} && \
echo "\ndaemon off;" >> ${nginx_conf}
#Copy supervisor configuration
COPY supervisord.conf ${supervisor_conf}
RUN mkdir -p /run/php && \
chown -R www-data:www-data /var/www/html && \
chown -R www-data:www-data /run/php
# Volume configuration
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html", "/var/lib/mysql"]
# ROOT PASSWORD
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_DATABASE=tsc_sandbox
ENV MYSQL_USER=tsc_user
ENV MYSQL_PASSWORD=tsc_user
# Copy Conf File Over
COPY web-application.conf /etc/nginx/conf.d/web-application.conf
# Handle SSL Certs
RUN mkdir /etc/nginx/ssl
COPY web-application.crt /etc/nginx/ssl/web-application.crt
COPY web-application.key /etc/nginx/ssl/web-application.key
# Setup Mysql DB
COPY init-file.sh /docker-entrypoint-initdb.d/init-file.sh
RUN chmod +x /docker-entrypoint-initdb.d/init-file.sh
# Configure Services and Port
COPY start.sh /start.sh
CMD ["./start.sh"]
COPY tsc-20200310.sql.gz /tsc-20200310.sql.gz
RUN chmod +x /tsc-20200310.sql.gz
# Network Ports
EXPOSE 80 443 3306 22
The command I'm using to run the container is:
docker run -detach -v ~/hosts/web-application:/var/www/html -p 80:80 -p 443:443 -p 3306:3306 -p 22:22 --name container_1 container_image
The command I'm using to build the image is:
docker build -t container_image .
init-file.sh
#!/bin/bash
/etc/init.d/mysql start
mysql -u root -e "CREATE USER 'tsc_user'@'%' IDENTIFIED BY 'tsc_user';"
mysql -u root -e "CREATE USER 'the_master'@'%' IDENTIFIED BY 'the_master';"
mysql -u root -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, SHOW VIEW ON *.* TO 'tsc_user'@'%';"
mysql -u root -e "GRANT SELECT ON *.* TO the_master'@'%';"
mysql -u root -e "CREATE DATABASE tsc_sandbox;"
gunzip < /tsc-20200310.sql.gz | mysql -u root tsc_sandbox;
as a note I can obviously exec into the container afterwards and run that shell file and everything works as intended. Just hoping to get it to happen when I use the docker run command.