3

I am trying to mount a folder from the host as a volume to the container. I am wondering at what step is this mounting process done - I would expect it to be during the build phase, but it seems that my folder is not mounted.

The folder structure:

 -> app
  |-> requirements.txt
 - docker
  |-> web
    |-> Dockerfile
 -> docker-compose.yml

docker-compose.yml contains the following:

version: '2'

services:
  web:
    build: ./docker/web
    volumes:
      - './app:/myapp'

The docker file of the container:

FROM ubuntu:latest
WORKDIR /myapp
RUN ls

I am mounting the app directory from the host into /myapp inside the container, the build process sets the working directory and runs ls to see the content and I am expecting my reuqiremetns.txt file to be there.

What am I doing wrong?

docker-compose v1.16.1, docker v1.16.1. I am using docker for windows.

hoonzis
  • 1,717
  • 1
  • 17
  • 32

1 Answers1

1

Your requirements.txt file isn't copied into the image at build time because it's not picked up by the context and requirements.txt is not present at build time.

Volumes are mounted at container creation time, not at build time. If you want your file to be available inside your Dockerfile (e.g. at build time) you need to include it in the context and COPY it to make it available.

From the Docker documentation here: https://docs.docker.com/engine/reference/builder/

The first thing a build process does is send the entire 
context (recursively) to the daemon

It's two issues:

  • You aren't sending your requirements file with your build context, because your Dockerfile is in a separate directory structure, so requirements.txt is not not available at build time
  • You aren't copying the file into the image before you run the ls command (COPY ./app/requirements.txt /myapp/)

If can change your directory structure to make requirements.txt available at build time, and add a COPY command to your Dockerfile before you run your ls you should see the behavior you expect during the build.

Jay Dorsey
  • 3,563
  • 2
  • 18
  • 24