Django for beginners par William Vincent

Thanks to William Vincent

total 52
drwxrwxr-x. 6 pvergain pvergain 4096 28 mai   16:10 ch10-bootstrap
drwxrwxr-x. 6 pvergain pvergain 4096 28 mai   16:10 ch11-password-change-reset
drwxrwxr-x. 6 pvergain pvergain 4096 28 mai   16:10 ch12-email
drwxrwxr-x. 7 pvergain pvergain 4096 28 mai   16:10 ch13-newspaper-app
drwxrwxr-x. 7 pvergain pvergain 4096 28 mai   16:10 ch14-permissions-and-authorizations
drwxrwxr-x. 7 pvergain pvergain 4096 28 mai   16:10 ch15-comments
drwxrwxr-x. 4 pvergain pvergain   92 28 mai   16:10 ch2-hello-world-app
drwxrwxr-x. 5 pvergain pvergain  103 28 mai   16:10 ch3-pages-app
drwxrwxr-x. 5 pvergain pvergain 4096 28 mai   16:15 ch4-message-board-app
drwxrwxr-x. 7 pvergain pvergain 4096 28 mai   16:10 ch5-blog-app
drwxrwxr-x. 7 pvergain pvergain 4096 28 mai   16:10 ch6-blog-app-with-forms
drwxrwxr-x. 7 pvergain pvergain 4096 28 mai   16:10 ch7-blog-app-with-users
drwxrwxr-x. 4 pvergain pvergain 4096 28 mai   16:10 ch8-custom-user-model
drwxrwxr-x. 5 pvergain pvergain 4096 28 mai   16:10 ch9-user-authentication
-rw-rw-r--. 1 pvergain pvergain  689 28 mai   16:15 Readme.md

tree ch4-message-board-app

tree ch4-message-board-app
ch4-message-board-app/
├── Dockerfile
├── manage.py
├── mb_project
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── Pipfile
├── Pipfile.lock
├── posts
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── Procfile
└── templates
        └── home.html

Dockerfile from Will Vincent

FROM python:3.6

ENV PYTHONUNBUFFERED 1

COPY . /code/
WORKDIR /code/

RUN pip install pipenv
RUN pipenv install --system

EXPOSE 8000

docker build .

We can’t run a Docker container until it has an image so let’s do that by building it.

Y:\projects_id3\P5N001\XLOGCA135_tutorial_docker\tutorial_docker\tutoriels\djangoforbeginners\ch4-message-board-app>docker build .
Sending build context to Docker daemon   47.1kB
Step 1/7 : FROM python:3.6
 ---> c1e459c00dc3
Step 2/7 : ENV PYTHONUNBUFFERED 1
 ---> Using cache
 ---> 221d2e9ab9e4
Step 3/7 : COPY . /code/
 ---> 874258521e07
Step 4/7 : WORKDIR /code/
Removing intermediate container 19c5a97f7968
 ---> b1a4a747aea7
Step 5/7 : RUN pip install pipenv
 ---> Running in 42f7073e751d
Collecting pipenv
  Downloading pipenv-9.0.3.tar.gz (3.9MB)
Collecting virtualenv (from pipenv)
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
Collecting pew>=0.1.26 (from pipenv)
  Downloading pew-1.1.2-py2.py3-none-any.whl
Requirement already satisfied: pip>=9.0.1 in /usr/local/lib/python3.6/site-packages (from pipenv)
Collecting requests>2.18.0 (from pipenv)
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting flake8>=3.0.0 (from pipenv)
  Downloading flake8-3.5.0-py2.py3-none-any.whl (69kB)
Collecting urllib3>=1.21.1 (from pipenv)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
Requirement already satisfied: setuptools>=17.1 in /usr/local/lib/python3.6/site-packages (from pew>=0.1.26->pipenv)
Collecting virtualenv-clone>=0.2.5 (from pew>=0.1.26->pipenv)
  Downloading virtualenv-clone-0.2.6.tar.gz
Collecting certifi>=2017.4.17 (from requests>2.18.0->pipenv)
  Downloading certifi-2018.1.18-py2.py3-none-any.whl (151kB)
Collecting idna<2.7,>=2.5 (from requests>2.18.0->pipenv)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests>2.18.0->pipenv)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting pycodestyle<2.4.0,>=2.0.0 (from flake8>=3.0.0->pipenv)
  Downloading pycodestyle-2.3.1-py2.py3-none-any.whl (45kB)
Collecting mccabe<0.7.0,>=0.6.0 (from flake8>=3.0.0->pipenv)
  Downloading mccabe-0.6.1-py2.py3-none-any.whl
Collecting pyflakes<1.7.0,>=1.5.0 (from flake8>=3.0.0->pipenv)
  Downloading pyflakes-1.6.0-py2.py3-none-any.whl (227kB)
Building wheels for collected packages: pipenv, virtualenv-clone
  Running setup.py bdist_wheel for pipenv: started
  Running setup.py bdist_wheel for pipenv: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/78/cf/b7/549d89ddbafb1cf3da825b97b730a7e1ac75602de9865d036e
  Running setup.py bdist_wheel for virtualenv-clone: started
  Running setup.py bdist_wheel for virtualenv-clone: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/24/51/ef/93120d304d240b4b6c2066454250a1626e04f73d34417b956d
Successfully built pipenv virtualenv-clone
Installing collected packages: virtualenv, virtualenv-clone, pew, certifi, idna, chardet, urllib3, requests, pycodestyle, mccabe, pyflakes, flake8, pipenv
Successfully installed certifi-2018.1.18 chardet-3.0.4 flake8-3.5.0 idna-2.6 mccabe-0.6.1 pew-1.1.2 pipenv-9.0.3 pycodestyle-2.3.1 pyflakes-1.6.0 requests-2.18.4 urllib3-1.22 virtualenv-15.1.0 virtualenv-clone-0.2.6
Removing intermediate container 42f7073e751d
 ---> 89cfca6a042a
Step 6/7 : RUN pipenv install --system
 ---> Running in 95effdc52999
Installing dependencies from Pipfile.lock (48d763)…
Removing intermediate container 95effdc52999
 ---> 60e848b90903
Step 7/7 : EXPOSE 8000
 ---> Running in 325a08f841b9
Removing intermediate container 325a08f841b9
 ---> 7bd32294cda7
Successfully built 7bd32294cda7
SECURITY WARNING: You are building a Docker image from Windows
against a non-Windows Docker host. All files and directories added
to build context will have '-rwxr-xr-x' permissions.
It is recommended to double check and reset permissions for sensitive
files and directories.

mb_project/settings.py

"""
Django settings for mb_project project.

Generated by 'django-admin startproject' using Django 2.0.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '_%!$0#1!cxd(rrj%=5rmeu%qiccz)7vsorclhey9-w00xq7&t4'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'posts',
]

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 = 'mb_project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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 = 'mb_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

# https://djangoforbeginners.com/docker-postgresql/
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db', # set in docker-compose.yml
        'POST': 5432 # default postgres port
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.0/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/2.0/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/2.0/howto/static-files/

STATIC_URL = '/static/'

pipenv install psycopg2

Y:\projects_id3\P5N001\XLOGCA135_tutorial_docker\tutorial_docker\tutoriels\djangoforbeginners\ch4-message-board-app>pipenv install psycopg2
Installing psycopg2…
Collecting psycopg2
  Using cached psycopg2-2.7.3.2-cp36-cp36m-win_amd64.whl
Installing collected packages: psycopg2
Successfully installed psycopg2-2.7.3.2

Adding psycopg2 to Pipfile's [packages]…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (c2c6d4)!

docker-compose.yml William Vincent

version: '3'

services:
  db:
    image: postgres:10.1
    volumes:
      - postgres_data:/var/lib/postgresl/data/

  web:
    build: .
    command: python /code/manage.py migrate --noinput
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

volumes:
  postgres_data:

On the top line we’re using the most recent version of Compose which is 3.

db

Under db for the database we want the Docker image for Postgres 10.1 and use volumes to tell Compose where the container should be located in our Docker container.

web

For web we’re specifying how the web service will run. First Compose needs to build an image from the current directory, automatically run migrations and hide the output, then start up the server at 0.0.0.0:8000.

volumes

We use volumes to tell Compose to store the code in our Docker container at /code/.

Warning

Cela nous permet d’avoir accès à notre code sur le host.

ports

The ports config lets us map our own port 8000 to the port 8000 in the Docker container.

And finally depends_on says that we should start the db first before running our web services.

volumes

The last section volumes is because Compose has a rule that you must list named volumes in a top-level volumes key.

Docker is all set!

docker-compose run web python /code/manage.py migrate –noinput

Y:\projects_id3\P5N001\XLOGCA135_tutorial_docker\tutorial_docker\tutoriels\djangoforbeginners\ch4-message-board-app>docker-compose run web python /code/manage.py migrate --noinput
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Creating network "ch4messageboardapp_default" with the default driver
Creating volume "ch4messageboardapp_postgres_data" with default driver
Pulling db (postgres:10.1)...
10.1: Pulling from library/postgres
Digest: sha256:3f4441460029e12905a5d447a3549ae2ac13323d045391b0cb0cf8b48ea17463
Status: Downloaded newer image for postgres:10.1
Creating ch4messageboardapp_db_1 ... done
Building web
Step 1/7 : FROM python:3.6
 ---> c1e459c00dc3
Step 2/7 : ENV PYTHONUNBUFFERED 1
 ---> Using cache
 ---> 221d2e9ab9e4
Step 3/7 : COPY . /code/
 ---> e03ac813d986
Step 4/7 : WORKDIR /code/
Removing intermediate container 7d82a1620667
 ---> f810a068e5ab
Step 5/7 : RUN pip install pipenv
 ---> Running in 95827f363022
Collecting pipenv
  Downloading pipenv-9.0.3.tar.gz (3.9MB)
Collecting virtualenv (from pipenv)
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
Collecting pew>=0.1.26 (from pipenv)
  Downloading pew-1.1.2-py2.py3-none-any.whl
Requirement already satisfied: pip>=9.0.1 in /usr/local/lib/python3.6/site-packages (from pipenv)
Collecting requests>2.18.0 (from pipenv)
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting flake8>=3.0.0 (from pipenv)
  Downloading flake8-3.5.0-py2.py3-none-any.whl (69kB)
Collecting urllib3>=1.21.1 (from pipenv)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
Collecting virtualenv-clone>=0.2.5 (from pew>=0.1.26->pipenv)
  Downloading virtualenv-clone-0.2.6.tar.gz
Requirement already satisfied: setuptools>=17.1 in /usr/local/lib/python3.6/site-packages (from pew>=0.1.26->pipenv)
Collecting certifi>=2017.4.17 (from requests>2.18.0->pipenv)
  Downloading certifi-2018.1.18-py2.py3-none-any.whl (151kB)
Collecting idna<2.7,>=2.5 (from requests>2.18.0->pipenv)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests>2.18.0->pipenv)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting pycodestyle<2.4.0,>=2.0.0 (from flake8>=3.0.0->pipenv)
  Downloading pycodestyle-2.3.1-py2.py3-none-any.whl (45kB)
Collecting mccabe<0.7.0,>=0.6.0 (from flake8>=3.0.0->pipenv)
  Downloading mccabe-0.6.1-py2.py3-none-any.whl
Collecting pyflakes<1.7.0,>=1.5.0 (from flake8>=3.0.0->pipenv)
  Downloading pyflakes-1.6.0-py2.py3-none-any.whl (227kB)
Building wheels for collected packages: pipenv, virtualenv-clone
  Running setup.py bdist_wheel for pipenv: started
  Running setup.py bdist_wheel for pipenv: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/78/cf/b7/549d89ddbafb1cf3da825b97b730a7e1ac75602de9865d036e
  Running setup.py bdist_wheel for virtualenv-clone: started
  Running setup.py bdist_wheel for virtualenv-clone: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/24/51/ef/93120d304d240b4b6c2066454250a1626e04f73d34417b956d
Successfully built pipenv virtualenv-clone
Installing collected packages: virtualenv, virtualenv-clone, pew, certifi, idna, chardet, urllib3, requests, pycodestyle, mccabe, pyflakes, flake8, pipenv
Successfully installed certifi-2018.1.18 chardet-3.0.4 flake8-3.5.0 idna-2.6 mccabe-0.6.1 pew-1.1.2 pipenv-9.0.3 pycodestyle-2.3.1 pyflakes-1.6.0 requests-2.18.4 urllib3-1.22 virtualenv-15.1.0 virtualenv-clone-0.2.6
Removing intermediate container 95827f363022
 ---> 5c4805a82b1e
Step 6/7 : RUN pipenv install --system
 ---> Running in 083ee437bbd2
Installing dependencies from Pipfile.lock (c2c6d4)
Removing intermediate container 083ee437bbd2
 ---> 8750b71fcc3f
Step 7/7 : EXPOSE 8000
 ---> Running in 79daa2dc8134
Removing intermediate container 79daa2dc8134
 ---> c5e7e58a668c
Successfully built c5e7e58a668c
Successfully tagged ch4messageboardapp_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, posts, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying posts.0001_initial... OK
  Applying sessions.0001_initial... OK

docker-compose run web python /code/manage.py createsuperuser

Y:\projects_id3\P5N001\XLOGCA135_tutorial_docker\tutorial_docker\tutoriels\djangoforbeginners\ch4-message-board-app>docker-compose run web python /code/manage.py createsuperuser
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Starting ch4messageboardapp_db_1 ... done
Username (leave blank to use 'root'):
Email address: patrick.vergain@id3.eu
Password:
Password (again):
The password is too similar to the email address.
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
Superuser created successfully.
../../_images/create_superuser.png

docker-compose run web python /code/manage.py createsuperuser

docker-compose up

Y:\projects_id3\P5N001\XLOGCA135_tutorial_docker\tutorial_docker\tutoriels\djangoforbeginners\ch4-message-board-app>docker-compose up
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

ch4messageboardapp_db_1 is up-to-date
Creating ch4messageboardapp_web_1 ... done
Attaching to ch4messageboardapp_db_1, ch4messageboardapp_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   |
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   |
db_1   | Data page checksums are disabled.
db_1   |
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   | syncing data to disk ... ok
db_1   |
db_1   | Success. You can now start the database server using:
db_1   |
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   |
db_1   |
db_1   | WARNING: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | ****************************************************
db_1   | WARNING: No password has been set for the database.
db_1   |          This will allow anyone with access to the
db_1   |          Postgres port to access your database. In
db_1   |          Docker's default configuration, this is
db_1   |          effectively any other container on the same
db_1   |          system.
db_1   |
db_1   |          Use "-e POSTGRES_PASSWORD=password" to set
db_1   |          it in "docker run".
db_1   | ****************************************************
db_1   | waiting for server to start....2018-01-23 08:34:30.556 UTC [39] LOG:  listening on IPv4 address "127.0.0.1", port 5432
db_1   | 2018-01-23 08:34:30.557 UTC [39] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
db_1   | 2018-01-23 08:34:30.557 UTC [39] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
db_1   | 2018-01-23 08:34:30.682 UTC [39] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-23 08:34:30.865 UTC [40] LOG:  database system was shut down at 2018-01-23 08:34:28 UTC
db_1   | 2018-01-23 08:34:30.928 UTC [39] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   | ALTER ROLE
db_1   |
db_1   |
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   |
db_1   | 2018-01-23 08:34:31.493 UTC [39] LOG:  received fast shutdown request
db_1   | waiting for server to shut down....2018-01-23 08:34:31.557 UTC [39] LOG:  aborting any active transactions
db_1   | 2018-01-23 08:34:31.559 UTC [39] LOG:  worker process: logical replication launcher (PID 46) exited with exit code 1
db_1   | 2018-01-23 08:34:31.560 UTC [41] LOG:  shutting down
db_1   | 2018-01-23 08:34:32.052 UTC [39] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   |
db_1   | PostgreSQL init process complete; ready for start up.
db_1   |
db_1   | 2018-01-23 08:34:32.156 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2018-01-23 08:34:32.156 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2018-01-23 08:34:32.256 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-23 08:34:32.429 UTC [57] LOG:  database system was shut down at 2018-01-23 08:34:31 UTC
db_1   | 2018-01-23 08:34:32.483 UTC [1] LOG:  database system is ready to accept connections
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  | January 23, 2018 - 08:46:09
web_1  | Django version 2.0.1, using settings 'mb_project.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

We can confirm it works by navigating to http://127.0.0.1:8000/ where you’ll see the same homepage as before.

docker-compose ps

PS Y:\projects_id3\P5N001\XLOGCA135_tutorial_docker\tutorial_docker\tutoriels\william_vincent\ch4-message-board-app> docker-compose ps
          Name                        Command               State           Ports
------------------------------------------------------------------------------------------
ch4messageboardapp_db_1    docker-entrypoint.sh postgres    Up      5432/tcp
ch4messageboardapp_web_1   python /code/manage.py run ...   Up      0.0.0.0:8000->8000/tcp

::

docker-compose exec db bash

docker-compose exec db bash

psql -d db -U postgres

root@ee941cf5bc20:/# psql -U postgres
psql (10.1)
Type "help" for help.

dt

postgres=# \dt
                   List of relations
 Schema |            Name            | Type  |  Owner
--------+----------------------------+-------+----------
 public | auth_group                 | table | postgres
 public | auth_group_permissions     | table | postgres
 public | auth_permission            | table | postgres
 public | auth_user                  | table | postgres
 public | auth_user_groups           | table | postgres
 public | auth_user_user_permissions | table | postgres
 public | django_admin_log           | table | postgres
 public | django_content_type        | table | postgres
 public | django_migrations          | table | postgres
 public | django_session             | table | postgres
 public | posts_post                 | table | postgres
(11 rows)

conninfo

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

dn

postgres=# \dn
  List of schemas
  Name  |  Owner
--------+----------
 public | postgres
(1 row)

d posts_post

postgres=# \d posts_post
                            Table "public.posts_post"
 Column |  Type   | Collation | Nullable |                Default
--------+---------+-----------+----------+----------------------------------------
 id     | integer |           | not null | nextval('posts_post_id_seq'::regclass)
 text   | text    |           | not null |
Indexes:
    "posts_post_pkey" PRIMARY KEY, btree (id)