Run django and cron in docker-compose - python

I have two similar dockerfiles. They differ only in the entrypoint
This is Dockerfile-cron
FROM python:3.8
WORKDIR /src
COPY requirements.txt ./
COPY requirements-dev.txt ./
COPY . ./
RUN apt-get update -y
RUN apt-get install libgl1-mesa-glx -y
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r requirements-dev.txt
RUN chmod +x ./cron.sh
CMD ./cron.sh
This is cron.sh
#!/usr/bin/env bash
* * * * * python manage.py check_subscriptions > /proc/1/fd/1 2>/proc/1/fd/2
I have cron service in docker-compose
cron:
build:
dockerfile: deploy/Dockerfile-cron
context: .
networks:
- sortif-network
When I up docker-compose I get error
cron_1 | ./cron.sh: line 2: Invoice # 1 (7).pdf: command not found

Related

failed to solve: rpc error: code = Unknown desc = failed commit on ref

I have a django application and I'm trying to run it using docker. But when I run docker-compose build the following error shows up:
> [ 1/15] FROM docker.io/library/python:3.9#sha256:d084f55e2bfeb86ae8e1f3fbac55aad813c7c343c7cbacc91ee11a2d07c32d25:
------
failed to solve: rpc error: code = Unknown desc = failed commit on ref "layer-sha256:bf48494000001a037b72870d2a6a2536f9da8bc5d1ceddd72d79f4a51fe7a60e": "layer-sha256:bf48494000001a037b72870d2a6a2536f9da8bc5d1ceddd72d79f4a51fe7a60e" failed size validation: 2623870 != 10876505: failed precondition
here is my Dockerfile:
FROM python:3.9
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN apt update && apt install -y lsb-release && apt clean all
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN apt -y update && apt -y install gettext
RUN wget -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc
RUN sudo apt-key add ACCC4CF8.asc
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg-testing main 13" | tee /etc/apt/sources.list.d/pgdg-testing.list
RUN apt -y update && apt -y install postgresql-client-13
COPY . /code/
RUN chmod +x /code/entrypoint.sh1
ENTRYPOINT ["sh", "/code/entrypoint.sh"]
here is my docker-compose.yml:
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_PORT=${POSTGRES_PORT}
volumes:
- postgres-data:/var/lib/postgresql/data
web:
build: .
command: bash -c "python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
- ./static:/static
- ./media:/media
ports:
- ${PORT}:8000
depends_on:
- db
volumes:
postgres-data:
I'm on Windows and my docker-compose version is v2.2.3 and my docker version is 20.10.12, build e91ed57.
Any idea how I can get rid of this error?
Update: I believe it was due to network issues. If you have restricted access to the internet and face this problem, you might want to use a VPN.

when creating django project via docker container it says sh: django-admin: not found

I created a docker python image on top of alpine
the problem is that when I want to start a django app it can not find django
and it is right bcz when I type pip list, it does not have django and other packages.
ps: when creating the images it shows that it is collecting django and other packages
this is the requirements.txt file
Django>=3.2.4,<3.3
djangorestframework>=3.12.4,<3.13
this is my Dockerfile:
FROM python:3.9-alpine3.13
LABEL maintainer="siavash"
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /tmp/requirements.txt
COPY ./requirements.dev.txt /tmp/requirements.dev.txt
COPY ./app /app
WORKDIR /app
EXPOSE 8000
ARG DEV=false
RUN python -m venv /py && \
/py/bin/pip install --upgrade pip && \
/py/bin/pip install -r /tmp/requirements.txt && \
if [ $DEV = "true" ]; \
then /py/bin/pip install -r /tmp/requirements.dev.txt ; \
fi && \
rm -rf /tmp && \
adduser \
--disabled-password \
--no-create-home \
django-user
ENV PATH = "/py/bin:$PATH"
USER django-user
and this is docker-compose.yml
version: "3.9"
services:
app:
build:
context: .
args:
- DEV=true
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"
and this is the command that I use:
docker-compose run --rm app sh -c "django-admin startproject app . "
BTW the image is created successfully
So the reason why this happening i believe is because of a very simple error that's hard to see 😭
ENV PATH = "/py/bin:$PATH"
should be
ENV PATH="/py/bin:$PATH"
and you might run into some django-user issue
USER django-user so you can use this one i pasted.
Everything else looks correct.
In normal cases, you should not use virtualenv inside Docker Container.
see https://stackoverflow.com/a/48562835/19886776
Inside the container there is no need to create an additional "django-user" user because the container is an isolated environment.
Below is code that creates a new Django project through a Docker Container
requirements.txt
Django>=3.2.4,<3.3
Dockerfile
FROM python:3.9-alpine3.13
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt && \
rm /tmp/requirements.txt
WORKDIR /app
docker-compose.yml
version: "3.9"
services:
app:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"
The commands to create the new project
docker-compose build
docker-compose run --rm app sh -c "django-admin startproject app ."
docker-compose up -d
To edit files that created by the docker container,
we need to fix the ownership of the new files.
sudo chown -R $USER:$USER app
Try pip3 install instead of pip install
If that doesn't work, try installing it separately in a step and check.

Django server does not start, the site does not work when starting dockerfile

I'm training in the dockerfile assembly, I can't understand why it doesn't work.
Python django project GitHub:
[https://github.com/BrianRuizy/covid19-dashboard][1]
At the moment I have such a dockerfile, who knows how to create docker files, help me figure it out and tell me what my mistake is.
FROM python:3.8-slim
RUN apt-get update && \
apt-get install -y --no-install-recommends build-essential
ENV PYTHONUNBUFFERED=1
ADD requirements.txt /
RUN pip install -r /requirements.txt
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "127.0.0.1:8000"]
You never coppied your files to the container an example would look like something like this. This also prevents as running as superuser
FROM python:3.8-slim
#INSTALL REQUIREMENTS
RUN apt-get update
RUN apt-get -y install default-libmysqlclient-dev gcc wget
# create the app user
RUN useradd -u 1000 -ms /bin/bash app && mkdir /app && chown -R app:app /app
# copy the requirements
COPY --chown=app:app ./requirements.txt /app/requirements.txt
COPY --chown=app:app ./deploy-requirements.txt /app/deploy-requirements.txt
WORKDIR /app
# and install them
RUN pip install -r requirements.txt && pip install -r deploy-requirements.txt
#####
#
# everything below runs as the 'app' user for security reasons
#
#####
USER app
#COPY APP
COPY --chown=app:app . /app
WORKDIR /app
#RUN
ENTRYPOINT gunicorn -u app -g app --bind 0.0.0.0:8000 PROJECT.asgi:application -k uvicorn.workers.UvicornWorker

How to dockerize fastText in flask

I wrote a simple flask web service to use fastText to do the prediction. I want to put them into docker. My Dockerfile is like this:
FROM python:3
WORKDIR /app
COPY . .
RUN pip3 install -r requirements.txt
RUN git clone https://github.com/facebookresearch/fastText.git /tmp/fastText && \
rm -rf /tmp/fastText/.git* && \
mv /tmp/fastText/* / && \
cd / && \
make
CMD ["python", "app.py"]
requirements.txt
Flask==0.10.0
docker-compose.yml
version: "3.7"
services:
helloworld:
build:
context: ./
ports:
- 5000:5000
When I run the docker-compose up , it comes with an error:
ModuleNotFoundError: No module named 'fasttext'
How to do fix that?
Try running those instructions instead of the make one:
$ git clone https://github.com/facebookresearch/fastText.git
$ cd fastText
$ pip install .
You have to replace your Dockerfile with the following:
FROM python:3
WORKDIR /app
COPY . .
RUN pip3 install -r requirements.txt
RUN git clone https://github.com/facebookresearch/fastText.git && \
cd fastText && \
pip install .
CMD ["python", "app.py"]
In this way, you can build fastText for python (as shown in the official documentation).

Docker Flask ModuleNotFoundError: No module named 'flask'

I'm getting the following error when executing
docker-compose up --build
web_1 | Traceback (most recent call last):
web_1 | File "glm-plotter.py", line 4, in <module>
web_1 | from flask import Flask, render_template, request, session
web_1 | ModuleNotFoundError: No module named 'flask'
glm-plotter_web_1 exited with code 1
I tried changing "Flask" to "flask" in the requirements.txt
Dockerfile
FROM continuumio/miniconda3
RUN apt-get update && apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y build-essential
COPY requirements.txt /
RUN pip3 install --trusted-host pypi.python.org -r /requirements.txt
ADD ./glm-plotter /code
WORKDIR /code
RUN ls .
CMD ["python3", "glm-plotter.py"]
docker-compose.yml
version: "3"
services:
web:
volumes:
- ~/.aws:/root/.aws
build: .
ports:
- "5000:5000"
requirements.txt
click==6.6
Flask==0.11.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
numpy==1.11.1
pandas==0.18.1
python-dateutil==2.5.3
pytz==2016.4
six==1.10.0
Werkzeug==0.11.10
glm-plotter.py
from flask import Flask, render_template, request, session
import os, json
import GLMparser
...
I created a Docker and it compiles fine. You might want to adapt it to your personal needs and add the last few lines from your Dockerfile above:
FROM library/python:3.6-stretch
COPY requirements.txt /
RUN pip install -r /requirements.txt
Please note that in the library/python images no explicit version number for python or pip is required since there is only one installed.
If you use miniconda image you have to create a new environment and activate it prior to installing the packages and run the program in your docker file. Something like:
FROM continuumio/miniconda3
RUN apt-get update && apt-get install -y python3
RUN apt-get install -y python3-pip
RUN apt-get install -y build-essential
COPY requirements.txt /
RUN ["conda", "create", "-n", "myenv", "python=3.4"]
RUN /bin/bash -c "source activate myenv && pip install --trusted-host pypi.python.org -r /requirements.txt"
ADD ./glm-plotter /code
WORKDIR /code
RUN ls .
CMD /bin/bash -c "source activate myenv && python glm-plotter.py"

Categories

Resources