martes, 6 de marzo de 2018

Alfresco & Docker (1): Instalación de PostgreSQL

1. Introducción


Primeramente agradecer estos enlaces:


Se plantea inicialmennte tener 2 contenedores:
  1. PostgreSQL
  2. Alfresco Repo
  3. LibreOffice (??)
Se orquestan los contenedores (servicios) con docker compose.

2. PostgreSQL


Se descarga la imagen oficial de postgres

docker pull postgres


Using default tag: latest
latest: Pulling from library/postgres
3e731ddb7fc9: Pull complete 
6c47120e994b: Pull complete 
5f766d668e8a: Pull complete 
25131cac3889: Pull complete 
64d99bc5f521: Pull complete 
52f77b20404c: Pull complete 
f170bb6b5bb2: Pull complete 
ccbe1546c300: Pull complete 
2ca8dba273c8: Pull complete 
984cfa5f7b21: Pull complete 
a5f187428a43: Pull complete 
375087219075: Pull complete 
7349f2a3e6e2: Pull complete 
Digest: sha256:b7e73f7768f8bdab547c56e921d7ac37d27368ff802677417b35aa9a633973bd
Status: Downloaded newer image for postgres:latest

Si le damos a docker image ls aparecerá. Ejecutamos la imagen


docker run --name 'postgresql' \
-e POSTGRES_USER=alfresco_pg \
-e POSTGRES_PASSWORD=123456 \
-v $HOME/alfresco/database:/var/lib/postgresql/data \
-d postgres

siendo:
 --name: nombre que le damos al contenedor al crearlo
-e : para pasarle variables de entorno de la forma "nombre=valor"
-v: monta en un volumen del host un volumen del contenedor con el formato -v host_vol: cont_vol
-d: ejecuta el contenedor en background. (tambien "detached mode", para poder  es lo contrario a ejecutarlo con un terminal "-ti" que es en foreground. En foreground tu consola se  adjunta al proceso). Al hacerlo en background, podemos ejercutar la consola del contenedor en más terminales. Para ello, podemos abrir cuialquier sesión de pantalla (tty) y ejecutar los comandos docker exec -ti postgresql psql -U alfresco_pg y docker attach postgresql que lo vermos mas adelante

Y si le damos a docker container ls nos aparecerá en marcha


CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6cafae9ea111        postgres            "docker-entrypoint.s…"   6 seconds ago       Up 5 seconds        5432/tcp            postgresql

Y si inspeccionamos el container,cpodemos ver variables de entorno, montaje de volúmenes, IPs..

docker inspect postgresql

o tambien: docker container inspect postgresql

[
    {
        "Id": "6cafae9ea111f6762d7ea5857faf28775a53e807b189495b81f8b733eeee9d45",
        "Created": "2018-03-08T06:30:07.510339765Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "postgres"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2701,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-03-08T06:30:07.654646355Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:6152297591eb02597c30ea6f847822ae6ce1da32cc1322e17523054efc89a8fe",
        "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/6cafae9ea111f6762d7ea5857faf28775a53e807b189495b81f8b733eeee9d45/resolv.conf",
        "HostnamePath": "/mnt/sda1/var/lib/docker/containers/6cafae9ea111f6762d7ea5857faf28775a53e807b189495b81f8b733eeee9d45/hostname",
        "HostsPath": "/mnt/sda1/var/lib/docker/containers/6cafae9ea111f6762d7ea5857faf28775a53e807b189495b81f8b733eeee9d45/hosts",
        "LogPath": "/mnt/sda1/var/lib/docker/containers/6cafae9ea111f6762d7ea5857faf28775a53e807b189495b81f8b733eeee9d45/6cafae9ea111f6762d7ea5857faf28775a53e807b189495b81f8b733eeee9d45-json.log",
        "Name": "/postgresql",
        "RestartCount": 0,
        "Driver": "aufs",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home/eduard/alfresco/database:/var/lib/postgresql/data"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "nw_alfresco",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": null,
            "Name": "aufs"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/eduard/alfresco/database",
                "Destination": "/var/lib/postgresql/data",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "6cafae9ea111",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5432/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "POSTGRES_USER=alfresco_pg",
                "POSTGRES_PASSWORD=123456",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/10/bin",
                "GOSU_VERSION=1.10",
                "LANG=en_US.utf8",
                "PG_MAJOR=10",
                "PG_VERSION=10.3-1.pgdg90+1",
                "PGDATA=/var/lib/postgresql/data"
            ],
            "Cmd": [
                "postgres"
            ],
            "ArgsEscaped": true,
            "Image": "postgres",
            "Volumes": {
                "/var/lib/postgresql/data": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "f1dca2c8ffbdeba9bbd5dc48e47b0500a798c09a91823bd916db3107bdc144f4",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5432/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/f1dca2c8ffbd",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "nw_alfresco": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "6cafae9ea111"
                    ],
                    "NetworkID": "a53b9c24e96646d52b169974d3ffae4c429ce52c159e79db040671c6584f6591",
                    "EndpointID": "fdf08e2e3d5663a1f4d9208d659072fbc1870550246cb557dabd6de02f69af48",
                    "Gateway": "172.19.0.1",
                    "IPAddress": "172.19.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:13:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]


Veamos que funciona en background o detached mode (la opción -d de run) Según las instrucciones
de Qamar-ud-Din

No debemos utilizar usuarios, ni esquemas  que comiencen por "pg"

Vamos a ejecutar una consola de SQL sobre nuestro contenedor

docker exec -ti postgresql psql -U alfresco_pg 

para saber los usuarios que hay creados hacer en la consola sql

SELECT u.usename AS "User name", u.usesysid AS "User ID",
       CASE WHEN u.usesuper AND u.usecreatedb THEN CAST('superuser, create database' AS pg_catalog.text)
            WHEN u.usesuper THEN CAST('superuser' AS pg_catalog.text)
            WHEN u.usecreatedb THEN CAST('create database' AS pg_catalog.text)
            ELSE CAST('' AS pg_catalog.text)  
       END AS "Attributes"
FROM pg_catalog.pg_user u 
ORDER BY 1;


Y nos sale


  User name  | User ID |         Attributes         
-------------+---------+----------------------------
 alfresco_pg |   32768 | superuser, create database
 postgres    |      10 | superuser, create database
(2 rows)

Veamos los esquemas creados ejecutando dentro de la consola


\l

Y sale


    Name     |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-------------+----------+----------+------------+------------+-----------------------
 alfresco_pg | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 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
(4 rows)

Como vemos, nos ha creado un esquema llamado alfresco_pg  (como nuestro usuario)

Para salir de la consola de PostgresSQL basta con hacer "Control + d" y volvemos a la consola de nuestro host.

Si queremos entrar dentro del contenedor tecleamos

docker exec -ti postgresql /bin/bash 

Abrimos otra tty y podemos comprobar que el directorio que hemos hecho un bind en el host, ya tiene información. Pero tenemos que entrar con sudo.

Aparte, si tenemos postgres instalado en el host, podemos utilizar el cliente psql para ver el postgres del container. Para ello paramos el container y lo eliminamos y volvemos a correr com el remapeo de puertos a 1111 de nuestro servidor. Aqui no hemos corrido postgres en modo red!


docker container kill postgresql
docker container rm   postgresql

docker run --name 'postgresql' -e POSTGRES_USER=alfresco_pg -e POSTGRES_PASSWORD=123456 -v $HOME/alfresco/database:/var/lib/postgresql/data -d -p 1111:5432 postgres

psql -h localhost -p 1111 -U alfresco_pg

Y nos pide la contraseña del usuario alfresco_pg


Password for user alfresco_pg: 
psql (9.5.12, server 10.3 (Debian 10.3-1.pgdg90+1))
WARNING: psql major version 9.5, server major version 10.
         Some psql features might not work.
Type "help" for help.

alfresco_pg=# \q






No hay comentarios :

Publicar un comentario