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

feat(docker): Create docker-compose version, add one-click installation #13

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv/
.DS_Store
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Use an official Python runtime as the base image
FROM python:3.10-alpine

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set the working directory in the container
WORKDIR /women_blog_site

# Copy the requirements file and install dependencies
COPY requirements.txt /women_blog_site/
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

# Copy the project code into the container
COPY . /women_blog_site/

# Expose the port that the Django development server will be listening on
EXPOSE 8000

# Run the Django development server
CMD ["python", "coolsite/manage.py", "runserver", "0.0.0.0:8000"]
90 changes: 90 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
.PHONY: help\
run \
shell \
mdb \
db \
web.up\
web.stop\
web.down\
web.shell\
web.db.shell\
web.logs\
web.db\
web.fill_db\
web.flush\
web.user\
web.def_user\
web.install\


PIP_VERSION = 22.0.4

help: ## Show available make targets and their descriptions
@echo "Available make targets:"
@echo ""
@awk 'BEGIN {FS = ":.*?## "}; /^[\$$\(\)\/\.0-9A-Za-z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

requirements: venv/bin/activate ## install requirements
/venv/bin/activate; pip install -r requirements.txt

run: venv/bin/activate ## Local Run
. venv/bin/activate; python coolsite/manage.py runserver

shell: venv/bin/activate ## Run django shell
. venv/bin/activate; python coolsite/manage.py shell

mdb: venv/bin/activate ## Make migrations
. venv/bin/activate; python coolsite/manage.py makemigrations

db: venv/bin/activate ## Migrate to database
. venv/bin/activate; python coolsite/manage.py migrate

web.up: venv/bin/activate ## create and run docker-compose container
. venv/bin/activate; docker-compose up

web.stop: venv/bin/activate ## stop docker-compose services
. venv/bin/activate; docker-compose stop web && \
. venv/bin/activate; docker-compose stop db

web.down: venv/bin/activate ## down docker-compose container
. venv/bin/activate; docker-compose down

web.shell: venv/bin/activate ## enter docker-compose Django app service shell
. venv/bin/activate && docker-compose exec web /bin/sh

web.db.shell: venv/bin/activate ## enter docker-compose psql database shell shell
. venv/bin/activate; docker-compose exec db psql -U my_user -d my_database
# Get all tables names:
# SELECT table_name FROM information_schema.tables WHERE table_schema='public';

web.logs: venv/bin/activate ## enter docker-compose web service logs
. venv/bin/activate && docker-compose logs -f web

web.db: venv/bin/activate ## migrate changes to db container
. venv/bin/activate; docker-compose exec web python coolsite/manage.py migrate

web.fill_db: venv/bin/activate ## add test data to database in docker-compose service "psql"
. venv/bin/activate; docker-compose exec web python coolsite/manage.py fill_db

web.flush: venv/bin/activate ## clear database in docker-compose service "psql"
. venv/bin/activate; docker-compose exec web python coolsite/manage.py flush --noinput

web.user: venv/bin/activate ## create custom superuser in django app docker-compose service
. venv/bin/activate; docker-compose exec web python coolsite/manage.py createsuperuser

web.def_user: venv/bin/activate ## create default superuser in django app docker-compose service
@echo "Now you can create a superuser and access the admin panel"
@read -p "Username: " username && \
read -p "Email: " email && \
read -s -p "Password: " password && \
docker-compose exec web python coolsite/manage.py createsuperuser --noinput --username $$username --email $$email && \
docker-compose exec web python coolsite/manage.py shell -c "from django.contrib.auth import get_user_model;\
User = get_user_model(); user = User.objects.get(username='$$username'); \
user.set_password('$$password'); user.save()"

web.install: venv/bin/activate ## one-click installation (create and run container, add test data to db)
. venv/bin/activate; docker-compose up -d && \
make web.db && \
make web.fill_db && \
make web.def_user && \
make web.logs
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Tools used during development: black, flake8
+ git@github.com:KyryloKireiev/women_blog_site.git
+ All commands are in the Makefile
+ Django app in master branch using sqlite3 database
+ Detail installation instruction you can found in ```SETUP.md``` file

## One-clik installation

Expand Down
Binary file modified coolsite/.DS_Store
Binary file not shown.
23 changes: 0 additions & 23 deletions coolsite/Makefile

This file was deleted.

Empty file added coolsite/__init__.py
Empty file.
12 changes: 9 additions & 3 deletions coolsite/coolsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '0.0.0.0']


# Application definition
Expand Down Expand Up @@ -82,8 +82,12 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'my_database',
'USER': 'my_user',
'PASSWORD': 'my_password',
'HOST': "psql",
'PORT': '5432',
}
}

Expand Down Expand Up @@ -136,4 +140,6 @@

INTERNAL_IPS = [
"127.0.0.1",
"localhost",
"0.0.0.0",
]
Binary file not shown.
Binary file not shown.
Binary file removed coolsite/media/photos/2022/04/$d/andjelina-joli.jpg
Binary file not shown.
Binary file not shown.
46 changes: 46 additions & 0 deletions coolsite/women/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
WOMEN_CONTENT = {
"Trinity": """Trinity is a fictional character in the Matrix franchise.[1] She is portrayed by
Carrie-Anne Moss in the films. In the gameplay segments of Path of Neo, she is voiced
by Jennifer Hale. Trinity first appears in the 1999 film The Matrix.""",
"Sarah Conor": """Sarah Connor (born Sarah Marianne Corina Lewe; 13 June 1980) is a German pop singer.
She rose to prominence after she signed with X-Cell Records in 2000 and released her
debut album Green Eyed Soul (2001) the following year. She followed it with a
series of successful albums, including Unbelievable (2002), Key to My Soul (2003),
Naughty but Nice (2005), Christmas in My Heart (2005), Soulicious (2007), Sexy as Hell
(2008) and Real Love (2010). Several songs from these albums became hit singles on the
pop record charts, including the number-one hits "From Sarah with Love", "Music is the Key",
"Just One Last Dance", "Living to Love You" and "From Zero to Hero". In 2015, Connor's first
German language project Muttersprache became her second chart-topper after a decade.""",
"Angelina Jolie": """Angelina Jolie[3] DCMG (/dʒoʊˈliː/; born Angelina Jolie Voight;[4] June 4, 1975)
is an American actress, filmmaker, and humanitarian. The recipient of numerous accolades,
including an Academy Award and three Golden Globe Awards, she has been named Hollywood's
highest-paid actress multiple times.

Jolie made her screen debut as a child alongside her father, Jon Voight,
in Lookin' to Get Out (1982), and her film career began in earnest a decade
later with the low-budget production Cyborg 2 (1993), followed by her first
leading role in a major film in Hackers (1995). She starred in the biographical
television films George Wallace (1997) and Gia (1998) and won the Academy Award
for Best Supporting Actress for her performance in the 1999 drama Girl, Interrupted.
Her starring role as the titular video game heroine in Lara Croft: Tomb Raider (2001)
established her as a leading Hollywood actress. She continued her action-star career
with Mr. & Mrs. Smith (2005), Wanted (2008), Salt (2010), and The Tourist (2010),
and received critical acclaim for her performances in the dramas A Mighty Heart
(2007) and Changeling (2008); the latter earned her a nomination for the Academy
Award for Best Actress. Her biggest commercial successes include the fantasy picture
Maleficent (2014), its 2019 sequel, and the superhero film Eternals (2021).
She has performed a voice role in the animation film series Kung Fu Panda since 2008.
Jolie has also directed and written the war dramas In the Land of Blood and Honey (2011),
Unbroken (2014), and First They Killed My Father (2017).""",
"Lesya Ukrainka": """Lesya Ukrainka[1] (Ukrainian: Леся Українка Ukrainian
pronunciation: [ˈlɛsʲɐ ʊkrɐˈjinkɐ]; born Larysa Petrivna Kosach,
Ukrainian: Лариса Петрівна Косач; 25 February [O.S. 13 February]
1871 – 1 August [O.S. 19 July] 1913) was one of Ukrainian
literature's foremost writers, best known for her poems and plays.
She was also an active political, civil, and feminist activist.[2]

Among her best-known works are the collections of poems On the
Wings of Songs (1893), Thoughts and Dreams (1899), Echos (1902),
the epic poem Ancient Fairy Tale (1893), One Word (1903), plays Princess (1913),
Cassandra (1903—1907), In the Catacombs (1905), and Forest Song (1911)."""
}
Empty file.
Empty file.
44 changes: 44 additions & 0 deletions coolsite/women/management/commands/fill_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os

from django.core.management.base import BaseCommand
from women.models import Category, Women
from women.constants import WOMEN_CONTENT

CATEGORIES = ("Characters", "Actresses", "Models", "Writers")
WOMEN = ("Trinity", "Sarah Conor", "Angelina Jolie", "Lesya Ukrainka")
PHOTO_PATH = "coolsite/media/photos/2022/04/$d/"
PHOTO_TITLES = ("trinity.jpeg", "connor.jpeg", "angelina.jpg", "lesya.jpeg")


class Command(BaseCommand):
help = 'Populates the database with test data'

def handle(self, *args, **options):
if self.validate_fill_bd():
return self.stdout.write(self.style.SUCCESS('Test data has already been written to the database'))

self.create_categories()
self.create_women()

self.stdout.write(self.style.SUCCESS('Test data added successfully'))

@staticmethod
def create_categories():
for cat in CATEGORIES:
Category.objects.create(name=cat)

@staticmethod
def create_women():
for index, woman in enumerate(WOMEN):
photo_filename = os.path.basename(f"{PHOTO_PATH}{PHOTO_TITLES[index]}")
obj = Women.objects.create(
title=WOMEN[index],
content=WOMEN_CONTENT[woman],
cat=Category.objects.get(pk=index + 1)
)
obj.photo.save(photo_filename, open(f"{PHOTO_PATH}{PHOTO_TITLES[index]}", "rb"), save=True)

@staticmethod
def validate_fill_bd():
return Category.objects.filter(name__in=CATEGORIES).exists()

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.0.4 on 2023-06-21 09:42

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('women', '0002_category_alter_women_photo_women_cat'),
]

operations = [
migrations.AlterModelOptions(
name='category',
options={'verbose_name': 'Category', 'verbose_name_plural': 'Categories'},
),
migrations.AlterModelOptions(
name='women',
options={'ordering': ['-time_create', 'title'], 'verbose_name': 'Famous woman', 'verbose_name_plural': 'Famous women'},
),
]
Binary file modified coolsite/women/static/women/.DS_Store
Binary file not shown.
24 changes: 20 additions & 4 deletions coolsite/women/static/women/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ ul.list-articles li {

.header {
background: #3F4137;
height: 60px;
height: 100px;
}

.logo {
background: url('../images/logo.png') no-repeat 10px 5px;
background: url('../images/logo.png') no-repeat 10px 10px;
width: 70px;
height: 60px;
height: 100px;
}

ul.mainmenu {
list-style: none;
margin: 0;
padding: 0;
height: 60px;
height: 100px;
color: #fdc073;
font-size: 20px;
overflow: hidden;
Expand Down Expand Up @@ -428,3 +428,19 @@ ul.lang-list li a:hover {color: #fdc073;}
.list-pages .page-num-selected:hover {
box-shadow: none;
}

.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 50px;
margin-bottom: 20px;
}
.cell {
display: flex;
align-items: center;
justify-content: center;
width: 50px;
height: 50px;
border: 3px solid black;
font-size: 48px;
}
Binary file modified coolsite/women/static/women/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified coolsite/women/static/women/images/main.ico
Binary file not shown.
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- '8000:8000'
volumes:
- .:/women_blog_site
restart: always
depends_on:
- db
environment:
- DB_HOST=psql
- DB_NAME=my_database
- DB_USER=my_user
- DB_PASS=my_password


db:
image: postgres:latest
restart: always
container_name: "psql"
environment:
- POSTGRES_USER=my_user
- POSTGRES_PASSWORD=my_password
- POSTGRES_DB=my_database
ports:
- "5432:5432"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ django-ranged-response==0.2.0
django-simple-captcha==0.5.17
Pillow==9.1.0
sqlparse==0.4.2
psycopg2-binary==2.9.6