Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerize #47

Merged
merged 7 commits into from
Mar 9, 2017
Merged

Dockerize #47

merged 7 commits into from
Mar 9, 2017

Conversation

MikeTheCanuck
Copy link
Collaborator

@MikeTheCanuck MikeTheCanuck commented Mar 8, 2017

Summary

This enables the Django app to run in a Docker container.

I have tested these changes both with and without Docker, to ensure that the Swagger styling appears in both non-Docker and Docker container contexts.

Testing

git pull
git checkout dockerize
cd budget_proj
docker-compose up --build
Browse to an endpoint e.g. http://localhost:8000/kpm/
You should see the Swagger-styled response from the endpoint

Next Steps After Merge

I will address the Travis failures once this has been merged. Travis testing results have been inconclusive given the flurry of activity in these files between branches and merges.

@jimtyhurst
Copy link
Contributor

I see this error message:

$ docker-compose up --build
...
Successfully built fd79068dd2db
Recreating budgetproj_web_1
Attaching to budgetproj_web_1
web_1  | Traceback (most recent call last):
...
web_1  | ImportError: No module named 'rest_framework_swagger'
web_1  | [2017-03-08 22:59:54 +0000] [12] [INFO] Worker exiting (pid: 12)
web_1  | [2017-03-08 22:59:54 +0000] [9] [INFO] Shutting down: Master
web_1  | [2017-03-08 22:59:54 +0000] [9] [INFO] Reason: Worker failed to boot.
budgetproj_web_1 exited with code 3

However, when I run outside of Docker container:

$ ./manage.py runserver

I see Swagger pages at http://localhost:8000/.

Things I checked:

  • pip install -r requirements.txt inside the Dockerfile.
  • django-rest-swagger==2.1.1 in the requirements.txt file.
  • rest_framework_swagger referenced inside INSTALLED_APPS in settings.py.
  • My local environment is running with budget_venv and I did pip install -r requirements.txt there, but I assume the error is from an unfulfilled import in the container.

I am not sure why I get the error when running docker-compose. I will look at it more tonight.

@MikeTheCanuck
Copy link
Collaborator Author

Thanks @jimtyhurst. I have seen that same error in other testing but I was unable to reproduce it with the clone of the repo I did just before the PR, so I figured I must have finally flushed it out.

However, I sync'd my fork (mikethecanuck/team-budget) with the main project (hackoregon/team-budget) and tried the docker-compose up --build with that code, and I'm now getting the exact error you are. Very frustrating.

The most informative article I can find referencing that error is this one on StackOverflow, but I can't see how the virtualenv would have any impact inside the container (as you observed):
http://stackoverflow.com/questions/27369314/django-rest-framework-swagger-ui-importerror-no-module-named-rest-framework

@MikeTheCanuck
Copy link
Collaborator Author

OK, I figured it out. This has to do with the /budget_proj/requirements.txt file (which I'd abandoned as did some of you, thinking it was redundant). As with the /Data/folder, Docker can't access /requirements.txt (which at time of writing included the django-rest-swagger reference - as well as psycopyg2), but can only use /budget_proj/requirements.txt.

I'll update these in the PR here later on if someone else doesn't get to it first. (I'm on the road at the moment so I won't have access to a computer for a while - and editing these files on a phone is a mite dangerous.)

@MikeTheCanuck
Copy link
Collaborator Author

Missing dependencies in /budget_proj/requirements.txt have been added back.

Please test again, the Docker container should build and run without a hitch now. Thanks for your patience.

@jimtyhurst
Copy link
Contributor

I tested the Docker build. Looks good.

Questions

(1) Why are we using Python 3.5, rather than the latest 3.6.0?

(2) Each time the image starts, it runs the migrations and imports the data as specified in docker-entrypoint.sh, right? I don't think it should do those things. In general, the application should be configured to access an existing database, so why should we run the migrations and import the data every time the web app starts? This makes me think that the image is accessing sqlite3 locally, rather than querying the PostgreSQL database running on an AWS EC2 instance. Or does the image use whatever database is specified in project_config.py?

Testing procedure

  1. git checkout dockerize
  2. git pull
  3. pip install -r requirements.txt
  4. docker-compose up --build
  5. Browse to http://localhost:8000/
  6. Swagger page appears as expected, served from Docker image.
  7. Experimented with other endpoints: /ocrb, /kpm, /ocrb-prod, /kpm-prod, /summary, including using query parameters where allowed. API returned expected results.
  8. Stopped the container with Ctrl-C.
  9. Restarted the container: docker start budgetproj_web_1
  10. Confirmed that endpoints still act the same.
  11. Stopped the container: docker stop budgetproj_web_1
  12. Started the server locally: ./manage.py runserver
  13. Experimented with the endpoints: / (for Swagger), /ocrb, /kpm, /ocrb-prod, /kpm-prod, /summary, including using query parameters where allowed. API returned expected results.
  14. Stopped the server with Ctrl-C.

Copy link
Contributor

@jimtyhurst jimtyhurst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my questions in the Conversation tab, but I think this is working well enough to merge.

@MikeTheCanuck
Copy link
Collaborator Author

Great questions @jimtyhurst - I've framed up two of your questions as separate issues ( #50 and #51 ).

As to this question: "This makes me think that the image is accessing sqlite3 locally, rather than querying the PostgreSQL database running on an AWS EC2 instance. Or does the image use whatever database is specified in project_config.py?"

I am pretty confident that the Docker image is using the AWS EC2-hosted PostgreSQL specified in project_config.py. During testing when I'd hit the /summary/ endpoint, my browser would often hang in ways that I hadn't previously seen before we introduced the DATABASE configuration change in settings.py.

@MikeTheCanuck MikeTheCanuck merged commit bed9d82 into master Mar 9, 2017
@MikeTheCanuck MikeTheCanuck mentioned this pull request Mar 9, 2017
@MikeTheCanuck MikeTheCanuck deleted the dockerize branch March 10, 2017 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants