What mean th error: Invalid compose project? - docker

I'm learning how to run django rest with docker. I created an image, and I it's work when I use the command: docker run -p 8000:8000 docker_django_tutorial
But now I want to run this image through a docker-compose.yml file. Here is mine (it's based on a youtube vidéo, that why I don't understand why it doesn't work f
or me):
version: '3'
services:
monapp:
image: docker_django_tutorial
ports:
- 8000:8000
networks:
- monreaseau
networks:
monreseau:
When I run docker-compose up I've got the following error:
service "monapp" refers to undefined network monreaseau: invalid compose project
Just in case, here is my Dockerfile use for my image docker_django_tutorial:
#Use the Python3.7.2 container image
FROM python:3.7.2-stretch
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
#RUN python3 manage.py runserver

Thank you for you're anwsers.
Someone help me re-write my files like this:
Docker file:
#Use the Python3.7.2 container image
FROM python:latest
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
#CMD ["python3", "manage.py", "runserver", "0.0.0.0:90"]
#RUN python3 manage.py runserver
docker-compose.yml:
version: '3'
services:
monapp:
image: docker_django_tutorial
ports:
- 90:90
build: .
command: python3 manage.py runserver 0.0.0.0:90

Related

Unable to access API created on flask server running on docker container

I'm trying to create a GET API endpoint in python flask and run the flask in Docker container but I'm unable to access the API running inside the container. I tried looking on other answers but nothing worked.
server.py
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)
Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.10.4
WORKDIR /code
ENV FLASK_APP=server.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_DEBUG="true"
EXPOSE 5000
RUN pip install --upgrade pip
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
# CMD ["python","server.py"]
CMD ["flask", "run", "--host", "0.0.0.0"]
docker-compose.yml
version: "3.9"
services:
flask-server:
build: ./flask-server/
ports:
- "5000:5000"
restart: on-failure

Docker volume is not working the way I think it should

This is my Dockerfile for a simple Django project:
FROM python:3.10.5-alpine
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
RUN adduser -D appuser
USER appuser
WORKDIR /home/appuser/
COPY requirements.txt .
RUN python -m pip install --user --no-cache-dir --disable-pip-version-check --requirement requirements.txt
COPY . .
ENTRYPOINT [ "./entrypoint.sh" ]
And Compose file:
version: "3.9"
services:
app:
build: ./app
ports:
- "8000:8000"
volumes:
- ./app:/app
restart: unless-stopped
And entrypoint.sh:
#!/bin/sh
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic --no-input
python manage.py runserver 0.0.0.0:8000
And the structure of the project:
app
|----project
|----venv
|----static
|----.dockerignore
|----Dockerfile
|----entrypoint.sh
|----manage.py
|----requirements.txt
docker-compose.yaml
Everything works except that when python manage.py migrate creates the db.sqlite3 file in the image, and python manage.py collectstatic copies static files to the static folder of the image (both in app folder), I don't see them in app folder of my host machine. Perhaps my understanding of how volumes work is not correct?

Docker-compose up works very slow

I have a web app with Django (and angular as compiled app) and I'm trying to run it using docker containers.
Here is my docker-compose.yml file:
version: '3'
services:
web:
build: ./
command: bash -c "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn decoderservice.wsgi:application --bind 0.0.0.0:8000"
ports:
- "8000:8000"
env_file:
- ./.env.dev
Dockerfile:
FROM python:3.7
WORKDIR /orionprotocolservice/
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt ./requirements.txt
RUN pip install -r requirements.txt
COPY . /orionprotocolservice/
Program works, but performance is extremely slow (about 17s for loading the main page). Routing between pages are also slow. Interestingly, if I click to some button twice, it immediately loads.
What is happening, who could help?

Django app exits for multistage build image

I am trying to build a docker image in multiple stages. My app exits immediately after getting up
My Dockerfile:
################# Builder #####################
FROM python:3.6 AS dependencies
COPY ./requirements.txt requirements.txt
RUN pip install --upgrade pip
RUN pip install --user -r requirements.txt
################# Release #####################
FROM python:3.6-alpine AS release
WORKDIR /src/
COPY . /src
COPY --from=dependencies /root/.local /root/.local/
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
RUN mv /src/wait-for /bin/wait-for
RUN chmod +x /bin/wait-for
ENV PATH=/root/.local/bin:$PATH
ENTRYPOINT [ "/entrypoint.sh" ]
My docker-compose:
version: '3.4'
services:
django_app:
build: ./app
command: sh -c "wait-for db:5432 && python manage.py collectstatic --no-input && python manage.py runserver 0.0.0.0:8000"
ports:
- "8000:8000"
env_file:
- ./.env
volumes:
- ./app:/src/
restart: on-failure
db:
image: postgres:9.6
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=${POSTGRESQL_DB_USER}
- POSTGRES_PASSWORD=${POSTGRESQL_DB_PASSWORD}
- POSTGRES_DB=${POSTGRESQL_DB_NAME}
ports:
- 5432:5432
restart: on-failure
entrypoint.sh
#! /bin/sh
cd /src/ || exit
# Run migrations
echo "RUNNING MIGRATIONS" && python manage.py migrate
# echo "COLLECT STATIC" && python manage.py collectstatic --noinput
exec "$#"
If I use django image with single stage build, everything works fine. Not able to understand the problem here.

manage.py no such file or dir

I've got this Dockerfile:
FROM python:3.6.9-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
COPY . /usr/src/app
CMD ["python", "manage.py", "run -h 0.0.0.0"] # <--
On the last line I'm getting python: can't open file 'manage.py': [Errno 2] No such file or directory
docker-compose.yml:
version: '3.7'
services:
users:
build:
context: ./users
dockerfile: Dockerfile
volumes:
- './services/users:/usr/src/app'
ports:
- 5001:5000
environment:
- FLASK_APP=project/__init__.py
- FLASK_ENV=development
I've got manage.py in the same directory as the Dockerfile (located in users dir). Where does it look for it ?
Issue solved by editing volumes:
- './services/users:/usr/src/app'
to:
volumes:
- './users:/usr/src/app'

Resources