7
  • There is a webservice running in a Docker container.
  • This webservice relies on big json files to boot.

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
François Romain
  • 13,617
  • 17
  • 89
  • 123
  • you dont need to copy. docker volume will mount files directory into my-api-files directory inside the container – prisar Feb 08 '19 at 17:38
  • 1
    I don't want to store the files inside the docker container. That's why I want to use a separate volume – François Romain Feb 08 '19 at 17:41
  • 2
    There is [recipe](https://stackoverflow.com/questions/39176561/copying-files-to-a-container-with-docker-compose) from 2017 on StackOverflow and there is an open issue [docker-compose copy file or directory to container](https://github.com/docker/compose/issues/5523) for docker-compose. AFAIK this problem does not have a direct solution but one can work around it – Alex Yu Feb 08 '19 at 17:50
  • You don't need to copy anything. You can just mount a directory on the host into a path on the container. The first part of [this post](https://stackoverflow.com/a/39181484/14357) shows you how. – spender Feb 08 '19 at 17:57

1 Answers1

4

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.

R Y
  • 455
  • 1
  • 3
  • 13