How to share code between Docker containers?

I’m pretty new to Docker, and I have this task. I need to write a configuration for creating a container infrastructure to run a PHP application using nginx, PHP-FPM & MySQL. The code for the application is in a tarball on a remote server.

What I did so far:

  1. Created the fully functioning nginx, php and mysql containers.
  2. Downloaded the code manually, extracted it to a host directory and mounted to both nginx and PHP-FPM containers with bind mount.

This setup works, but I don’t want to keep the code locally. What I want is to download it during the build step of one of the containers and use it. My first idea is to use a shared volume to store downloaded code and mount this volume both to nginx and PHP-FPM containers. However, if I do it from within one of the Dockerfiles, I don’t have access to the mounted volume (volumes are mounted after the container is built). I can do it to the host filesystem, but this doesn’t seem right. What is the right way to deal with it?

Source: StackOverflow