>> docker
>> docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
>> start docker
==============================
Dockerfile:
FROM postgres:11.5
ENV POSTGRES_HOST_AUTH_METHOD=trust
ENV POSTGRES_USERNAME='flaskdb'
ENV POSTGRES_PASSWORD='Password1'
COPY ./docker-entrypoint-initdb.d/sql1.sql docker-entrypoint-initdb.d/sql1.sql
CREATE USER flaskdb WITH ENCRYPTED PASSWORD 'Password1';
CREATE DATABASE flaskdb;
GRANT ALL PRIVILEGES ON DATABASE flaskdb TO flaskdb;
#Establishing the connection
conn = psycopg2.connect(
database='flaskdb', user='flaskdb', password='Password1', host='mydb', port= '5432'
)
# Note : host is db-container name not localhost or 127.0.0.1 in docker
#requirements.txt
flask
psycopg2
@@ With Virtual
FROM python:latest
WORKDIR /code
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install virtualenv
RUN python3 -m venv virtual
RUN . virtual/bin/activate
ADD requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY app.py app.py
COPY templates/index.html templates/index.html
CMD ["python", "app.py"]
@@ Another one without virtual:
FROM python:latest
WORKDIR /code
ADD requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY app.py app.py
COPY templates/ templates/
CMD ["python", "app.py"]
version: "3.8"
services:
app:
image: web:v1
container_name: app
ports:
- 5000:5000
links:
- db
networks:
- python-postgres
db:
image: mydb:v1
container_name: mydb
restart: always
ports:
- 5432:5432
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
- PGDATA='/var/lib/postgresql/data'
volumes:
- /var/postgres/data:/var/lib/postgres/data
networks:
- python-postgres
networks:
python-postgres:
driver : bridge
name: python-postgres
@@@Docker-compose file:
@@@mydb log after docker-compose up:
@@@mydb-docker-compose up:
@@@Browser
######################################################################################################
## NOTES
######################################################################################################
# Postgres SQL:
docker run --name db -e POSTGRES_PASSWORD=Password1 -d -p 5432:5432 db:v1
docker exec -it db bash
psql -U flaskdb
postgres=# \l
postgres=# \c flaskdb ---to connect
postgres=# select * from userdetail;
\q to quit
#In case connection refuse in local:
1. Modify two configure files
# vi /var/lib/pgsql/data/postgresql.conf
Replace the line:
listen_addresses = 'localhost' -> listen_addresses = '*'
2. Modify two configure files
# vi /var/lib/pgsql/data/postgresql.conf
Replaced the line:
listen_addresses = 'localhost' -> listen_addresses = '*'
# vi /var/lib/pgsql/data/pg_hba.conf
Add the line at the very end:
host all all 0.0.0.0/0 trust
(If IPv6: host all all ::/0 trust)
3. Restart the database service
# service postgresql restart
docker exec container-name pg_dump -U db_user dbname > dbdump_file
docker exec mydb pg_dump -U flaskdb flaskdb > 2.sql
docker exec mydb pg_dump -U flaskdb flaskdb | gzip > 2.gz
unzip with : gzip -d 2.gz
docker exec mydb pg_dumpall -U postgres --schema-only > definitiononly.sql
In execution of docker :
docker exec mydb pg_dumpall -U postgres --roles-only > allroles.sql
docker exec mydb pg_dumpall -U flaskdb --tablespaces-only > tb.sql
pg_dump -U postgres -W -F t dvdrental > c:\pgbackup\dvdrental.tar
-U postgres: specifies the user to connect to the PostgreSQL database server. We used the postgres in this example.
-W: forces pg_dump to prompt for the password before connecting to the PostgreSQL database server. After you hit enter, pg_dump will prompt for the password of postgres user.
-F : specifies the output file format that can be one of the following:
c: custom-format archive file format
d: directory-format archive
t: tar
p: plain-text SQL script file).
In this example, we use -F t to specify the output file as a tar file.
dvdrental: is the name of the database that you want to back up.
> c:\pgbackup\dvdrental.tar is the output backup file path.