Do I need to use virtual environment while using docker? - django

I didn't know what is the benefit of Docker over virtual environment. Is it necessary to use virtual environment and docker at the same time.
Before today, Basically I used virtual environment to crate Django project. But today My friend recommended me to use docker. I'm confused what should i use ?
I used this command to create virtual environment
python3 -m venv virtual_environment_name
Is this is best way to create virtual environment or should i use another way to create virtual environment

I would rather use pipenv to replace virtualenv at local development environment, and docker without virtual environment at production. Here is my Dockerfile(run django with gunicorn):
FROM python:3.6
ENV PYTHONUNBUFFERED 1
# switch system download source
RUN python -c "s='mirrors.163.com';import re;from pathlib import Path;p=Path('/etc/apt/sources.list');p.write_text(re.sub(r'(deb|security)\.debian\.org', s, p.read_text()))"
RUN apt-get update
# aliyun source for pip
RUN python -c "s='mirrors.aliyun.com';from pathlib import Path;p=Path.home()/'.pip';p.mkdir();(p/'pip.conf').write_text(f'[global]\nindex-url=https://{s}/pypi/simple\n[install]\ntrusted-host={s}\n')"
# Optional: install and conf vim, install ipython
RUN apt-get install -y vim
RUN wget https://raw.githubusercontent.com/waketzheng/carstino/master/.vimrc
RUN pip install ipython
# copy source code to docker image
WORKDIR /carrot
ADD . .
# required packages for carrot
RUN apt-get install -y ruby-sass
# install gunicorn and Pipfile
RUN pip install pipenv gunicorn
RUN pipenv install --system
RUN python manage.py collectstatic --noinput
# database name and rpc server ip
ENV POSTGRES_HOST=db
ENV RPC_SERVER_IP=172.21.0.2
EXPOSE 9000
# the PASSWORD env should be replace to a real one
CMD ["gunicorn", "--bind", ":9000", "--env", "PASSWORD=123456", "--error-logfile", "gunicorn.error", "--log-file", "gunicorn.log", "carrot.wsgi:application"]

Related

what is the most efficient way of running PLaywright on Azure App Service

I am hosting Django application on Azure that contains 4 Docker images: Django, React, Celery beats and Celery worker. I have celery task where user can set up python file and run Playwright.
Question
What is the best way to run Playwright. As you can see in my Dockerfile below I am installing chromium usuing playwright install but I am not sure if it is the best approach for this solution:
FROM python:3.9
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
RUN apt-get update && apt-get -y install netcat && apt-get -y install gettext
RUN mkdir /code
COPY . /code/
WORKDIR /code
RUN pip install --no-cache-dir git+https://github.com/ByteInternet/pip-install-privates.git#master#egg=pip-install-privates
RUN pip install --upgrade pip
RUN pip_install_privates --token ${GITHUB_TOKEN} /code/requirements.txt
RUN playwright install --with-deps chromium
RUN playwright install-deps
RUN touch /code/logs/celery.log
RUN chmod +x /code/logs/celery.log
EXPOSE 80

pip3 is installing django globally not inside my environment

I used virtualenvwrapper to create a new env but when I tried to install pip3 to install newer versions of django it was install globally although my environment was activated , which leads to global installation of django ..
How can I use it only inside my virtual env
did you activated virtual env first ? If you see env name in front of name in terminal - it is activated. If it is activated try python3 -m pip install --upgrade pip
then python3 -m pip install django
it is obliged to install last stable version of django (3rd for now) I'm doing this often
Use below commands to install django inside virtual environment.
1) upgrade pip3:
python3 -m pip install --upgrade pip
2) Install virtual env
pip3 install virtualenv
3) You can then create a virtualenv using the full path like this:
virtualenv -p /home/example_username/opt/python-3.6.2/bin/python3 venv
4) Activate virtual env
source venv/bin/activate
5) install Django
pip3 install Django
1.create a virtualenvironment with virtualenv or venv.
2.Activate the virtual environment by entering in the virtual environment folder and type this command if you are on windows cd scripts then type activate.bat or if you are using git bash just do . scripts/activate then you will see ("name of your virtual environment") which proves that your virtual environment is active.
3.Then you can pip install django in your virtual environment
some images below to guide you
enter image description here
enter image description here
you are welcomed!!!

Error installing/initiating pgadmin4 SQLAlchemy 1.1.0 or greater is required

i'm installing pgadmin4 in Mint 19, following this instructions:
https://linuxhint.com/install-pgadmin4-ubuntu/
When i come to run:
https://linuxhint.com/install-pgadmin4-ubuntu/
I have an error:
File "/home/nico/pgadamin4/pgAdmin4/local/lib/python2.7/site-packages/alembic/util/__init__.py", line 31, in <module>
raise CommandError("SQLAlchemy 1.1.0 or greater is required. ")
alembic.util.exc.CommandError: SQLAlchemy 1.1.0 or greater is required
at this point, i don't know how to proceed. I don't really understand the whole installing process, using the virtual environment, so i can't add more information. Anyone can help? Thanks!
Why are you doing it manually?
pgAdmin4 now comes as native linux package.
Goto postgres site, Select your Ubuntu version as "Ubuntu Bionic" (which your Linux mint is based on).
https://www.postgresql.org/download/linux/ubuntu/
Add postgres repo as show on above link, and then do
sudo apt-get update && sudo apt-get install pgadmin4
For pgAdmin 4 v4.21 on Ubuntu, according to the download page:
Install dependencies, create a virtual environment, download, install & configure
Using Python2.x
sudo apt-get install virtualenv python-pip libpq-dev python-dev
cd
virtualenv pgadmin4
cd pgadmin4
source bin/activate
pip install https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v4.21/pip/pgadmin4-4.21-py2.py3-none-any.whl
Using Python3.6 (Preferred to avoid encoding related issues)
sudo apt-get install virtualenv python3-pip libpq-dev python3-dev
cd
virtualenv -p python3 pgadmin4
cd pgadmin4
source bin/activate
pip3 install https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v4.21/pip/pgadmin4-4.21-py2.py3-none-any.whl
Configure
Override default paths and set it to single-user mode in the local configuration file:
nano lib/python2.7/site-packages/pgadmin4/config_local.py
For Python3.x:
nano lib/python3.6/site-packages/pgadmin4/config_local.py
Write:
import os
DATA_DIR = os.path.realpath(os.path.expanduser(u'~/.pgadmin/'))
LOG_FILE = os.path.join(DATA_DIR, 'pgadmin4.log')
SQLITE_PATH = os.path.join(DATA_DIR, 'pgadmin4.db')
SESSION_DB_PATH = os.path.join(DATA_DIR, 'sessions')
STORAGE_DIR = os.path.join(DATA_DIR, 'storage')
SERVER_MODE = False
Run
python lib/python2.7/site-packages/pgadmin4/pgAdmin4.py
For Python3.x:
python3 lib/python3.6/site-packages/pgadmin4/pgAdmin4.py
Access
Access at http://localhost:5050
Exit
Exit with Ctrl-C
Run again
cd ~/pgadmin4
source bin/activate
python lib/python2.7/site-packages/pgadmin4/pgAdmin4.py
For Python3.6
#!/bin/bash
cd ~/pgadmin4
source bin/activate
python3 lib/python3.6/site-packages/pgadmin4/pgAdmin4.py
Make a shortcut
touch ~/pgadmin4/pgadmin4
chmod +x ~/pgadmin4/pgadmin4
nano ~/pgadmin4/pgadmin4
Write:
#!/bin/bash
cd ~/pgadmin4
source bin/activate
python lib/python2.7/site-packages/pgadmin4/pgAdmin4.py
For Python3.6
#!/bin/bash
cd ~/pgadmin4
source bin/activate
python3 lib/python3.6/site-packages/pgadmin4/pgAdmin4.py
Now you can just run it with a simpler command:
~/pgadmin4/pgadmin4
Python3 users
Replace [x] in Python3.6 with your respective version.
Conflict with pgAdmin 3 configuration
pgAdmin 4 will not start in the environment where pgAdmin 3 was previously installed and used because of incompatible configuration in the .pgadmin directory. The simplest solution is to either clear that directory or tweak config_local.py to point to a clean new .pgadmin4 directory.

ModuleNotFoundError: No module named 'flask_sqlalchemy' error observer while i do running the docker images

I am getting ModuleNotFoundError No module named 'flask_sqlalchemy' when try to docker run of builded docker images.The same python flask run using terminal is working fine for me but not in docker?
FROM python:3.6
ADD . /app
WORKDIR /app
RUN pip install flask gunicorn
EXPOSE 8000
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app"]
The good practice is to save file with installed pip packages, e.g. in requirements.txt. (preferably with only needed packages, e.g. when you are using pyevn).
This is done by pip freeze > requirements.txt (when located on local machine, in terminal).
Then, all you should do is replace RUN pip install flask gunicorn with RUN pip install -r requirements.txt and all installed packages on the local machine will be installed in the docker too.

Have created a docker that installs nginx, python, uwsgi and django. How do I test it within a VM?

I have created the following project using docker.
Here is the Dockerfile
############################################################
# Purpose : Dockerize Django App to be used in AWS EC2
# Django : 1.8.1
# OS : Ubuntu 14.04
# WebServer : nginx
# Database : Postgres inside RDS
# Python : 2.7
# VERSION : 0.1
############################################################
from ubuntu:14.04
maintainer Kim Stacks, kimcity#gmail.com
# make sure package repository is up to date
# this is commented out because it clashes with install build-essential
# run echo "deb http://archive.ubuntu.com/ubuntu trusty main universe" > /etc/apt/sources.list
run apt-get update
# run apt-get upgrade --force-yes -y
# install python
run apt-get install python --force-yes -y ## install 2.7
run apt-get install python-setuptools --force-yes -y ## for python2.7 or above
run apt-get install build-essential --force-yes -y ##
run apt-get install python-virtualenv --force-yes -y ## virtual env
run apt-get install python-dev --force-yes -y ## because ubuntu 14.04 does not have dev version of python 2
# install nginx
run apt-get install \
nginx \
--force-yes -y
## copy the nginx config files
COPY ./nginx_configuration/common.conf /etc/nginx/common.conf
COPY ./nginx_configuration/fastcgi_params /etc/nginx/fastcgi_params
COPY ./nginx_configuration/nginx.conf /etc/nginx/nginx.conf
COPY ./nginx_configuration/php.conf /etc/nginx/php.conf
COPY ./nginx_configuration/default /etc/nginx/sites-available/default
COPY ./nginx_configuration/php.example /etc/nginx/sites-available/php.example
COPY ./nginx_configuration/django-app.conf /etc/nginx/sites-available/django-app.conf
## copy the bash_rc over
# COPY ./bash_files/.bash_profile /root/.bash_profile
run /etc/init.d/nginx restart
########################################
## Install Django
## and associated python modules
########################################
# Install pip
RUN easy_install pip
# Add and install Python modules
ADD requirements.txt /src/requirements.txt
RUN cd /src; pip install -r requirements.txt
# Bundle app source
ADD ./djangoapp /src
########################################
## Remove any unwanted packages
########################################
run apt-get autoremove --force-yes -y
I have a VM that is Ubuntu14.04, so I ran the docker inside the guest OS.
docker build -t django18-python27-ubuntu1404 . ## build docker reponame dockerfilepath
docker run -ti django18-python27-ubuntu1404 sh ## run the docker and go in
Once I am inside the docker, I go to /src and then run uwsgi --ini uwsgi.ini
My question is how do I test the djangoapp inside /src of the docker container?
My host OS is Mac OS X Mavericks.
My guest OS is Ubuntu 14.04.
My docker is run inside the guest OS.
Please advise.
In your docker file define the network ports you will expose for external access like so:
EXPOSE 80
This will expos the port 80 of the container to docker.
Now you can tell docker to NAT and Forward this port to an free port of your docker host to access it. Like so:
docker run -p 80:80 ...
this will tell docker to map the host port 80 to the container port 80.
Please note that it is basically
host-port:container-port
After this you can open a browser, enter the IP of your Docker host (Ubuntu server) with port 80 and voila you are on your container ;)