I want to write a dockerfile where I install a custom mysqlserver with user + password.
My command looks like this at the moment:
RUN apt-get install -y mysql-server \
-y mysql-client
can I specify the input vars as running parameter of the
docker build process?
Any ideas?
thanks
Ok, so user2915097 has mentioned a nice option to you, but if you still want to create a separate image, you might proceed with the following pointers.
Figure out how to install mysql using a bash script (No manual input needed). This link might help.
In Dockerfile, either run that script in Dockerfile itself, or save the script seperatly -> use COPY to copy it in the docker image -> run the script using a Dockerfile instruction in container.
I think this is all you need to work on for now.
have a look at the reference
https://hub.docker.com/_/mysql/
I would rather begin my Dockerfile with
FROM mysql
or such .You can find on the previous link
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
which should meet your needs
Related
I am trying to get a bash script to run when my MySQL container starts. Not the initial time when there are no databases to create, but subsequent times (so placing the files in docker-entrypoint-initdb.d will not work).
My objective is to re-build my container with some database upgrade scripts (schema changes, etc). The thought being I deploy the container with the initial scripts and deploy subsequent updates with my database upgrades as the application ages. It seems like this would be an easy task, but I am having trouble.
Most of the things I have tried came from suggestions I found googling. Here are things I have tried with no success:
Modify the entrypoint.sh (and /usr/local/bin/docker-entrypoint.sh) in the Dockerfile build to add in a call to my script.
This does not even seem to be called, which I suspect is a sign, but my database starts (also note it creates my schema fine the first time)
I do this with a RUN sed in my Dockerfile and have confirmed my changes exist after the container starts
Tried running my script on startup by:
adding a script to /etc/r.d/rc.local
adding a restart cron job (well, I tried, but the Oracle Linux distro doesn’t have it)
— Modifying the /etc/bashrc
— Adding a script to /etc/profile.d/
— Appending to /etc/profie.d/sh.local
Tried adding a command to my docker-compose.yml, but it said that wasn’t found.
My actual database upgrade script works great when I log in to the container manually and execute it. All of my experiments above have been just touching a file or echoing to a file as a proof of concept. Once I get that working, I'll add in the logic to wait for MySQL to start and then run my actual script.
Dockerfile:
FROM mysql:8.0.32
VOLUME /var/lib/mysql
## these are my experiments
RUN sed -i '/main "$#"/a echo "run the script here" > /usr/tmp/XXX' /entrypoint.sh
RUN sed -i '/main "$#"/a echo "run the script here" > /usr/tmp/XXX' /usr/local/bin/docker-entrypoint.sh
RUN echo "touch /usr/tmp/XXX" >> /etc/profile.d/sh.local
RUN sed -i '/doublesourcing/a echo “run the script here > /usr/tmp/XXX' etc/bashrc
I build and run it using:
docker build -t mysql-database -f Dockerfile .
docker run -it --rm -d -p 3306:3306 --name database -v ~/Docker-Volume-Share/database:/var/lib/mysql mysql-database
Some other information that may be useful
I am using a volume on the host. I’ve run my experiments with an existing schema as well as by deleting this directory so it starts fresh
I am using mysql:8.0.32 as the image (Oracle Linux Server release 8.7)
Docker version 20.10.22, build 3a2c30b
Host OS is macOS 13.2.1
Thanks in advance for any tips and guidance!
It sounds like you are trying to run a script after the MySQL container has started and the initial setup has been completed. Here are a few suggestions:
1-Use a custom entrypoint script
You can create a custom entrypoint script that runs after the default entrypoint script included in the MySQL container image. In your Dockerfile, copy your custom entrypoint script into the container and set it as the entrypoint. Here's an example:
FROM mysql:8.0.32
COPY custom-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/custom-entrypoint.sh
ENTRYPOINT ["custom-entrypoint.sh"]
In your custom entrypoint script, you can check if the database already exists and run your upgrade script if it does. Here's an example:
#!/bin/bash
set -e
# Run the default entrypoint script
/docker-entrypoint.sh "$#"
# Check if the database already exists
if mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "use my_database"; then
# Run your upgrade script
/path/to/upgrade-script.sh
fi
2-Use a Docker Compose file
If you're using Docker Compose, you can specify a command to run after the container has started. Here's an example:
version: '3'
services:
database:
image: mysql:8.0.32
volumes:
- ~/Docker-Volume-Share/database:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: mypassword
command: >
bash -c "
/docker-entrypoint.sh mysqld &
while ! mysqladmin ping -hlocalhost --silent; do sleep 1; done;
/path/to/upgrade-script.sh
"
This command runs the default entrypoint script in the background, waits for MySQL to start, and then runs your upgrade script.
I hope these suggestions help you achieve your goal!
I'm trying to add mysql to a dockerfile. I dont want to use a mysql source docker, I'm using something else as I need ffmpeg/nvidia/asp.net aswell. So I can't simple use a different base image to start from.
So how can I
Add mysql to my docker build file?
Configure it so the data for mysql is in a specific directory (so I can can map outside the docker file)
Have mysql start up but not be the entry point service
Everything I found so far basically say "use this base image". which doesn't help me. I dont want to have mysql separate, just self contained docker image with everything it needs.
TIA
Install mysql
Use apt-get to install packages on debian distro's. Add in your dockerfile the following line:
RUN apt-get update && apt-get install -y mysql-server
Start MySQL
Add to the Dockerfile CMD a prefix where you start mysql in detached mode. Like:
CMD mysql start & # [paste here your default command]`.
This will start mysql and start your app.
Mount directories
Mounting directories is done with the -v flag:
docker run -ti -v <host_dir>:<container_dir> my-image /bin/bash
I am running Docker on a Macintosh, and have installed the MariaDB image. I would like to access it from another machine on the LAN.
I understand that the solution is to enable bind-address=0.0.0.0 (or something similar) in /etc/mysql/my.cnf. I executed docker exec -it mariadb bash, installed Joe text editor (because I am much more familiar with it than Vi or Nano), and edited the file.
The problem is that when I restart the Docker image,it has forgotten all the changes, and it doesn’t work.
Am I missing a step, or is this not the way to go about it?
Containers are throw-away by design and, as you noticed, any modifications are lost when you run fresh one.
You have two options:
First one is described here: Docker: editing my.cnf in when building the image (just mount your custom config and be done).
Second option is to make your custom container image based on official image + your modification, something like this:
Dockerfile:
# Lets say mariadb v10.3.28... Change for what you want.
FROM mariadb:10.3.28
# there is already `#bind-address=0.0.0.0` in /etc/mysql/my.cnf
# we use sed and replace it with `bind-address=0.0.0.0`)
RUN sed -i "s/#bind-address=0.0.0.0/bind-address=0.0.0.0/g" /etc/mysql/my.cnf && \
# and, for example, lets change `max_allowed_packet` too.
sed -i "s/max_allowed_packet.*/max_allowed_packet=512M/g" /etc/mysql/my.cnf;
(rule of thumbs is "make as many steps in single RUN as possible" to save image layers)
then build it:
$ cd /where/my/dockerfile/is
$ docker build . -t mymysql
and run it:
# In newer mariadb it should be `-e MARIADB_ROOT_PASSWORD=`
# And maybe you should mount datadir somewhere `-v /my/own/datadir:/var/lib/mysql`
$ docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw mymysql
I am trying to use a MySQL image on docker, attaching a volume, furthermore I would like to add a sql script in order to create a table if not present yet.
So if the container is used in another machine the table will be Always present.
My command :
docker run -d -p 3306:3306 --name my-mysql --network sma -v /scripts:/docker-entrypoint-initdb.d/ -v /myvolume/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=myDB mysql
My situation:
I am able to attach the volume with -v option (/myvolume/:/var/lib/mysql) during the run, and actually I am also able to insert the script in the init directory ( /docker-entrypoint-initdb.d/ ) but if I do these two things, only the volume attaching will work.
I guess it is something like the script is executed (because it is placed in the directory) but then the MySQL is overwritten by the volume attaching, so the only thing I am seeing is what is present in myvolume.
There is some way that makes that work?
I resolved using it in a swarm from a docker-compose with docker stack deploy -c docker-compose.yml swarm_name.
In the service definition of the docker-compose I added the command line in order to force it to execute the init script.
command: --init-file /docker-entrypoint-initdb.d/initDb.sql
I have written a dockerfile that runs mysql on an ubuntu image. The Dockerfile is:
FROM ubuntu
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
RUN sed -i '43s/.*/bind-address = 0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf
EXPOSE 3306
ENTRYPOINT service mysql start && bash
If I run:
docker run -dit mysql-server
after building the container everything works fine and my Apache/PHP container can communicate with it. However, if I run it with a volume attached (docker run -dit -v ~/vol/:/var/lib/mysql/ mysql-server) the container will stop running after 30 seconds (I'm pretty sure it's the same amount of time every time).
Does anyone know a way I can keep the container up and mount a volume? I've never had this problem before and can't find anything else online (I've been looking a while). Thanks.
This is because you are masking the contents of /var/lib/mysql with the contents of ~/vol which I'm assuming is empty. As such the MySQL server can't start as it's missing database files. I would personally use the official image over your custom implementation as it will handle what your looking for here is the link to Dockerhub. It has options for mounting your custom my.cnf file if you need those changes. However by default the image does bind to 0.0.0.0. See the Dockerhub link for config options.
Hope this helps
Dylan