Josh.F
Josh.F

Reputation: 3806

How can I run an arbitrary MySQL command from within a docker container?

I want a docker container that runs a command for it's effects, and then it's fine to stop until I run it again.

I just want to run only this, to delete expired rows:

mysql --user=$MYSQL_USER --host=$MYSQL_HOST --database=$MYSQL_DB --password=$MYSQL_PASS -se "DELETE FROM sometable WHERE expiration < NOW();"

I've tried this:

FROM ?????

ENV MYSQL_USER=$MYSQL_USER
ENV MYSQL_HOST=$MYSQL_HOST
ENV MYSQL_PASS=$MYSQL_PASS
ENV MYSQL_DB=$MYSQL_DB

ENTRYPOINT mysql --user=$MYSQL_USER --host=$MYSQL_HOST --database=$MYSQL_DB --password=$MYSQL_PASS -se "DELETE FROM sometable WHERE expiration < NOW();"

and running it with:

docker run --name some-process \
-e MYSQL_USER=$MYSQL_USER \
-e MYSQL_PASS=$MYSQL_PASS \
-e MYSQL_HOST=$MYSQL_HOST \
-e MYSQL_DB=$MYSQL_DB \
-d some-image:latest

And I've tried many permutations of this, but, I'm really lost.

Any advice what my Dockerfile and docker run command should look like to run that?


EDIT:

I've tried so many things and gotten so many errors that I think it's futile to "fix" my attempts, and instead just solicit, "how do I have a container run mysql -se "DELETE ...""

But if it helps, here are some of the errors I've been getting ( i get the first one most frequently)

ERROR 2002 (HY000): Can't connect to local MySQL server through socket 

------

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

------

Error response from daemon: No such container:

------

docker: Error response from daemon: repository my-mysql-server not found: does not exist or no pull access.

------

ERROR: mysqld failed while attempting to check config

------

error: database is uninitialized and password option is not specified

Upvotes: 1

Views: 138

Answers (1)

Andy Shinn
Andy Shinn

Reputation: 28493

You don't need to build an image to do this. You can just use the MySQL client in the official image:

docker run -it -e MYSQL_PWD=yourpass mysql:5.6 mysql -h $MYSQL_HOST --port 3306 -u youruser -e "DELETE FROM sometable WHERE expiration < NOW();"

Upvotes: 1

Related Questions