2015年11月27日 星期五

Use Docker for Mysql


Run a Mysql container

  • how to run
    $ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --name
    Assign a name to the container
    -d  
    Run container in background and print container ID
    ex . docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

  

expose port

  • docker run -p 3306:<host port> --name test-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

expose port and set user/password and database

  • docker run -p 3306:3306 --name test-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=mydb -e MYSQL_USER=myuser -e MYSQL_PASSWORD=mypw -d mysql

Manage running container 

  • docker ps -a (list all containers)
  • docker rm $(docker ps -a -q)   (remove all containers)
  • docker stop $(docker ps -a -q) (stop all containers)

Link Mysql for App

  • $ docker run --name some-app --link some-mysql:mysql -d application-that-uses-mysql

Connect to MySQL from the MySQL command line client

    Description: start a mysql container linked to test-mysql and start mysql client to connect to test-mysql,  remove it after exit (--rm)
  • $ docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
    • -t Allocate a pseudo-TTY
    • -i Keep STDIN open even if not attached

Container shell access and viewing MySQL logs

  • $ docker exec -it some-mysql bash
write custom mysql dockerfile and build image and publish

2 則留言: