0

I created a Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
RUN pip install -r requirements.txt
COPY . /code/
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

and a docker-compose:

version: "3"
services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - DB:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres

  app:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - db
volumes:
  DB:

And it finally works, cool! But then I wanted to continue working on that project and It seems like I have to rebuild my app every time when I do some changes to see the result but this operation is too costly. Can I somehow trigger just the "COPY . /code/" operation?

Reema Q Khan
  • 878
  • 1
  • 7
  • 20
Murtrag
  • 31
  • 4
  • 1
    You can use an ordinary Python virtual environment for day-to-day development, and package and distribute your application via Docker if that makes sense for you. – David Maze Feb 04 '21 at 12:17
  • 1
    @DavidMaze He can also go further, and use a notepad and a pencil to write code, but this is not what the question about which makes your comment useless here. – Nairum Oct 25 '22 at 12:02

1 Answers1

1

You should use a bind mount for the code folder from your PC to the app container code folder and the changes will be reflected without a need to re-build images or re-start the containers. For example, if your local code folder is in the same directory as docker-compose, your docker-compose should look like this:

version: "3"

services:
  db:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - DB:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=postgres
  app:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - ./code:/code
    tty: true
    environment:
      - DJANGO_SETTINGS_MODULE=boo.settings
      - PYTHONPATH=$PYTHONPATH:/code/
    depends_on:
      - db
volumes:
  DB:

The section below created a volume and also reflects any changes made (binds the folder ./code on your PC to /code in the app container).

volumes:
  - ./code:/code

I also passed needed environment variables to the app container. Note that you can run the manage.py after you run the containers, you don't need to run it inside the image.

Silidrone
  • 1,471
  • 4
  • 20
  • 35
  • But this is causing problems with permissions to the /code directory "python: can't open file '/code/manage.py': [Errno 13] Permission denied" – Murtrag Feb 04 '21 at 11:12
  • In configuration with app with suggested volume this container doesn't even want to start because of mentioned permission problem, and I can't start server from the host either, because of the database configuration: django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known I can keep changing the the configuration but I it doasn't sound like a right way to do it – Murtrag Feb 04 '21 at 11:35
  • The reason it cannot translate hostname db to address is because they are not in the same docker network. And remove `CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]` from your Dockerfile and try to run (from the CLI) `python manage.py runserver 0.0.0.0:8000` when you `docker exec -it app bash` into the app container. – Silidrone Feb 04 '21 at 11:37
  • I don' believe the network configuration is necessary at all cause docker-compose by default creates it. And I can't run this container without any server running on it, but even if I manage to do it I won't be able to do anything in the /code volume because of the permissions – Murtrag Feb 04 '21 at 12:05
  • That's why you need use `tty: true` flag in the docker-compose (I'll edit the answer) to be able to run the container without any script running on it. Try running it from the CLI and see if you have the problems with permissions still, don't assume you will. – Silidrone Feb 04 '21 at 12:19
  • (Yes, Compose provides a `default` network for you, and for most applications you don't need `networks:` at all, even if you need to communicate between containers. [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation says more.) – David Maze Feb 04 '21 at 12:19
  • @DavidMaze Did not know that, thanks, edited the answer. That's not the main issue he's facing though. – Silidrone Feb 04 '21 at 12:21
  • @Murtrag You didn't provide enough information so I'm trying to help you debug the problem. If you update your question with more info I'm sure it'll be much easier to help you with your problem. – Silidrone Feb 04 '21 at 12:22
  • [murtrag@localhost Room-Booker]$ sudo docker exec -it 2033d7f32175 bash root@2033d7f32175:/code# ls ls: cannot open directory '.': Permission denied – Murtrag Feb 04 '21 at 12:26
  • This /path:/path volumes always results in Permission problems but now I guess this is something wrong with my docker. – Murtrag Feb 04 '21 at 12:33
  • @Murtrag Yeah they do, but I can't figure it out from here without some information. When I seem to have tried everything and nothing works, I do `service docker restart`, you can maybe try that, it sometimes helps but I doubt it will in the case of a permission problem. – Silidrone Feb 04 '21 at 12:46
  • I had tried to restart service didn't work, I've tried to change the owner and the group of /code from my host to root:root didn't help, from container I have no permission to do that. – Murtrag Feb 04 '21 at 13:03
  • I have found it https://stackoverflow.com/questions/24288616/permission-denied-on-accessing-host-directory-in-docker - /path:/path:Z solves the problem with permissions and everything works now \o/ – Murtrag Feb 04 '21 at 13:11
  • Ah so it was a specific problem, because SELinux was configured I guess. Well I'm glad you solved your problem. Although the title and the question drastically differs from that answer so I don't think I can edit my answer to add that, that is for another question. Consider up-voting/accepting my answer if it was of any help (and if it resolves the problem asked in the title). – Silidrone Feb 04 '21 at 13:15