1

I'm trying to prepare a development container with Python + Flask and Postgre.

Since it is a development container, it is meant to be productive, so I don't want to run a build each time I change a file, so I can't COPY the files in the build phase, instead I mount a volume with all the source files, so when I change a python file in the host machine, the Flask server will automatically detect the changes and restart itself, even though it is in the container.

So far so good, running docker-compose up and these containers run fine on Windows, but when I tried to run on Linux, i got:

/bin/sh: 1: ./start.sh: Permission denied

Everyplace I searched tells me to RUN chmod +x start.sh, which doesn't work, because the file doesn't exist at build phase, so I try changing to CMD, instead of RUN... but still same error.

Any ideas why? Aren't containers supposed to help with the 'works on my machine' ? Because these files work on a Windows Host, but not on a Linux Host.

Is what I am doing the right approach in order to make the file changes on the host machine reflect in the container (without a build)?

Thanks in advance!!

Below are my files:

docker-compose.yml:

version: '3'

services:
    postgres-docker:
      image: postgres:9.6
      environment:
        POSTGRES_PASSWORD: "Postgres2019!"
      ports:
        - "9091:5432"
      expose:
        - "5432"
      volumes:
        - volpostgre:/var/lib/postgresql/data
      networks:
        - app-network

    rest-server:
      build:
        context: ./projeto
      ports:
        - "9092:5000"
      depends_on:
        - postgres-docker
      volumes:
        - ./projeto:/app
      networks:
        - app-network

volumes:
  volpostgre:

networks:
  app-network:
    driver: bridge

and inside projeto folder I got the following Dockerfile

FROM python:3.8.5

WORKDIR /app

CMD ./start.sh

And in start.sh:

#!/bin/bash
pip install -r requirements.txt
python setupdatabase.py
python run.py
Bruno Brs
  • 673
  • 1
  • 6
  • 23
  • 2
    If you have troubles calling the script dirsctly use `sh start.sh`. – Klaus D. Jul 24 '20 at 05:09
  • Surely you mean `RUN chmod +x start.sh`; but your Windows file system does not have this concept, so it has nowhere to store the information that this file is executable. (My preferred solution is always to ditch Windows; for good, if you can.) – tripleee Jul 24 '20 at 05:14
  • Possible duplicate of https://stackoverflow.com/questions/52066176/how-can-i-make-a-script-executable-in-windows but the workaround is in the first comment. – tripleee Jul 24 '20 at 05:24
  • @KlausD. It worked! Thanks... I got a question, as being a Python developer, what do you think about this config? Is there a better way to make the files reflect inside the container? In the way I did, it will try to install dependencies everytime I run docker-compose up (even though all dependencies will be satisfied). – Bruno Brs Jul 24 '20 at 05:42
  • @tripleee You're right, i forgot the `chmod`, edited. – Bruno Brs Jul 24 '20 at 14:33

1 Answers1

0

One of the options that you can try is to override CMD in docker-compose.yml and first set the permission to file and then start the execute the script.

So by doing this you do not need to build docker image at all as the only thing in the image is you are setting the CMD ./start.sh

webapp:
  image: python:3.8.5
  volumes:
    - $PWD/:/app
  working_dir: /app
  command: bash -c 'chmod +x start.sh && ./start.sh'

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • I thought about that, but I'm trying to leave the container's inner configuration to its own Dockerfile... seems more encapsulated. Thanks – Bruno Brs Jul 24 '20 at 05:39
  • This still won't work if the file system is mounted from a Windows host. – tripleee Jul 24 '20 at 07:09
  • @tripleee I do not think so, as the `chmod +x file` is expected to run in the container environment not in the host, but @BrunoBL can verify on window as its working in other OS – Adiii Jul 24 '20 at 07:12