François Romain
François Romain

Reputation: 14393

How to copy files to a Docker volume and use that volume with docker-compose

I create a Docker volume to store the json files with docker volume create my-api-files.

Here is the docker-compose file for the webservice:

version: '3'

services:
  my-api:
    image: node:alpine
    expose:
      - ${NODE_PORT}
    volumes:
      - ./:/api
      - my-api-files:/files
    working_dir: /api
    command: npm run start

volumes: 
  my-api-files: 
    external: true

Now, how can I copy the json files to the my-api-files docker volume before to start the the webservice with docker-compose up?

Upvotes: 7

Views: 13788

Answers (1)

R Y
R Y

Reputation: 465

You could run a temporary container with that volume and a bind mount to your host files and run a copy from there:

docker run --rm -it -v my-api-files:/temporary -v $PWD/jsonFileLocation:/big-data alpine cp /big-data/*.json /temporary
docker run --rm -it -v my-api-files:/test alpine ls /test

You should see your JSON files in there.

EDIT: Of course, replace $PWD/jsonFileLocation with your JSON file location and your operating system's syntax.

Upvotes: 4

Related Questions