Nathan Jones
Nathan Jones

Reputation: 5144

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.

Upvotes: 2

Views: 3139

Answers (1)

Alex Pavlenko
Alex Pavlenko

Reputation: 469

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/.

Upvotes: 3

Related Questions