I have 3 containers with postgresql, nginx and django. The last two have their own Dockerfile, I run whole system with docker-compose. I have multiple problems:
I can't run my custom nginx container, named nginx with build: ./nginx/. only image: nginx. Yes, I try docker-compose build as was suggested in thread.
Strange behavior with volumes, I mentioned theese one: - ./web:/code and nothing work, i commented it, but now Django said me:
/usr/local/lib/python3.5/site-packages/environ/environ.py:609: UserWarning: /code/translation_microservice/.env doesn't exist - if you're not configuring your environment separately, create one.
"environment separately, create one." % env_file)
Traceback (most recent call last):
File "manage.py", line 22, in
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 337, in execute
django.setup()
File "/usr/local/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/usr/local/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 986, in _gcd_import
File "", line 969, in _find_and_load
File "", line 958, in _find_and_load_unlocked
File "", line 673, in _load_unlocked
File "", line 673, in exec_module
File "", line 222, in _call_with_frames_removed
File "/code/api_controller/models.py", line 8, in
from web.translation import TranslatedLesson, TranslatedStep
ImportError: No module named 'web'
My tree of files:
.
├── docker-compose.yml
├── nginx
│ ├── Dockerfile
│ └── nginx.conf
├── __pycache__
├── README.md
├── venv
│ ├── bin
│ ├── include
│ ├── lib
│ └── pip-selfcheck.json
└── web
├── .env
├── Dockerfile
├── manage.py
├── requirements.txt
└── translation
My docker-compose file:
version: '3'
services:
site:
build: ./web
env_file: ./web/.env
# wanna do this
#volumes:
# - ./web:/code
command: python manage.py runserver 0.0.0.0:8000
proxy:
# wanna build: ./nginx/
image: nginx
ports:
- "80:80"
- "443:443"
volumes:
- /www/static
postgres:
restart: always
image: postgres:latest
ports:
- "5432:5432"
volumes:
- /var/lib/postgresql/data/
And finally my both Dockerfiles. Django Dockerfile:
FROM python:3.5
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD ./requirements.txt /code/
RUN pip install --no-cache-dir -r requirements.txt
ADD . /code/
EXPOSE 8000
CMD ["/usr/local/bin/gunicorn", "translation_microservice.wsgi", "-w", "2", "-b", ":8000", "--access-logfile", "-", "--capture-output"]
Nginx Dockerfile:
FROM nginx:latest
COPY ./nginx.conf /etc/nginx/nginx.conf
Related
I have this project structure:
└── folder
└── my_project_folder
├── my_app
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
├── .env.dev
├── docker-compose.yml
├── entrypoint.sh
├── requirements.txt
└── Dockerfile
docker-compose.yml:
version: '3.9'
services:
web:
build: .
command: python my_app/manage.py runserver 0.0.0.0:8000
volumes:
- .:/usr/src/app/
ports:
- 8000:8000
env_file:
- .env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=db_admin
- POSTGRES_PASSWORD=db_pass
- POSTGRES_DB=some_db
volumes:
postgres_data:
Dockerfile:
FROM python:3.10.0-alpine
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
RUN apk update
RUN apk add postgresql-dev gcc python3-dev musl-dev
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./entrypoint.sh .
RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
COPY . .
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
It's working, but i dont like the line python my_app/manage.py runserver 0.0.0.0:8000 in my docker-compose file.
What should i change to run manage.py from the docker folder?
I mean, how can i use python manage.py runserver 0.0.0.0:8000 (without my_app)?
In your Dockerfile, you can use WORKDIR to change your directory inside docker file:
...
COPY . .
WORKDIR "my_app"
...
Then you are inside my_app dir and you can call your command:
python manage.py ...
I am so new in Docker and I have dockerized a simple django application and this is Dockerfile :
FROM python:3.8-slim-buster
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY . .
RUN pip freeze > requirements.txt
RUN pip3 install -r requirements.txt
CMD ["python3","manage.py", "runserver","0.0.0.0:8000
and this docker-compose.yml:
version: "3.9"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- d
and this is the packages in requirements.txt :
asgiref==3.4.1
Django==3.2.9
pytz==2021.3
sqlparse==0.4.2
i have created image myapp 4 successfuly and when i tried to run this image with dcoker run myapp4 i got the following error :
Traceback (most recent call last):
File "manage.py", line 11, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 13, in main
raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
what am i missing here?
./project
/site_ts
/web
Dockerfile
requirements.txt
docker-compose.yml
my docker-compose.yml:
version: '3'
services:
web:
build: ./web
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/site_ts
ports:
- '8000:8000'
my Dockerfile:
FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /site_ts
WORKDIR /site_ts
COPY requirements.txt /site_ts/
RUN pip install --upgrade pip && pip install -r requirements.txt
ADD . /site_ts/
i write docker-compose up and take this Error:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "docker-compose", line 3, in <module>
File "compose\cli\main.py", line 67, in main
File "compose\cli\main.py", line 123, in perform_command
File "compose\cli\command.py", line 69, in project_from_options
File "compose\cli\command.py", line 132, in get_project
File "compose\cli\docker_client.py", line 43, in get_client
File "compose\cli\docker_client.py", line 170, in docker_client
File "site-packages\docker\api\client.py", line 188, in __init__
File "site-packages\docker\api\client.py", line 213, in _retrieve_server_version
docker.errors.DockerException: Error while fetching server API version: (2, 'CreateFile', 'The specified file cannot be found.')
[9356] Failed to execute script docker-compose
I have a django project running perfect in AWS Elastic Beanstalk in awsgi mode.
I want add an pipeline from Bitbucket to simplify deploy process.
bitbucket-pipelines.yml
image: atlassian/default-image:2
pipelines:
default:
- step:
name: "Build and Test"
script:
- echo "Everything is awesome!"
- apt-get update
- apt-get install -y zip
- zip application.zip application/*
- pipe: atlassian/aws-elasticbeanstalk-deploy:0.5.0
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
S3_BUCKET: $S3_BUCKET
APPLICATION_NAME: $APPLICATION_NAME
ENVIRONMENT_NAME: $APPLICATION_ENVIRONMENT
COMMAND: 'upload-only'
ZIP_FILE: 'application.zip'
artifacts:
- application.zip
- step:
name: "Deploy to Production"
deployment: production
script:
- echo "Deployment to Production!"
- pipe: atlassian/aws-elasticbeanstalk-deploy:0.5.0
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
APPLICATION_NAME: $APPLICATION_NAME
ENVIRONMENT_NAME: $APPLICATION_ENVIRONMENT
COMMAND: 'deploy-only'
WAIT: 'true'
I make a new folder called application and put all my Django project inside.
Bitbucket pipelines works fine but show 502 error.
#edit
/var/log/web.stdout.log
web: Traceback (most recent call last):
web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
web: worker.init_process()
web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
web: super().init_process()
web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process
web: self.load_wsgi()
web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
web: self.wsgi = self.app.wsgi()
web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
web: self.callable = self.load()
web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
web: return self.load_wsgiapp()
web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
web: return util.import_app(self.app_uri)
web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
web: mod = importlib.import_module(module)
web: File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
web: return _bootstrap._gcd_import(name[level:], package, level)
web: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
web: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web: File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
web: ModuleNotFoundError: No module named 'application'
.ebextensions/django_config.conf
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: 'myproject.wsgi:application'
I have a project with the following structure.
ProjectName/
├── Dockerfile
├── api/
│ ├── Dockerfile
│ └── manage.py
├── docker-compose.yml
├── frontend/
│ ├── Dockerfile
│ ├── build/
│ └── src/
└── manifests/
├── development.yml
└── production.yml
docker-compose.yml has a database image that's common between both environments, and the dev.yml and prod.yml have similar but slightly different images for production and dev.
Example: The api dev uses django and just runs python manage.py runserver, but in prod it will run gunicorn api.wsgi.
And the frontend runs npm start but in prod I want it to be based off a different image. Currently the dockerfile only works with one or the other, since the npm command is only found when I use FROM node and the nginx command only shows up when I use FROM kyma/docker-nginx.
So how can I separate these out when in different environments?
./frontend/Dockerfile:
FROM node
WORKDIR /app/frontend
COPY package.json /app/frontend
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]
# Only run this bit in production environment, and not anything above this line.
#FROM kyma/docker-nginx
#COPY build/ /var/www
#CMD 'nginx'
./api/Dockerfile:
FROM python:3.5
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app/api
COPY requirements.txt /app/api
RUN pip install -r requirements.txt
EXPOSE 8000
# Run this command in dev
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
# Run this command in prod
#CMD ["gunicorn", "api.wsgi", "-b 0.0.0.0:8000"]
./docker-compose.yml:
version: '3'
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
volumes:
node-modules:
./manifests/production.yml:
version: '3'
services:
gunicorn:
build: ./api
command: ["gunicorn", "api.wsgi", "-b", "0.0.0.0:8000"]
restart: always
volumes:
- ./api:/app/api
ports:
- "8000:8000"
depends_on:
- db
nginx:
build: ./frontend
command: ["nginx"]
restart: always
volumes:
- ./frontend:/app/frontend
- ./frontend:/var/www
- node-modules:/app/frontend/node_modules
ports:
- "80:80"
volumes:
node-modules:
./manifests/development.yml:
version: '3'
services:
django:
build: ./api
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
restart: always
volumes:
- ./api:/app/api
ports:
- "8000:8000"
depends_on:
- db
frontend:
build: ./frontend
command: ["npm", "start"]
restart: always
volumes:
- ./frontend:/app/frontend
- node-modules:/app/frontend/node_modules
ports:
- "3000:3000"
volumes:
node-modules:
You could have as an ENTRYPOINT a script running one or the other command depending of an environment variable that you can set at run time:
docker run -e env=DEV
# or
docker run -e env=PROD
You can set that same environment variable in a docker compose file.