Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-using existing volume with docker compose

I have setup two standalone docker containers, one runs a webserver another one runs a mysql for it. Right now I was attempting to have it working with docker-compose. All is nice and it runs well, but I was wondering how could I re-use existing volumes from the existing standalone containers that I have previously created (since I want to retain the data from them).

I saw people suggesting to use external: true command for this, but could not get the right syntax so far.

Is external: true the correct way approach for this, or should I approach this differently? Or can I just specify the path to the volume within docker-compose.yml and make it use the old existing volume?

like image 606
jonuxas Avatar asked Feb 10 '20 10:02

jonuxas


People also ask

Do Docker compose volumes persist?

There are two ways where you can create a volume, bind mounts and volumes. Whichever you choose, once you have set up a volume to the folder where the data is stored in the container, if you do a docker-compose down , and then a docker-compose up , your data will not be erased and it will become persistent.

Can I copy docker volume to another host?

You can use the -v option of docker run to copy volume data between a data volume container and the host. For example, you might want to back up the data so that you can restore it to the same data volume container or to copy it to a different data volume container.

Can two docker containers use the same volume?

Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default.

Does Docker compose down destroy volumes?

Stops containers and removes containers, networks, volumes, and images created by up .

How to use external volumes with docker-compose?

Per the documentation using the external flag allows you to use volumes created outside the scope of the docker-compose file. However it is advisable to create a fresh volume via the docker-compose file and copy the existing data from the old volumes to the new volumes Show activity on this post.

What is a named volume in Docker?

Docker named volumes Named volumes can be defined as internal (default) or external. 2.1. Docker internal named volumes Docker compose internal named volumes have the scope of a single Docker-compose file and Docker creates them if they don’t exist.

How do I preserve a docker-compose stack?

Get your docker compose stack into state you want to preserve These scripts assume you’re using docker-compose (not docker deploy ), which prepends named volumes with the current directory name. The named values are stored as bzip archives in the current folder with names ending in .tar.bz.

What are the advantages of using Docker Compose?

The main advantage of using Docker Compose is that it works in all environments such as production, staging, development, testing, as well as CI workflows. Here at Bobcares, we often use Docker Compose to manage Docker Volumes of our customers using Docker.


2 Answers

Yes you can do it normally, just an example below: Set external to true and set name to the name of the volume you want to mount.

version: "3.5"
services:
  transmission:
    image: linuxserver/transmission
    container_name: transmission
    volumes:
      - transmission-config:/config
      - /path/to/downloads:/downloads
    ports:
      - 51413:51413
      - 51413:51413/udp
    networks:
      - rede
    restart: always

networks:
  rede:
    external: true
    name: rede
volumes:
  transmission-config:
    external: true
    name: transmission-config

like image 135
Ronald Carvalho Avatar answered Sep 20 '22 10:09

Ronald Carvalho


Per the documentation using the external flag allows you to use volumes created outside the scope of the docker-compose file.

However it is advisable to create a fresh volume via the docker-compose file and copy the existing data from the old volumes to the new volumes

like image 32
Shenal Silva Avatar answered Sep 20 '22 10:09

Shenal Silva