Search code examples
djangodockerdocker-composemount

Mounted a local direcotry in my Docker image, but it can't read a file from that directory


I'm trying to build a docker container with MySql, Django, and Apache images. I have set up this docker-compose.yml ...

version: '3'

services:
  mysql:
    restart: always
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: 'maps_data'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'chicommons'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - "3406:3306"
    volumes:
      - my-db:/var/lib/mysql
    command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']

  web:
    restart: always
    build: ./web
    ports:           # to access the container from outside
      - "8000:8000"
    env_file: .env
    environment:
      DEBUG: 'true'
    command: /usr/local/bin/gunicorn maps.wsgi:application --reload -w 2 -b :8000
    volumes:
    - ./web/:/app
    depends_on:
      - mysql

  apache:
    restart: always
    build: ./apache/
    ports:
      - "9090:80"
    links:
      - web:web

volumes:
  my-db:

I would like to mount my docker Django image to a directory on my local machine so that local edits can be reflected in the docker container, which is why I have this

volumes:
- ./web/:/app

in my "web" portion. This is the web/Dockerfile I'm using ...

FROM python:3.7-slim

RUN apt-get update && apt-get install

RUN apt-get install -y libmariadb-dev-compat libmariadb-dev
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc \
    && rm -rf /var/lib/apt/lists/*

RUN python -m pip install --upgrade pip

WORKDIR /app/

COPY requirements.txt requirements.txt
RUN python -m pip install -r requirements.txt

RUN ["chmod", "+x", "/app/entrypoint.sh"]

ENTRYPOINT ["/app/entrypoint.sh"]

However, when I run things using "docker-compose up,", I get this error ...

chmod: cannot access '/app/entrypoint.sh': No such file or directory

Even though when I look in my local directory, I can see the file ...

localhost:maps davea$ ls -al web/entrypoint.sh 
-rw-r--r-- 1 davea staff 99 Mar  9 15:23 web/entrypoint.sh

I sense I haven't mapped/mounted things properly, but not sure where the issue is.


Solution

  • It seems that your docker-compose and Dockerfiles are set up correctly.

    However, 1 thing I notice is that, your entrypoint ENTRYPOINT ["/app/entrypoint.sh"] is executing the file /app/entrypoint.sh which you do not have permission to do so according to the ls -al command

    -rw-r--r-- 1 davea staff 99 Mar  9 15:23 web/entrypoint.sh
    

    There are 2 simple solutions for this:

    1. Give execution permission to the entrypoint.sh file:
    chmod a+x web/entrypoint.sh
    
    1. Or if you do not want to give this permission, you can update your entrypoint to be like ENTRYPOINT ["bash", "/app/entrypoint.sh"]

    Note that, in either case, this is not a problem with your docker-compose mounting but your Dockerfile and hence, you will need to rebuild your docker image after making the changes like

    docker-compose up -d --build