简体   繁体   中英

How do I copy a directory from one container to another container in Docker Compose?

I have two docker-compose services nginx and django , and in my command shell script I perform a gulp task which builds my static files. After the build process is complete, I want to perform a copy command to copy those files to the nginx container, but I'm not sure what command to use.

docker-compose.yml

django:
  build:
    context: .
    dockerfile: ./compose/django/Dockerfile
  user: django
  depends_on:
    - postgres
    - redis
  command: /gunicorn.sh
  env_file: .env

nginx:
  build: ./compose/nginx
  depends_on:
    - django

  ports:
    - "0.0.0.0:80:80"

gunicorn.sh

#!/bin/sh
gulp build
python manage.py migrate
/usr/local/bin/gunicorn config.wsgi -w 4 -b 0.0.0.0:5000 --chdir=/app

I'm guessing I could use scp within gunicorn.sh , but i'm not sure how I would reference the nginx service's IP address.

Just share volumes between containers, there is no need to actually copy the files.

There are multiple ways to define the volumes, but I suppose you don't want these files to be on your host file system.

Try something like the following:

volumes:
  - static:

django:
  build:
    context: .
    dockerfile: ./compose/django/Dockerfile
  user: django
  depends_on:
    - postgres
    - redis
  command: /gunicorn.sh
  volumes:
    - static:/path/to/your/static/files
  env_file: .env

nginx:
  build: ./compose/nginx
  depends_on:
    - django
  volumes:
    - static:/some/dir/to/serve/files/from/
  ports:
    - "0.0.0.0:80:80"

And then configure nginx to serve your static files from /some/dir/to/serve/files/from/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM