From adb6e3ba2d457005e0edb03a139641d0e02add14 Mon Sep 17 00:00:00 2001 From: palash Date: Wed, 14 Jul 2021 00:04:28 +0530 Subject: [PATCH 1/3] added docker files --- .dockerignore | 1 + Dockerfile | 8 ++++++++ entrypoint.sh | 3 +++ 3 files changed, 12 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..6b8710a7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.git diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..f1b03816 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.8-buster +WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt +COPY . . +RUN cp .env.dev.sample .env +EXPOSE 8000 +CMD ["sh","entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..59b6bdf9 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +python manage.py migrate +python manage.py loaddata fixtures/app_name_initial_data.json --app app.model_name +python manage.py runserver From 7b0380183060aba8caeeb17a133b05c6e1f1f4ba Mon Sep 17 00:00:00 2001 From: palash Date: Wed, 14 Jul 2021 09:31:01 +0530 Subject: [PATCH 2/3] made changes to use mysql and docker-compose files --- docker-compose.yml | 24 ++++++++++++++++++++++++ docker/init.sql | 1 + jobs/settings.py | 13 ++++++++++--- jobsapp/__init__.py | 2 ++ requirements.txt | 1 + 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 docker-compose.yml create mode 100644 docker/init.sql diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..7c083935 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +version: "2.0" +services: + mysql_database: + image: mysql:5.7 + ports: + - "33061:3306" + command: --init-file /data/application/init.sql + volumes: + - ./docker/init.sql:/data/application/init.sql + environment: + MYSQL_ROOT_USER: root + MYSQL_ROOT_PASSWORD: toor + MYSQL_DATABASE: jobs_db + MYSQL_USER: jobs_user + MYSQL_PASSWORD: jobs@123 + portal: + build: . + ports: + - 8000:8000 + depends_on: + - mysql_database + links: + - mysql_database + diff --git a/docker/init.sql b/docker/init.sql new file mode 100644 index 00000000..211d4e2f --- /dev/null +++ b/docker/init.sql @@ -0,0 +1 @@ +create database if not exists jobsdb; diff --git a/jobs/settings.py b/jobs/settings.py index 2d246635..cac53c7c 100644 --- a/jobs/settings.py +++ b/jobs/settings.py @@ -80,9 +80,16 @@ # https://docs.djangoproject.com/en/2.1/ref/settings/#databases DATABASES = { - "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": os.path.join(BASE_DIR, "db.sqlite3"), + # "default": { + # "ENGINE": "django.db.backends.sqlite3", + # "NAME": os.path.join(BASE_DIR, "db.sqlite3"), + # }, + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'jobdb', + 'HOST': 'mysql_database', #docker db service container + 'USER': 'jobs_user', + 'PASSWORD': 'jobs@123' }, # 'default': { # 'ENGINE': 'django.db.backends.postgresql_psycopg2', diff --git a/jobsapp/__init__.py b/jobsapp/__init__.py index e69de29b..063cd2cc 100644 --- a/jobsapp/__init__.py +++ b/jobsapp/__init__.py @@ -0,0 +1,2 @@ +import pymysql +pymysql.install_as_MySQLdb() diff --git a/requirements.txt b/requirements.txt index e1ce0d23..a6a0bcfd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -80,3 +80,4 @@ urllib3==1.25.8 virtualenv==20.4.7 webencodings==0.5.1 whitenoise==5.0.1 +PyMySQL==1.0.2 From cbdb9e92e2370e5275771144dcb49c5e07fc6834 Mon Sep 17 00:00:00 2001 From: palash Date: Wed, 14 Jul 2021 18:04:33 +0530 Subject: [PATCH 3/3] dockerize --- docker-compose.yml | 21 +++++++++++++-------- docker/init.sql | 4 +++- entrypoint.sh | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 7c083935..a7487bb7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,24 +1,29 @@ -version: "2.0" +version: "2.1" services: mysql_database: image: mysql:5.7 ports: - - "33061:3306" + - "33061:3306" command: --init-file /data/application/init.sql volumes: - ./docker/init.sql:/data/application/init.sql environment: - MYSQL_ROOT_USER: root - MYSQL_ROOT_PASSWORD: toor - MYSQL_DATABASE: jobs_db - MYSQL_USER: jobs_user - MYSQL_PASSWORD: jobs@123 + MYSQL_ROOT_USER: root + MYSQL_ROOT_PASSWORD: toor + MYSQL_DATABASE: jobdb + MYSQL_USER: jobs_user + MYSQL_PASSWORD: jobs@123 + healthcheck: + test: ["CMD","mysqladmin","ping","-h","localhost"] + timeout: 20s + retries: 10 portal: build: . ports: - 8000:8000 depends_on: - - mysql_database + mysql_database: + condition: service_healthy links: - mysql_database diff --git a/docker/init.sql b/docker/init.sql index 211d4e2f..b61b243e 100644 --- a/docker/init.sql +++ b/docker/init.sql @@ -1 +1,3 @@ -create database if not exists jobsdb; +create database if not exists jobdb; +GRANT ALL PRIVILEGES ON `jobdb`.* To 'jobs_user'@'%'; +FLUSH PRIVILEGES; diff --git a/entrypoint.sh b/entrypoint.sh index 59b6bdf9..1e040a5b 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,3 +1,3 @@ python manage.py migrate python manage.py loaddata fixtures/app_name_initial_data.json --app app.model_name -python manage.py runserver +python manage.py runserver 0.0.0.0:8000