jonuxas
jonuxas

Reputation: 425

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?

Upvotes: 31

Views: 51606

Answers (3)

Shenal Silva
Shenal Silva

Reputation: 2053

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

Upvotes: 14

LTNB
LTNB

Reputation: 121

You can create a volume explicitly using the docker volume create command, or Docker can create a volume during container or service creation. When you create a volume, it is stored within a directory on the Docker host. When you mount the volume into a container, this directory is what is mounted into the container.

If your system is running, you can exec into the mysql container, copy and move it outside.

docker cp "${container_id}":/path_to_folder /path_to_server

Upvotes: 2

Ronald Carvalho
Ronald Carvalho

Reputation: 564

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

Upvotes: 44

Related Questions