How to move docker volume to disk location?

Viewed 3563

I have a MySQL docker image running in a docker container on Ubuntu VPS. I bring up MySQL using the docker-compose up -d command via the following docker-composer.yml file

version: "3"
services:

    mysql_server:
        image: mysql:8.0.21
        restart: always
        container_name: mysql_server
        environment:
            MYSQL_DATABASE: db_name
            MYSQL_USER: db_username
            MYSQL_PASSWORD: db_password
            MYSQL_ROOT_PASSWORD: root_password
        volumes:
            - mysql_server_data:/var/lib/mysql
            - /mysql/files/conf.d:/etc/mysql/conf.d

I am having some performance issues and would like to do the following in attempt to improve performance.

  1. I want the data in the mysql_server_data volume to be mounted on /mysql/data without losing any data as this instance in running in production.

  2. I also want to mount the MySQL config file on /mysql/files so I can change the instance configuration to increase performance.

Questions

How can change the data location of a the volume from mysql_server_data to /mysql/data?

Also, how can I mount MySQL's config file on /mysql/files/conf.d to allow me to update the settings?

I tried to mount config file like this

        volumes:
            - /mysql/files/conf.d:/etc/mysql/conf.d

But that created a directory /mysql/files/conf.d with no config file.

1 Answers

To move the data: Shutdown the container with docker-compose down, then on the local file system, copy the data from mysql_server_data to /mysql/data. Then change the compose file to reflect the new location. Finally restart the container with docker-compose up.

To mount the config files, as per the docker hub documentation for MySQL, If /my/custom/config-file.cnf is the path and name of your custom configuration file, the your volume map is:

/my/custom:/etc/mysql/conf.d

Note that mapping the volume to the container does not bring the data from your container to you local, but the other way around. So if you want to have the file in the container, you must first create it on your local.

Related