Skip to content

Instantly share code, notes, and snippets.

@averyfreeman
Created May 8, 2024 03:02
Show Gist options
  • Save averyfreeman/86ac6ab13bf90c77f5e4090f8f664d45 to your computer and use it in GitHub Desktop.
Save averyfreeman/86ac6ab13bf90c77f5e4090f8f664d45 to your computer and use it in GitHub Desktop.
# did docker compose down --rmi all beforehand
➜ avery workshop vitualenv:(.venv) docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
➜ avery workshop vitualenv:(.venv) docker compose up --build
[+] Running 21/21
✔ pgadmin Pulled 13.2s
✔ 540db60ca938 Already exists 0.9s
✔ 703171bdeac3 Pull complete 3.9s
✔ ec12905299b3 Pull complete 4.0s
✔ 083fe9d056f7 Pull complete 4.0s
✔ dbc5b87a0140 Pull complete 4.8s
✔ 2a4ca048ef43 Pull complete 5.2s
✔ c09872953271 Pull complete 5.3s
✔ d51549556e9d Pull complete 5.4s
✔ 87553ab490b8 Pull complete 5.4s
✔ ed9ea3fe40d4 Pull complete 7.8s
✔ 2333a4165d7a Pull complete 7.9s
✔ fa43f71dd147 Pull complete 11.3s
✔ pg Pulled 14.6s
✔ a3cb73039552 Pull complete 6.6s
✔ 39855706e49a Pull complete 6.7s
✔ 74e3b91ca207 Pull complete 10.9s
✔ 61c8cceb594b Pull complete 11.0s
✔ a63418dfbd97 Pull complete 11.0s
✔ cb8adc73d3af Pull complete 11.1s
✔ d6a2a57ca5f8 Pull complete 11.2s
[+] Building 2.3s (11/11) FINISHED docker:default
=> [web internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 228B 0.0s
=> [web internal] load metadata for docker.io/library/python:3.9.4-slim 0.9s
=> [web auth] library/python:pull token for registry-1.docker.io 0.0s
=> [web internal] load .dockerignore 0.0s
=> => transferring context: 409B 0.0s
=> [web 1/5] FROM docker.io/library/python:3.9.4-slim@sha256:d35ab472c4e3c711ce00bbc682ba243dcb 0.0s
=> [web internal] load build context 0.0s
=> => transferring context: 4.08kB 0.0s
=> CACHED [web 2/5] COPY requirements.txt . 0.0s
=> CACHED [web 3/5] RUN python -m pip install -r requirements.txt 0.0s
=> CACHED [web 4/5] WORKDIR /app 0.0s
=> [web 5/5] COPY . /app 0.1s
=> [web] exporting to image 1.1s
=> => exporting layers 1.1s
=> => writing image sha256:ed0bdee10d2d726b95d030e74c6327cf810f194fd70b6fbc0e4422b4fe10bc85 0.0s
=> => naming to docker.io/library/workshop-web 0.0s
[+] Running 4/4
✔ Network django_net Created 0.1s
✔ Container pg_container Created 0.1s
✔ Container workshop-web-1 Created 0.1s
✔ Container pgadmin_container Created 0.1s
Attaching to pg_container, pgadmin_container, web-1
pg_container | ********************************************************************************
pg_container | WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow
pg_container | anyone with access to the Postgres port to access your database without
pg_container | a password, even if POSTGRES_PASSWORD is set. See PostgreSQL
pg_container | documentation about "trust":
pg_container | https://www.postgresql.org/docs/current/auth-trust.html
pg_container | In Docker's default configuration, this is effectively any other
pg_container | container on the same system.
pg_container |
pg_container | It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace
pg_container | it with "-e POSTGRES_PASSWORD=password" instead to set a password in
pg_container | "docker run".
pg_container | ********************************************************************************
pg_container | The files belonging to this database system will be owned by user "postgres".
pg_container | This user must also own the server process.
pg_container |
pg_container | The database cluster will be initialized with locale "en_US.utf8".
pg_container | The default database encoding has accordingly been set to "UTF8".
pg_container | The default text search configuration will be set to "english".
pg_container |
pg_container | Data page checksums are disabled.
pg_container |
pg_container | fixing permissions on existing directory /var/lib/postgresql/data ... ok
pg_container | creating subdirectories ... ok
pg_container | selecting dynamic shared memory implementation ... posix
pg_container | selecting default max_connections ... 100
pg_container | selecting default shared_buffers ... 128MB
pg_container | selecting default time zone ... UTC
pg_container | creating configuration files ... ok
pg_container | running bootstrap script ... ok
pg_container | sh: locale: not found
pg_container | 2024-05-08 02:37:02.132 UTC [31] WARNING: no usable system locales were found
pg_container | performing post-bootstrap initialization ... ok
pg_container | syncing data to disk ... ok
pg_container |
pg_container |
pg_container | Success. You can now start the database server using:
pg_container |
pg_container | pg_ctl -D /var/lib/postgresql/data -l logfile start
pg_container |
pg_container | initdb: warning: enabling "trust" authentication for local connections
pg_container | You can change this by editing pg_hba.conf or using the option -A, or
pg_container | --auth-local and --auth-host, the next time you run initdb.
pg_container | waiting for server to start....2024-05-08 02:37:03.073 UTC [36] LOG: starting PostgreSQL 13.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.2.1_pre1) 10.2.1 20201203, 64-bit
pg_container | 2024-05-08 02:37:03.078 UTC [36] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
pg_container | 2024-05-08 02:37:03.094 UTC [37] LOG: database system was shut down at 2024-05-08 02:37:02 UTC
pg_container | 2024-05-08 02:37:03.100 UTC [36] LOG: database system is ready to accept connections
pg_container | done
pg_container | server started
web-1 | Performing system checks...
web-1 |
web-1 | System check identified no issues (0 silenced).
web-1 | Exception in thread django-main-thread:
web-1 | Traceback (most recent call last):
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web-1 | self.connect()
web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web-1 | return func(*args, **kwargs)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
web-1 | self.connection = self.get_new_connection(conn_params)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web-1 | return func(*args, **kwargs)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
web-1 | connection = Database.connect(**conn_params)
web-1 | File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web-1 | psycopg2.OperationalError: connection to server at "pg" (172.27.0.2), port 5432 failed: Connection refused
web-1 | Is the server running on that host and accepting TCP/IP connections?
web-1 |
web-1 |
web-1 | The above exception was the direct cause of the following exception:
web-1 |
web-1 | Traceback (most recent call last):
web-1 | File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
web-1 | self.run()
web-1 | File "/usr/local/lib/python3.9/threading.py", line 892, in run
web-1 | self._target(*self._args, **self._kwargs)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
web-1 | fn(*args, **kwargs)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
web-1 | self.check_migrations()
web-1 | File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 486, in check_migrations
web-1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
web-1 | self.loader = MigrationLoader(self.connection)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
web-1 | self.build_graph()
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 220, in build_graph
web-1 | self.applied_migrations = recorder.applied_migrations()
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
web-1 | if self.has_table():
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
web-1 | with self.connection.cursor() as cursor:
web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web-1 | return func(*args, **kwargs)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
web-1 | return self._cursor()
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 235, in _cursor
web-1 | self.ensure_connection()
web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web-1 | return func(*args, **kwargs)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web-1 | self.connect()
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
web-1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web-1 | self.connect()
web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web-1 | return func(*args, **kwargs)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
web-1 | self.connection = self.get_new_connection(conn_params)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
web-1 | return func(*args, **kwargs)
web-1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
web-1 | connection = Database.connect(**conn_params)
web-1 | File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web-1 | django.db.utils.OperationalError: connection to server at "pg" (172.27.0.2), port 5432 failed: Connection refused
web-1 | Is the server running on that host and accepting TCP/IP connections?
web-1 |
pg_container | CREATE DATABASE
pg_container |
pg_container |
pg_container | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
pg_container |
pg_container | waiting for server to shut down....2024-05-08 02:37:03.391 UTC [36] LOG: received fast shutdown request
pg_container | 2024-05-08 02:37:03.395 UTC [36] LOG: aborting any active transactions
pg_container | 2024-05-08 02:37:03.396 UTC [36] LOG: background worker "logical replication launcher" (PID 43) exited with exit code 1
pg_container | 2024-05-08 02:37:03.396 UTC [38] LOG: shutting down
pg_container | 2024-05-08 02:37:03.422 UTC [36] LOG: database system is shut down
pg_container | done
pg_container | server stopped
pg_container |
pg_container | PostgreSQL init process complete; ready for start up.
pg_container |
pg_container | 2024-05-08 02:37:03.512 UTC [1] LOG: starting PostgreSQL 13.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.2.1_pre1) 10.2.1 20201203, 64-bit
pg_container | 2024-05-08 02:37:03.512 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
pg_container | 2024-05-08 02:37:03.512 UTC [1] LOG: listening on IPv6 address "::", port 5432
pg_container | 2024-05-08 02:37:03.521 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
pg_container | 2024-05-08 02:37:03.535 UTC [50] LOG: database system was shut down at 2024-05-08 02:37:03 UTC
pg_container | 2024-05-08 02:37:03.541 UTC [1] LOG: database system is ready to accept connections
pgadmin_container | NOTE: Configuring authentication for DESKTOP mode.
pgadmin_container | [2024-05-08 02:37:14 +0000] [1] [INFO] Starting gunicorn 20.1.0
pgadmin_container | [2024-05-08 02:37:14 +0000] [1] [INFO] Listening at: http://[::]:5433 (1)
pgadmin_container | [2024-05-08 02:37:14 +0000] [1] [INFO] Using worker: threads
pgadmin_container | [2024-05-08 02:37:14 +0000] [94] [INFO] Booting worker with pid: 94
^X^CGracefully stopping... (press Ctrl+C again to force)
[+] Stopping 3/3
✔ Container pgadmin_container Stopped 0.8s
✔ Container workshop-web-1 Stopped 0.2s
✔ Container pg_container Stopped 0.2s
canceled
➜ avery workshop vitualenv:(.venv) docker compose down --rmi all
[+] Running 7/7
✔ Container workshop-web-1 Removed 0.0s
✔ Container pgadmin_container Removed 0.0s
✔ Container pg_container Removed 0.0s
✔ Image workshop-web:latest Removed 0.0s
✔ Image dpage/pgadmin4:5.2 Removed 0.4s
✔ Image postgres:13.2-alpine Removed 0.1s
✔ Network django_net Removed
❯ docker network inspect django_net | more
[
{
"Name": "django_net",
"Id": "821e14a5cf63a8344ff2f6b5026ea7822e749b3c822eb627331303a05f30e42c",
"Created": "2024-05-07T19:37:01.212143273-07:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"1a57c55bde0233c5d355286dde3dda62ea108c0f398ae8643a0034716fbf0dec": {
"Name": "pg_container",
"EndpointID": "eec1c5f9daebfd6d49b87dd62348d199aca987c42c3479782255a5cbf2d4683e",
"MacAddress": "02:42:ac:1b:00:02",
"IPv4Address": "172.27.0.2/16",
"IPv6Address": ""
},
"1f07bc19bb20b29777cdd4c931a2345f330a515dc39dafb015cd984c3906189f": {
"Name": "workshop-web-1",
"EndpointID": "329b15a15c5238677310c51f6baea3938ab94d4b7ad0a013a32e820f8c373d92",
"MacAddress": "02:42:ac:1b:00:03",
"IPv4Address": "172.27.0.3/16",
"IPv6Address": ""
},
"7e2737fea3d4bf6f3bc9c304d685dabbdc1a315f5bb035612a241ef004c37b22": {
"Name": "pgadmin_container",
"EndpointID": "99587ded4ea80f43d915398d61c737c9633356e5942ef563fdcd20fc61201890",
"MacAddress": "02:42:ac:1b:00:04",
"IPv4Address": "172.27.0.4/16",
"IPv6Address": ""
❯ docker network inspect django_net | jq
services:
pg:
container_name: pg_container
image: postgres:13.2-alpine
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: admin123
POSTGRES_DB: nc_tutorials_db
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
volumes:
- ./data:/data
- ./data/misc/django_init.sql:/docker-entrypoint.initdb.d/1-django-init.sql
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4:5.2
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin@example.com
PGADMIN_DEFAULT_PASSWORD: admin123
PGADMIN_LISTEN_PORT: 5433
PGADMIN_CONFIG_SERVER_MODE: "False"
PGADMIN_CONFIG_MASTER_PASSWORD: "False"
PGADMIN_CONFIG_UPGRADE_CHECK_ENABLED: "False"
ports:
- 5433:5433
depends_on:
- pg
web:
build: ./app
command: python manage.py runserver 0.0.0.0:8000
environment:
DB_NAME: nc_tutorials_db
DB_USER: postgres
DB_PASSWORD: admin123
DB_HOST: pg
DB_PORT: "5432"
DATABASE: postgres
ports:
- 8000:8000
volumes:
- ./app/:/usr/src/app/
depends_on:
- pg
networks:
default:
name: django_net
"""
Django settings for nc_tutorials project.
Generated by 'django-admin startproject' using Django 3.2.2.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
# from decouple import config
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-uuzbofr10*@vee_46vjv=frj($)yl%8m*agjv(tr3)l@cvfh8t'
# SECRET_KEY = config('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = config('DEBUG', default=False)
DEBUG = False
ALLOWED_HOSTS = ["*", "0.0.0.0", "::0/0"]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'nc_tutorials.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'nc_tutorials.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME" : "nc_tutorials_db",
"USER" : "postgres",
"PASSWORD" : "admin123",
"HOST" : "pg",
"PORT" : "5432"
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment