Confidence
Confidence

Reputation: 2303

docker compose yaml - command

I want to run a creation of a new database using mysql

this is the snippet I have in my docker-compose.yml file

mysql:
  image: mysql
  container_name: mysql-machine
  ports:
    - 3306:3306
  environment:
    MYSQL_ALLOW_EMPTY_PASSWORD: true
    MYSQL_DATABASE: mxdb
    MYSQL_USER: mxdb
    MYSQL_PASSWORD: mxdb
  command: mysqladmin create testing_db

Now when i run docker-compose up

I watch the console, and it says

mysql_1    | mysqladmin: connect to server at 'localhost' failed
mysql_1    | error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
mysql_1    | Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

So how do I re-write the command piece, so i get the service working properly?

I want to create more than one database. So manually using commands is the easiest way.

Upvotes: 1

Views: 15028

Answers (3)

Ervis Zyka
Ervis Zyka

Reputation: 506

I think you should run the mysql container and then have another container do the data import. Example:

.credentials

MYSQL_ALLOW_EMPTY_PASSWORD=true
MYSQL_USER=mxdb
MYSQL_PASSWORD=mxdb
MYSQL_DATABASE=mxdb

Run the container:

docker run --name mydb -d --env-file .credentials mysql

If you want to import data from file, create a new container, link to the one that is already running and do the import:

docker run --rm -t --link mydb:DB -v /path/to/dump.sql:/dump.sql mysql bash -c "mysql -h DB -u mxdb -pmxdb mxdb < /dump.sql"

If you just want to run a command, use:

docker run --rm -t --link mydb:DB mysql mysql -h DB -u mxdb -pmxdb -e "CREATE DATABASE bar"

or

docker exec -t mydb mysql -u mxdb -pmxdb -e "CREATE DATABASE bar"

Upvotes: 1

haradwaith
haradwaith

Reputation: 2731

I don't think you should override the command to create the database.

In docker-compose, the command should be the command to start the given service in the docker image. In your case, the service is a MySQL server. If you gives a command for the mysql service in your docker_compose.yml, the MySQL server will never start.

What you should do is start the mysql service, and then run commands in it.

mysql:
  image: mysql
  container_name: mysql-machine
  ports:
    - 3306:3306
  environment:
    MYSQL_ALLOW_EMPTY_PASSWORD: true
    MYSQL_DATABASE: mxdb
    MYSQL_USER: mxdb
    MYSQL_PASSWORD: mxdb

Start the service:

docker-compose up

Connect to it:

mysql -umxdb -pmxdb

Then create the database:

create database testing_db;

If you need to automatize this database creation, you could put these SQL commands in a file, and do when needed:

cat init_db.sql | mysql -umxdb -pmxdb

Upvotes: 1

Radhwane Chebaane
Radhwane Chebaane

Reputation: 864

If you use this image of mysql: tutum/mysql You can add the name of the database you want to create at startup as an environment variable:

environment:

-ON_CREATE_DB="newdatabase"


Another solution is to put a shell script on the command part. In the script you start mysql and then create databases and add users:

command : run.sh

And on you script:

/usr/bin/mysqld_safe &

mysqladmin create testing_db

Upvotes: 0

Related Questions