Create Dockerfile with MySQL - mysql

I would like to create a Dockerfile in order to create a container that has already mysql installed and my databases created.
I have an sql folder that contains my *.sql files and a script folder that contains my db_builder.sh script that does all the work I need (create the databases, import the needed sql files, etc...).
The only thing I'm missing is to run the mysql server before the db_builder.sh script runs. Also I need to know what would be the default password of the root user.
FROM ubuntu:18.04
ADD sql src/sql
ADD scripts src/scripts
RUN apt-get update && apt-get install mysql-server -y
# somehow start mysql ???
RUN src/scripts/db_builder.sh

I solved my issue by:
1) creating the Dockerfile FROM MySQL image instead of Ubuntu image
2) splitting my db_builder.sh into two scripts:
- prepare_sql_files.sh -> which prepares the needed sql files to be imported
- db_import.sh -> which actually does the import
3) set RUN the prepare_sql_files.sh in the Dockerfile, while just placing (ADD) the db_import.sh in /docker-entrypoint-initdb.d because of this feature of the mysql docker image:
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
So my dockerfile now looks like this:
FROM mysql:latest
ADD sql /src/sql
ADD scripts /src/scripts
RUN /src/scripts/prepare_sql_files.sh
ADD /src/scripts /docker-entrypoint-initdb.d

Related

Dockerfile CMD not accepting multiple commands for mysql image

trying to run mysql 5.7 image with datadir flag and also include a bash script at CMD.
CMD ["--datadir=/data ; sh db_translations.sh"]
What ends up happening is the the container run and creates a folder for mysql called data ; sh db_translations.sh. Any idea why this is happening?
While many containers have bash /sh as an ENTRYPOINT and this would work, in the case of the mysql container, the ENTRYPOINT is already a script that just takes the CMD offered and doesn't re-evaluate this in script form.
Look at using the /docker-entrypoint-initdb.d as a location for scripts per docs - https://hub.docker.com/_/mysql, though you'll probably need to restructure your script and environment.

How to build my MySQL script and table in my container with an attached terminal and a dockerfile

Here's a sample for Mysql and I create a dockerfile as my photo here
My database needs:
Tech MySQL
Database table: user
Database fields:user_name|password|Type
However, I don't why it doesn't appear.
enter image description here
from mysql:latest
copy script.mysql
cmd bash script_mysql
I build it but get "failed to solve with frontend dockerfile.v0: failed to create LLB definition: dockerfile parse error line 2: COPY requires at least two arguments, but only one was provided. Destination could not be determined."
actualy you have got a syntax error in your docker file, for the COPY commands in docker build you need to specify the destination directory, where you want to copy the script.mysql file in container:
COPY script.mysql <destination_directory_in_container>
Add to that there are missing configurations in your Dockerfile check the mysql_images_docs form more infos.

Mysql Docker container - inserting user input on creation

I'm Creating a custom docker Image based on MySQL official image.
My docker files looks like this
# Derived from official mysql image (our base image)
FROM mysql
# Add a database
ENV MYSQL_DATABASE company
# Add the content of the sql-scripts/ directory to your image
# All scripts in docker-entrypoint-initdb.d/ are automatically
# executed during container startup
COPY ./sql-scripts/ /docker-entrypoint-initdb.d/
In the folder Sql-scrips i have 2 files:
CreateTable.sql - with a create table instruction and InsertData.sql with some insert instructions.
All works fine when i create the container.
My question is :
How can i use some external variables when inserting data into the mysql. For example i have a front end interface where user can choose the name and password( php built ) and i want to insert that info into mysql database on container creation.

How to setup multiple environments (dev\stage\production) with elastic beanstalk?

I need to do the following
Change environment variables according to the published env. Set Set up cron jobs according to the dev. I I would like to run just 1 command line "eb deploy dev" or something similar.
Use setenv
You can set environment variables with setenv. These will then be remembered for that environment.
More details: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-setenv.html
Example
For example, suppose you have created an EB environment called 'staging' and you want to set the variable DB to 'localhost', you can use:
eb setenv DB=localhost -e staging
Crons
Now that you have a different environment variables, you can check them in a script etc. to decide if the cron should be set up.
Note that the crons may not actually have access to your environment variables so you need to set those again for the cron while setting up the cron.
This is my solution to the problem, it took some time to setup but now i can do all the changes with 1 command line.
Make your own folder with all the files for all the environments.
In .ebextensions folder setup empty config files for eb.
npm runs a script named "deploy.js" together with the flag of the specific env.
The script will do the following
copy the requested env data to the empty files according to the env
git stash the changes of .ebextensions folder (eb deploys using git)
eb use env
eb deploy
So now i can tun npm run deploy:dev and everything runs

How can I make a MySQL Script to run automatically whenever a the MySQL Server reboots on a Linux environment

I'd like to automatically populate Memory tables each time the MySQL Server reboots. Is there a way I can set a trigger which is based on that event? Or a script which is run by either the Mysqd or mysqld_safe startup scripts?
Thanks in advance
You can use the below startup script for linux :
add the followin in init.d file.
vi /etc/init.d/ you have to set it executable with: chmod +x /etc/init.d/start_my_app And dont forget to add #!/bin/sh on top of that file
And put the complete location of your script in it, like /var/myscripts/test.php instead of just start_my_app
in test.php page you can have mysql query executed.
You can set a command line option "--init-file=file_name" whenever mysql start
--init-file=file_name
Command-Line Format --init-file=file_name
Option-File Format init-file
Read SQL statements from this file at startup. Each statement must be on a single line and should not include comments.
This option is unavailable if MySQL was configured with the --disable-grant-options option.
Source : Mysql developer Documentation
For More Detail
http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_init-file