1566618548
Originally published by Lewis kori at lewiskori.com
Hey there I was inspired to write this post based on my experience trying to move my deployments to use docker, particularly for django applications and couldn't get a comprehensive place/article that covered what I needed.Hopefully this article will help anyone out there who is feeling as stuck as I was.
A lot of you might have already heard this phrase being thrown around almost everywhere you turn. You probably googled the term docker up and even tried experimenting with it but might have given up along the way. Heck, to be honest I did hold up on it one or two times before taking a full dive. It can be a little intimidating at first, but oh boy! Once you start using docker, there's no going back. The ease of moving from production to development environment is simply mind blowing to say the least!!
so enough rumbling, let's get started.
Docker is an open-source tool that automates the deployment of an application inside a software container. which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system.
for detailed information on the workings of docker, I'd recommend reading this article and for those not comfortable reading long posts, this tutorial series on youtube was especially useful in introducing me to the concepts of docker.
In case you don't have docker installed on your machine follow the detailed steps below as per your operating system.
Going forwards, I've assumed you already had an existing django application, so this tutorial will just be a guide on how to containerize it.
3.ubuntu
For deploying a typical django application you would need the following services in order to get it running.
To launch each of these services, you'll need a dockerfile. This is basically a text document highlighting all the commands on the cli and steps you would normally take to assemble an image.
FROM python:3.6RUN mkdir /code
WORKDIR /codeCOPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txtCOPY . .
In order to build this image run the simple command
docker build .
on the current dockerfile location directory.
For our use case we’ll be having multiple images and running this command for every image will be tiresome. Hence the need for docker-compose, More on that as we finalize.
FROM nginxRUN rm /etc/nginx/conf.d/default.conf
COPY mysite.conf /etc/nginx/conf.d
the commands are the same as for python only specific to nginx
in this case we use the nginx base image, delete the default configuration file that ships with nginx and replace it with our custom config file.
which might look something like this
upstream my_site {
server web:8080;
}server {
listen 80; charset utf-8; server_name 127.0.0.1; client_max_body_size 4G; access_log /code/logs/nginx-access.log; error_log /code/logs/nginx-error.log; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://my_site; break; } } location /static/ { autoindex on; alias /code/static_cdn/; } location /media/ { autoindex on; alias /code/media_cdn/; }
}
the file locations will of course be relative to your own configurations.
And lastly we get to the database, in this use case, I used postgres.
FROM postgres:latestCOPY ./init/01-db_setup.sh /docker-entrypoint-initdb.d/01-db-setup.sh
and now you’re thinking
“But Lewis, what’s this init file?”
for context let’s take a look at the postgres directory within our project
postgres
├── postgres/Dockerfile
└── postgres/init
└── postgres/init/01-db_setup.sh
this is a shell script(docker entry point) specifying what commands to run on the database container, things like creating the database, users and granting privileges to the said user.
#!/bin/shpsql -U postgres -c “CREATE USER $POSTGRES_USER PASSWORD ‘$POSTGRES_PASSWORD’”
psql -U postgres -c “CREATE DATABASE $POSTGRES_DB OWNER $POSTGRES_USER”
psql -U postgres -c “GRANT ALL PRIVILEGES ON DATABASE $POSTGRES_DB TO $POSTGRES_USER”
note: when you create this file, don’t forget to make it executable by running
sudo chmod u+x filename.sh
at this point, you’ve probably noticed that we have a lot of dockerfiles,
with docker-compose, we can conveniently build all this images using
the command
docker-compose build .
First off, we’ll need to create a docker-compose.yml file within our project directory. we’ll specify the services needed for our webapp to run within this file.
version: ‘3’
services:web:
build: . container_name: great volumes: - .:/code - static:/code/static_cdn - media:/code/media_cdn depends_on: - postgres expose: - 8080 command: bash -c "python manage.py collectstatic --no-input && python manage.py makemigrations && python manage.py migrate && gunicorn --workers=3 projectname.wsgi -b 0.0.0.0:8080"
postgres:
build: ./postgres
restart: unless-stopped
expose:
- “5432”
environment: # will be used by the init script
LC_ALL: C.UTF-8
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassowrd.
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data/nginx:
restart: always
build: ./nginx/
volumes:
- ./nginx/:/etc/nginx/conf.d
- ./logs/:/code/logs
- static:/code/static_cdn
- media:/code/media_cdn
ports:
- “1221:80”
links:
- web
volumes:
pgdata:
media:
static:
Going through this commands line by line:
To build the images, it’s now a matter of simply running
docker-compose build
This might take a few minutes to build as the base images are downloading in case you didn’t have them locally,
To start the various service containers, simply run
docker-compose up
or if you want to specify which compose file to run in case of multiple docker-compose files within one directory
docker-compose -f filename.yml up
DISCLAIMER: Don’t forget to set the Debug = False and allowed hosts, in the settings.py file of django to reflect the domain name or ip-address you’ll be using.
In addition to this, change the database from the default sqlite3 that comes with django to reflect the database and usernames we specified
in the environment section of the postgres service like so
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,
‘NAME’: ‘mydb’,
‘USER’: ‘myuser’,
‘PASSWORD’: ‘mypassword’,
‘HOST’: ‘postgres’,
‘PORT’: 5432,
}
}
and that’s it.
To view the running site, run
in case you want to stop the containers
docker-compose stop
to start the stopped containers
docker-compose start
to destroy the containers
docker-compose down
you made changes to the docker-files and need those changes applied
docker-compose down && docker-compose build && docker-compose up
Now to get the site up and running on the web, simply create a configuration file for your local machines nginx(or apache) on the web server and simply point it to the docker-container running your django app. In this case you’ll point it to the nginx container.
127.0.0.1:1221
To get a list of common commands you’ll need for docker, read this concise post
for laravel developers
here’s something to get you started with docker
Thank you very much for your time and I hope this article was useful. If you want more of this , feel free to contact me
Originally published by Lewis kori at lewiskori.com
===================================================================
Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter
☞ Complete Python Bootcamp: Go from zero to hero in Python 3
☞ Python and Django Full Stack Web Developer Bootcamp
☞ Python for Time Series Data Analysis
☞ Python Programming For Beginners From Scratch
☞ Beginner’s guide on Python: Learn python from scratch! (New)
☞ Python for Beginners: Complete Python Programming
☞ Django 2.1 & Python | The Ultimate Web Development Bootcamp
☞ Python eCommerce | Build a Django eCommerce Web Application
☞ Python Django Dev To Deployment
#python #django #docker
1619518440
Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.
…
#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners
1619510796
Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.
Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is
Syntax: x = lambda arguments : expression
Now i will show you some python lambda function examples:
#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map
1624418760
See how to create a weather app in Django that gets the current weathers for multiple cities. This tutorial uses Python Requests to call the Open Weather Map API.
📺 The video in this post was made by freeCodeCamp.org
The origin of the article: https://www.youtube.com/watch?v=qCQGV7F7CUc&list=PLWKjhJtqVAbnqBxcdjVGgT3uVR10bzTEB&index=15
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!
#weather app #django #python #weather app - django tutorial (using python requests) #using python requests #django tutorial
1620177818
Welcome to my blog , hey everyone in this article you learn how to customize the Django app and view in the article you will know how to register and unregister models from the admin view how to add filtering how to add a custom input field, and a button that triggers an action on all objects and even how to change the look of your app and page using the Django suit package let’s get started.
#django #create super user django #customize django admin dashboard #django admin #django admin custom field display #django admin customization #django admin full customization #django admin interface #django admin register all models #django customization
1623057780
Django…We all know the popularity of this Python framework. Django has become the first choice of developers to build their web applications. It is a free and open-source Python framework. Django can easily solve a lot of common development challenges. It allows you to build flexible and well-structured web applications.
A lot of common features of Django such as a built-in admin panel, ORM (object-relational mapping tool), Routing, templating have made the task easier for developers. They do not require spending so much time on implementing these things from scratch.
One of the most killer features of Django is the built-in Admin panel. With this feature, you can configure a lot of things such as an access control list, row-level permissions, and actions, filters, orders, widgets, forms, extra URL helpers, etc.
Django ORM works with all major databases out of the box. It supports all the major SQL queries which you can use in your application. Templating engine of Django is also very, very flexible and powerful at the same time. Even a lot of features are available in Django, developers still make a lot of mistakes while building an application. In this blog, we will discuss some common mistakes which you should avoid while building a Django application.
#gblog #python #python django #building a django application #django #applications