Sunday, April 19, 2020

Docker Assignments #10: Docker, MySQL and .env file

docker .env file used for creating process level environment variables.

  1. Create a folder structure as per above image in the host machine
  2. add following content into .env file
  3. MYSQL_ROOT_PASSWORD=mysql123
    MYSQL_DATABASE=hr
    MYSQL_USER=hruser
    MYSQL_PASSWORD=hruser123
  4. Write docker run command by setting following options
  • enable detached mode
  • set container name as "testmysql"
  • map host port number to 3500
  • set env_file
  • do bind-mount from 'node-mysql-envfile/data' to container '/var/lib/mysql'
      5. Check testmysql container up and running
      6. Use docker exec and enter testmysql  container mysql cli client with root password
       7. Check “hr” database

Step 4: 
> docker run -d \
  -p 3500:3306 \
   --env-file .env   \
   --name testmysqldata 
   --mount type=bind,source=/c/Users/rama/node-mysql-envfile/data,target=/var/lib/mysql      
   mysql:5.7
  • -d --> used to run the container in detached mode
  • -p --> map host port 3500 to container port 3306
  • --env-file --> set .env file
  • --name --> set container name
  • we can take backup of mysql container data by doing mount to /var/lib/mysql folder
Step 5: 
     > docker ps

Step 6: 
     > docker exec -it testmysql mysql -u root -p

Step7: 
     mysql> show databases; ( you must see 'hr' database in the list )
   
Happy Coding :)



No comments:

Post a Comment