Saturday 9 May 2020

Docker: Volume Mapping (Mount)

-v option is used to map the volume.

For example, you started a docker mysql container. Whatever the data you stored in mysql server, will reside in the container (Data stored in /var/lib/mysql folder).


When you destroy or remove the container all the data that is stored in the container also get deleted.

How to solve this problem?
By mapping the folder /var/lib/mysql to docker host folder, we can store actual data in docker host. So even though we destroy the container, data still exist in docker host.

Step 1: Share specific folder with docker.
You can configure shared paths from Docker -> Preferences... -> File Sharing.


Click on Preferences.

Resources -> FILE SHARING

Click in + button and add the folder that you want to be available to docker containers.

For example, I mounted /Users/Shared/ folder. 

Step 2: Execute below command.

docker run -v /Users/Shared/datadir:/var/lib/mysql -p 3306:3306 --name mySqlServer_1 -e MYSQL_ROOT_PASSWORD=tiger -d mysql



Above command mounts the /var/lib/mysql directory of mysql server to /Users/Shared/datadir directory. So, whatever the data you stored in mysql server is actually stored in /Users Shared/datadir folder.

Step 3: Execute the command ‘docker ps’ to see on which port mysql server is running.
$docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
bda7390cac5b        mysql               "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mySqlServer_1

As you see the output, you can observe mysql server us running on port 3306.

Step 4: Connect to MySQL server using DBeaver.

Open DBeaver.

Select DBeaver -> Database Connection.


Click on Next button.

Select MySQL and clicn on Next button.

Enter Server Host, Port, Database, User name and Password details.

Click on Finish button.

If you got any error like 'Public Key Retrieval is not allowed', refer below link and resolve it.

Previous                                                    Next                                                    Home

No comments:

Post a Comment