Oleksandr Ovcharenko
Oleksandr Ovcharenko

Reputation: 35

How to create docker-compose file with mysql image to save all data inside volume

I'm trying to write docker-compose file for my Spring boot app for deploy this app on another pc. Here's mine docker-compose file.

version: "3.7"
services:
  db:
    image: messor2000/mysql
    ports:
      - "3308:3306"
    environment:
      MYSQL_DATABASE: web_library
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - /var/lib/docker/volumes/e24e69d7db152cea837b440ff02baa627d9d3e44ff9ad4c6ca3e6d1ac09f6a4e/_data

It works almost the way I want it to. I write docker-compose up command on another pc my container with the base is starting(with my bd and all tables in it), but the data from the tables were not transferred. As I know data inside my mysql tables show must be prescribed in volumes: , if this is correct please advise how to correctly spell let to my data. In my situation I have such config in my container mounts:

"Mounts": [
            {
                "Type": "volume",
                "Name": "e24e69d7db152cea837b440ff02baa627d9d3e44ff9ad4c6ca3e6d1ac09f6a4e",
                "Source": "/var/lib/docker/volumes/e24e69d7db152cea837b440ff02baa627d9d3e44ff9ad4c6ca3e6d1ac09f6a4e/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

So with path show I write to my docker compose file, to solve my problem? I also tried to write in volumes: -/var/lib/mysql Thank you very much

Upvotes: 0

Views: 5242

Answers (1)

David Maze
David Maze

Reputation: 158714

You should almost never directly refer to paths in /var/lib/docker. These are in a part of the filesystem managed by Docker and the exact paths and filesystem contents are subject to change between Docker releases.

If you're trying to directly transplant the database data from one machine to another, you can use a bind mount to store the data directly in a host directory. This exposes you to some potential permission problems, and it can be slower on some host operating systems, but it is otherwise very straightforward.

version: '3.8'
services:
  db:
    # user: 1000  # result of `id -u`, may be needed to give R/W access
    volumes:
      - ./dbdata:/var/lib/mysql
      # ^^ host system relative path
here$ docker-compose down
here$ tar cvf myapp.tar docker-compose.yml dbdata
here$ scp myapp.tar there:
here$ ssh there
there$ tar xzf myapp.tar
there$ docker-compose up -d

You can also use a Docker named volume, in which case Docker manages the storage for you. While the Docker documentation is extremely enthusiastic about named volumes, tasks like backing up and restoring named volumes require complex steps with temporary containers.

version: '3.8'
services:
  db:
    volumes:
      - dbdata:/var/lib/mysql
      # ^^^^^^ no slashes; matches top-level volumes: block
volumes:
  dbdata:
here$ docker-compose down
here$ docker-compose run -w /var/lib/mysql db \
> tar cf - . \
> > dbdata.tar
here$ scp docker-compose.yml dbdata.tar there:
here$ ssh there
there$ docker-compose run -w /var/lib/mysql db \
> tar xf - . \
> < dbdata.tar
there$ docker-compose up -d

In both cases, note that we've completely ignored the /var/lib/docker directory. Also note that the volumes: mount explicitly names the /var/lib/mysql directory in the container as the mount point.

Upvotes: 3

Related Questions