How to include local libraries in build?

I have a directory structure like this:


where inc/ contains code that is common to app and www. to create an image for app I do:

  • Link php docker to host mysql
  • Docker container failing to install mongo
  • Error 'import path does not begin with hostname' when building docker with local package
  • Using ecryptfs inside Docker without --privileged. Appropriate value for --device?
  • How to set an environment variable in a running docker container
  • Configuring Kafka to accept clients both from inside and outside docker
  • cd app
    docker build .

    so the context for the build are the contents of app/. of course, the code in inc/ will be missing from the app and it breaks

    googling I’ve read suggestions that I should:

    docker build -f app/Dockerfile .

    which includes inc/ (good) but also includes everything else (bad) e.g. www

    to solve the problem I would have to build a script that copies the contents of inc/ into app/, performs the build and the deletes app/inc/. but that’s messy and, actually, I can’t do it because I want docker-compose to do my builds.

    what is the recommended way to manage this?

  • How to grant privileges on mysql from Dockerfile?
  • Exception on reloading remote spring boot application with Intellij IDEA
  • Unable to execute shell script with parameter from Dockerfile
  • Can't get clipboard forwarding working, while being in Docker under SSH
  • log tag option ignored by docker daemon
  • How to set the container name used for --link option with Docker
  • One Solution collect form web for “How to include local libraries in build?”

    but also includes everything else (bad) e.g. www

    That is where a .dockerignore file can come in handy: declare www/ in it.

    Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context.
    If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it.

    This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY.

    Wrapping docker build or docker compose to generate the right .dockerignore would be ideal for now.

    Ultimately, each build should be able to specify its own dockerignore file.
    See issue 12886.

    This feature was put on hold, pending other changes that are being researched/worked on; one of those is making the builder “smarter” when sending the build context; see #31829.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.