Can't activate django python environment in docker django application - django

Can't understand django command in docker application.
I am trying to run command which normally would work.
source project/bin/activate
Which results in :
-bash: project/bin/activate: No such file or directory
The command would work in non docker django app for sure. Also tried :
docker-compose run web source project/bin/activate
docker-compose up source project/bin/activate
What is right command then?

Have you tried, giving absolute path for the activate file. Something like this:
~/workspace/project/bin/activate
The above might actually work.

Related

Dockerfile Customization for Google Cloud Run with Nginx and uWSGI

I'm trying to migrate a Django application from Google Kubernetes Engine to Google Cloud Run, which is fully managed. Basically, with Cloud Run, you containerize your application into a single Dockerfile and Google does the rest.
I have a Dockerfile which at one point does call a bash script via ENTRYPOINT
But I need to start Nginx and start Gunicorn. The Google Cloud Run documentation suggest starting Gunicorn like this:
CMD gunicorn -b :$PORT foo.wsgi
(let's sat my Django app is named "foo")
But I also need to start Nginx via:
CMD ["nginx", "-g", "daemon off;"]
And since only one CMD is allowed per Dockerfile, I'm not sure how to combine them.
To try to get around some of these difficulties, I was looking into using a Dockerfile to build from that already has both working and I came across this one:
https://github.com/tiangolo/meinheld-gunicorn-docker
But the paths don't quite match mine. Quoting from the documentation of that repo:
You don't need to clone the GitHub repo. You can use this image as a
base image for other images, using this in your Dockerfile:
FROM tiangolo/meinheld-gunicorn:python3.7
COPY ./app /app It will expect a file at /app/app/main.py.
And will expect it to contain a variable app with your "WSGI"
application.
My wsgi.py file ends up at /app/foo/foo/wsgi.py and contains an application named application
But if I understand that documentation correctly, when it says it will expect the WSGI application to be named app and to be located at /app/app/main.py it's basically saying that I need to revise the path and the variable name so that when it builds the image it knows that app is called application and that instead of finding it at /app/app/main.py it will find it at /app/foo/foo/wsgi.py
I assume that I can fix the app vs application variable name by adding a line to my wsgi.py file like app = application but I'm not sure how to correct the path that Docker expects.
Can someone explain to me how to adapt this to my needs?
(Or any other way to get it working)

Execute command inside django

I've been working with the PaaS service AppFog and I was able to get my Django up and running, but the problem is that my application uses static files and this are not working because it needs to execute the collectstatic command in shell.
I've been reading about it on the internet but I wasn't able to find a proper solution. Should I make a shell file and execute it? How?
I appreciate your time.
You can execute collectstatic with the following command:
python manage.py collectstatic
This is called an administrative management command. You can find out more about these and implementing them yourself by reading https://docs.djangoproject.com/en/1.4/howto/custom-management-commands/.

Django 1.2 - Management - Command - Can't Run manage.py commands on crontab

On my project i have an app : my_app with Managment command : my_command.py
On SSH i try :
my/folder/project/and/app/python2.4 manage.py my_command all is ok
but if i try : python2.4 /my/folder/project/and/app/manage.py my_command, manage.py doesn't know my command...
i try to run my command on a crontab..
Thx
laurent
In my experience I had this kind of issues for several reasons.
I'd check first the python interpreter used. If you are using virtualenv or something like that you should ensure you are using the correct python executable.
If your server has selinux, you should ensure it's not denying the cron to read some files.
I also had an issue like this because the settings file (I used a separate setting file to make it less verbose) didn't exist.

Running non-django commands from a sub-directory for a Django project hosted on Heroku?

I've deployed a Django application on Heroku. The application by itself works fine. I can run commands such as heroku run python project/manage.py syncdband heroku run python project/manage.py shell and this works well.
My Django project makes use of the Python web scraping library called Scrapy. Scrapy comes with a command called scrapy crawl abc which helps me scrape websites I have defined in the scrapy application. When I run a scrapy command such as scrapy crawl spidername on my local machine, the application is able to scrape date and copy it to my database. However when I run the same command on Heroku under a sub-directory of my project directory heroku run scrapy crawl spidername, nothing happens.
I don't see anything in the Heroku logs which can point to where I'm going wrong:
2012-01-26T15:45:38+00:00 heroku[run.1]: State changed from created to starting
2012-01-26T15:45:43+00:00 app[run.1]: Awaiting client
2012-01-26T15:45:43+00:00 app[run.1]: Starting process with command `project/spiderMainDir scrapy crawl spidername`
2012-01-26T15:45:44+00:00 heroku[run.1]: State changed from starting to up
2012-01-26T15:45:46+00:00 heroku[run.1]: State changed from up to complete
2012-01-26T15:45:46+00:00 heroku[run.1]: Process exited
Some additional information:
My scrapy app calls pipelines.py to save the scraped items to the database. In the pipelines.py file, this is what I've written to invoke the Django settings so that I can import my models and save data to the database from the scrapy application.
import os,sys
PROJECT_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(PROJECT_PATH)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
Any pointers on where exactly am I going wrong? How do I execute the scrapy command on Heroku such that my application can scrape an external website and save that data to the database. Isn't the way external commands are run in Heroku like - heroku run command?
I'm answering my own question because I discovered what the problem was. Heroku for some reason was not able to find scrapy when I executed the command from a sub-directory and not the top-level directory.
The command heroku run ... is generally run from the top-level directory. For my project which uses scrapy, I was required to go to a sub-directory and run the scrapy command from the sub-directory (this is how scrapy is designed). This wasn't working in Heroku. So I went to the Heroku bash by typing heroku run bash to see what was going on. When I ran the scrapy command from the top-level directory, Heroku recognized the command but when I went to a sub-directory, it failed to recognize the scrapy command. I suppose there is some problem related to the path. From the sub-directory, I had to specify the complete path to scrapy (~/bin/scrapy crawl spidername) to be able to execute it.
To run the scrapy command without going to the Heroku bash manually each time, my work around this problem was that I created a shell script containing the following code and put it under the bin directory of my top-level directory and pushed the changes to Heroku.
bin/scrapy.sh :
#!/usr/bin/env bash
cd ~/project/spiderSubDirectory
~/bin/scrapy $#
After this was done, I could execute $ heroku run scrapy.sh crawl spidername from my local bash. I suppose its not the best solution but this works.
Isn't the way external commands are run in Heroku like - heroku run
appdir command?
It's actually heroku run command. By including your appdir in there, it resulted in an invalid command. Heroku's output doesn't give useful error messages when these commands fail, and instead just tells you that the command finished which is what you're seeing. So for you, just change the command to something like:
heroku run scrapy crawl spidername

Django management task won't work on CentOS in crontab or outside project directory

On my local machine (Mac OSX 10.6) I wrote a django custom admin command which works great. I can use it both within and outside my project directory just fine. For some reason on my CentOS 5.6 server, it won't work from outside the project directory. This is really annoying since using this custom admin command in a cron job requires it to run from the home directory.
in short:
When I run "python ./manage.py scrape" or "python manage.py scrape", everything is fine.
When I run "python /home/[username]/webapps/myproject/manage.py scrape" or "python myproject/manage.py scrape", I get the following error:
unknown command: 'scrape'
Type 'manage.py help' for usage.
On CentOS, when I run manage.py help inside the project directory, scrape shows up as a command; but if I run it outside the project directory, scrape does not appear as a valid command. On OS-X scrape appears as a valid command regardless of where I run manage.py help from.
Any idea how I can fix this?
I know CentOS ships with Python 2.4, so is your code running on 2.4 or are you using a contained environment, this is usually fixed by adding your PYTHONPATH correctly
import sys
print sys.path
verify such for starters
This should get you up and running: http://djangosnippets.org/snippets/374/