Attention ceci est mon brouillon avant de faire une belle documentation sur Docker (il y a à boire et à manger).
Maintenant on a mieux compris le système (enfin j’ai mieux compris) et l’on sait qu’il faut faire les choses dans une certain ordre (Les précédents POST n°1 et n°2 ) .
- On a compris qu’il fallait une partition spéciale (par exemple Btrfs) pour Docker.
- On a compris qu’il devait y avoir une version « spéciale » pour Docker sous Oracle Linux. Mais on a préféré prendre la version standard. Par exemple on doit avoir le fichier /etc/sysconfig/docker .
- On a compris qu’il fallait avant tout faire le fichier Dockerfile
, puis le « docker build » et « docker run« .
- On a aussi l’option -v qui permet de faire le lien entre l’hôte et le container.
- On a pris le risque de mettre le dernier noyau 4.1.12-32.2.3.el7uek.x86_64 , afin d’avoir la dernière version de Docker. Ce n’est jamais bon d’être sur les derniers version car on est le premier a se prendre les nouveaux problèmes.
Maintenant on va pouvoir lancer des containers en utilisant la syntaxe :
docker run -p <hôte_port1>:<container_port1> -p <hôte_port2>:<container_port2> …
Car jusqu’à présent on n’a pas vu de port en écoute, c’est parce que l’on n’a pas fait le lien entre le port hôte et le port du conteneur.
Plutôt que de continuer avec HAproxy on va faire avoir PostgreSQL qui me semble plus simple à mettre en oeuvre dans un premier temps.
Je redonne les informations de mon docker :
[root@localhost ~]# docker info
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 4
Server Version: 1.11.0
Storage Driver: btrfs
Build Version: Btrfs v3.19.1
Library Version: 101
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge null host
Kernel Version: 4.1.12-32.2.3.el7uek.x86_64
Operating System: Oracle Linux Server 7.2
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 739.1 MiB
Name: localhost.localdomain
ID: X7PA:FHLQ:XKJJ:IH72:P5HO:ULDH:ZMID:VGSB:KOMV:AJGF:SEY6:PTGV
Docker Root Dir: /docker
Debug mode (client): false
Debug mode (server): false
Registry: https://index.docker.io/v1/
On se lance donc dans PostgreSQL, d’abord on regarde ce qui est disponible :
[root@localhost ~]# docker search postgres
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
postgres The PostgreSQL object-relational database ... 1909 [OK]
abevoelker/postgres Postgres 9.3 + WAL-E + PL/V8 and PL/Python... 10 [OK]
macadmins/postgres Postgres that accepts remote connections b... 8 [OK]
jamesbrink/postgres Highly configurable PostgreSQL container. 5 [OK]
linuxkonsult/postgres A Postgres trusted build installed with Chef. 5 [OK]
cptactionhank/postgres 4 [OK]
kampka/postgres A postgresql image build on top of an arch... 2 [OK]
azukiapp/postgres Docker image to run PostgreSQL by Azuki - ... 2 [OK]
eeacms/postgres Docker image for PostgreSQL (RelStorage re... 2 [OK]
icescrum/postgres PostgreSQL image for iceScrum. 1 [OK]
clkao/postgres-plv8 Docker image for running PLV8 1.4 on Postg... 1 [OK]
jgiannuzzi/postgres-bdr Docker image for PostgreSQL with BDR support 1 [OK]
baselibrary/postgres ThoughtWorks Postgres & PostGIS Docker Image 1 [OK]
travix/postgres A container to run the PostgreSQL database. 0 [OK]
timbira/postgres Postgres containers 0 [OK]
blacklabelops/postgres Postgres Image for Atlassian Applications 0 [OK]
livingdocs/postgres Postgres v9.3 with the plv8 extension inst... 0 [OK]
opencog/postgres This is a configured postgres database for... 0 [OK]
beorc/postgres Ubuntu-based PostgreSQL server 0 [OK]
vrtsystems/postgres PostgreSQL image with added init hooks, bu... 0 [OK]
vibioh/postgres PostgresQL Server 0 [OK]
coreroller/postgres official postgres:9.4 image but it adds 2 ... 0 [OK]
khipu/postgres postgres with custom uids 0 [OK]
aheimsbakk/postgres-nosync `postgres` docker with `synchronous_commit... 0 [OK]
akilli/postgres debian:jessie based PostgreSQL image 0 [OK]
Maintenant je regarde quel est la dernière version de PostgreSQL :
FROM postgres:latest
MAINTAINER toto toto@cyber-neurones.org
RUN yum install postgresql && yum install postgresql-server
RUN /etc/init.d/postgresql initdb
# Modification de la configuration
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf
# Le port en ecoute
EXPOSE 5432
# Ajout des volumes pour faire un backup
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/pgsql"]
# Pour lancer postgres
CMD ["/var/lib/pgsql/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]
Je me lance donc dans le test de mon premier fichier ! La première étape c’est le build :
[root@localhost ~]# docker build -t my-postgres .
Sending build context to Docker daemon 15.36 kB
Step 1 : FROM postgres:latest
latest: Pulling from library/postgres
efd26ecc9548: Already exists
a3ed95caeb02: Pull complete
aef28b128f84: Pull complete
5c6681af0d52: Pull complete
43fd2592d569: Pull complete
90b140d542bd: Pull complete
9f56303d7da8: Pull complete
75f16d91384c: Pull complete
a7e55988e47e: Pull complete
6980fe8c2745: Pull complete
94512148bc3a: Pull complete
055e85b433f4: Pull complete
Digest: sha256:3ab14aa8758e62570f589848a93039a55c9e3ab123090c58721374d4f633ff71
Status: Downloaded newer image for postgres:latest
---> 0f3af79d8673
Step 2 : MAINTAINER toto toto@cyber-neurones.org
---> Running in 4ee6e08a7c8c
---> dadadc713dfa
Removing intermediate container 4ee6e08a7c8c
Step 3 : RUN yum install postgresql && yum install postgresql-server RUN /etc/init.d/postgresql initdb
---> Running in 5cfa35a21346
/bin/sh: 1: yum: not found
The command '/bin/sh -c yum install postgresql && yum install postgresql-server RUN /etc/init.d/postgresql initdb' returned a non-zero code: 127
Aie le yum ne passe pas … j’ai ajouté l’option -y mais pas mieux (l’option -y permet de répondre y aux questions, c’est impératif en ligne de commande).
RUN yum install postgresql -y && yum install postgresql-server -y RUN /etc/init.d/postgresql initdb
La commande yum n’est pas dispo, on est donc sur une base Débian. On va donc faire un apt-get ! ou alors on va partir d’une base de Fédora (proche de Oracle Linux) pour avoir la commande yum.
Voici donc mon nouveau fichier :
FROM fedora
MAINTAINER toto toto@cyber-neurones.org
RUN yum install postgresql -y
RUN yum install postgresql-server -y
RUN /etc/init.d/postgresql initdb
# Modification de la configuration
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf
# Le port en ecoute
EXPOSE 5432
# Ajout des volumes pour faire un backup
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/pgsql"]
# Pour lancer postgres
CMD ["/var/lib/pgsql/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]
On refait un test :
[root@localhost ~]# docker build -t my-postgres .
Sending build context to Docker daemon 15.36 kB
Step 1 : FROM fedora
---> ddd5c9c1d0f2
Step 2 : MAINTAINER toto toto@cyber-neurones.org
---> Using cache
---> f170274e277c
Step 3 : RUN yum install postgresql -y
---> Running in a8bd83110986
Yum command has been deprecated, redirecting to '/usr/bin/dnf install postgresql -y'.
See 'man dnf' and 'man yum2dnf' for more information.
To transfer transaction metadata from yum to DNF, run:
'dnf install python-dnf-plugins-extras-migrate && dnf-2 migrate'
Last metadata expiration check performed 0:00:47 ago on Thu Apr 14 17:50:24 2016.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
postgresql x86_64 9.4.7-1.fc23 updates 1.1 M
postgresql-libs x86_64 9.4.7-1.fc23 updates 240 k
Transaction Summary
================================================================================
Install 2 Packages
Total download size: 1.3 M
Installed size: 4.4 M
Downloading Packages:
--------------------------------------------------------------------------------
Total 75 kB/s | 1.3 MB 00:18
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Installing : postgresql-libs-9.4.7-1.fc23.x86_64 1/2
Installing : postgresql-9.4.7-1.fc23.x86_64 2/2
Verifying : postgresql-9.4.7-1.fc23.x86_64 1/2
Verifying : postgresql-libs-9.4.7-1.fc23.x86_64 2/2
Installed:
postgresql.x86_64 9.4.7-1.fc23 postgresql-libs.x86_64 9.4.7-1.fc23
Complete!
---> 398c5e8555cb
Removing intermediate container a8bd83110986
Step 4 : RUN yum install postgresql-server -y
---> Running in 1dc2f23f63bd
Yum command has been deprecated, redirecting to '/usr/bin/dnf install postgresql-server -y'.
See 'man dnf' and 'man yum2dnf' for more information.
To transfer transaction metadata from yum to DNF, run:
'dnf install python-dnf-plugins-extras-migrate && dnf-2 migrate'
Last metadata expiration check performed 0:01:15 ago on Thu Apr 14 17:50:24 2016.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
postgresql-server x86_64 9.4.7-1.fc23 updates 4.0 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 4.0 M
Installed size: 17 M
Downloading Packages:
--------------------------------------------------------------------------------
Total 585 kB/s | 4.0 MB 00:07
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Installing : postgresql-server-9.4.7-1.fc23.x86_64 1/1
Verifying : postgresql-server-9.4.7-1.fc23.x86_64 1/1
Installed:
postgresql-server.x86_64 9.4.7-1.fc23
Complete!
---> 7592731d113b
Removing intermediate container 1dc2f23f63bd
Step 5 : RUN /etc/init.d/postgresql initdb
---> Running in 29aa04f7d09a
/bin/sh: /etc/init.d/postgresql: No such file or directory
The command '/bin/sh -c /etc/init.d/postgresql initdb' returned a non-zero code: 127
Encore un échec, mais je ne baisse pas les bras 😉 Voici ma version n°3 du fichier :
FROM fedora
MAINTAINER toto toto@cyber-neurones.org
RUN dnf install postgresql -y
RUN dnf install postgresql-server -y
RUN postgresql-setup initdb
# Modification de la configuration
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf
# Le port en ecoute
EXPOSE 5432
# Ajout des volumes pour faire un backup
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/pgsql"]
# Pour lancer postgres
CMD ["/var/lib/pgsql/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]
Maintenant le test :
[root@localhost ~]# docker build -t my-postgres .
Sending build context to Docker daemon 15.36 kB
Step 1 : FROM fedora
---> ddd5c9c1d0f2
Step 2 : MAINTAINER toto toto@cyber-neurones.org
---> Using cache
---> f170274e277c
Step 3 : RUN dnf install postgresql -y
---> Running in 3cbd03b57500
Last metadata expiration check performed 0:00:43 ago on Thu Apr 14 17:57:13 2016.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
postgresql x86_64 9.4.7-1.fc23 updates 1.1 M
postgresql-libs x86_64 9.4.7-1.fc23 updates 240 k
Transaction Summary
================================================================================
Install 2 Packages
Total download size: 1.3 M
Installed size: 4.4 M
Downloading Packages:
--------------------------------------------------------------------------------
Total 75 kB/s | 1.3 MB 00:18
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Installing : postgresql-libs-9.4.7-1.fc23.x86_64 1/2
Installing : postgresql-9.4.7-1.fc23.x86_64 2/2
Verifying : postgresql-9.4.7-1.fc23.x86_64 1/2
Verifying : postgresql-libs-9.4.7-1.fc23.x86_64 2/2
Installed:
postgresql.x86_64 9.4.7-1.fc23 postgresql-libs.x86_64 9.4.7-1.fc23
Complete!
---> 3d2c98e008bc
Removing intermediate container 3cbd03b57500
Step 4 : RUN dnf install postgresql-server -y
---> Running in ea14d4661833
Last metadata expiration check performed 0:01:10 ago on Thu Apr 14 17:57:13 2016.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
postgresql-server x86_64 9.4.7-1.fc23 updates 4.0 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 4.0 M
Installed size: 17 M
Downloading Packages:
--------------------------------------------------------------------------------
Total 488 kB/s | 4.0 MB 00:08
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Installing : postgresql-server-9.4.7-1.fc23.x86_64 1/1
Verifying : postgresql-server-9.4.7-1.fc23.x86_64 1/1
Installed:
postgresql-server.x86_64 9.4.7-1.fc23
Complete!
---> 7dde38450b15
Removing intermediate container ea14d4661833
Step 5 : RUN postgresql-setup initdb
---> Running in 1fa62834212e
WARNING: using obsoleted argument syntax, try --help
WARNING: arguments transformed to: postgresql-setup --initdb --unit postgresql
/usr/share/postgresql-setup/library.sh: line 63: find: command not found
Failed to get D-Bus connection: Operation not permitted
Failed to get D-Bus connection: Operation not permitted
FATAL: no db datadir (PGDATA) configured for 'postgresql.service' unit
The command '/bin/sh -c postgresql-setup initdb' returned a non-zero code: 1
On va dire que l’on a encore avancé mais pas facile de trouver de la documentation claire pour l’instant. En plus du yum qui a changé en dnf, la commande l’initialisation de postgres n’est plus la même. La compatibilité ascendante c’est pour demain 😉
[root@localhost ~]# cat Dockerfile
FROM fedora
MAINTAINER toto toto@cyber-neurones.org
RUN dnf install postgresql -y
RUN dnf install postgresql-server -y
RUN postgresql-setup --initdb --unit postgresql
# Modification de la configuration
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf
# Le port en ecoute
EXPOSE 5432
# Ajout des volumes pour faire un backup
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/pgsql"]
# Pour lancer postgres
CMD ["/var/lib/pgsql/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]
[root@localhost ~]# docker build -t my-postgres .
Sending build context to Docker daemon 15.36 kB
Step 1 : FROM fedora
---> ddd5c9c1d0f2
Step 2 : MAINTAINER toto toto@cyber-neurones.org
---> Using cache
---> f170274e277c
Step 3 : RUN dnf install postgresql -y
---> Using cache
---> 3d2c98e008bc
Step 4 : RUN dnf install postgresql-server -y
---> Using cache
---> 7dde38450b15
Step 5 : RUN postgresql-setup --initdb --unit postgresql
---> Running in b4dd8e2cef49
/usr/share/postgresql-setup/library.sh: line 63: find: command not found
Failed to get D-Bus connection: Operation not permitted
Failed to get D-Bus connection: Operation not permitted
FATAL: no db datadir (PGDATA) configured for 'postgresql.service' unit
The command '/bin/sh -c postgresql-setup --initdb --unit postgresql' returned a non-zero code: 1
Je ne vais pas faire d’initdb …
[root@localhost ~]# docker build -t my-postgres .
Sending build context to Docker daemon 15.36 kB
Step 1 : FROM fedora
---> ddd5c9c1d0f2
Step 2 : MAINTAINER toto toto@cyber-neurones.org
---> Using cache
---> f170274e277c
Step 3 : RUN dnf install postgresql -y
---> Using cache
---> 3d2c98e008bc
Step 4 : RUN dnf install postgresql-server postgresql-contrib -y
---> Using cache
---> 29c873629d39
Step 5 : RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
---> Using cache
---> 4a99cf64311c
Step 6 : RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf
---> Using cache
---> 4b431bcfe6fb
Step 7 : USER postgres
---> Using cache
---> b88497ef1a95
Step 8 : ENV PGDATA /var/lib/pgsql/data
---> Using cache
---> 9272c705d631
Step 9 : EXPOSE 5432
---> Running in b2e4c043bc2e
---> a6ea2c236302
Removing intermediate container b2e4c043bc2e
Step 10 : VOLUME /etc/postgresql /var/log/postgresql /var/lib/pgsql
---> Running in cf06d787cecf
---> cffe96473050
Removing intermediate container cf06d787cecf
Step 11 : CMD /var/lib/pgsql/bin/postgres -D /var/lib/pgsql/data -c config_file=/var/lib/pgsql/data/postgresql.conf
---> Running in 7690289ad7cb
---> 1204d059861e
Removing intermediate container 7690289ad7cb
Successfully built 1204d059861e
[root@localhost ~]# cat Dockerfile
FROM fedora
MAINTAINER toto toto@cyber-neurones.org
RUN dnf install postgresql -y
RUN dnf install postgresql-server postgresql-contrib -y
# Modification de la configuration
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf
USER postgres
ENV PGDATA /var/lib/pgsql/data
#RUN initdb --data-checksums
# Le port en ecoute
EXPOSE 5432
# Ajout des volumes pour faire un backup
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/pgsql"]
# Pour lancer postgres
CMD ["/var/lib/pgsql/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]
Maintenant je lance :
[root@localhost ~]# docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=password -d postgres
a590ea4020c45974f68d2e9b7296d83e4176a2dca109510abd72a80b0d26fc11
[root@localhost ~]# ss -at
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:ssh *:*
LISTEN 0 100 127.0.0.1:smtp *:*
ESTAB 0 0 192.168.1.13:ssh 192.168.1.10:62127
LISTEN 0 128 :::ssh :::*
LISTEN 0 128 :::postgres :::*
LISTEN 0 100 ::1:smtp :::*
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a590ea4020c4 postgres "/docker-entrypoint.s" 5 minutes ago Up 5 minutes 0.0.0.0:5432->5432/tcp postgres
Le port semble être en écoute, maintenant il me faut perfectionner la conf de PostgreSQL et installé des outils en local afin d’avoir accès à la base.
[root@localhost ~]# yum install postgresql
[root@localhost ~]# yum install telnet
Je vais donc modifier ce fichier :
FROM fedora
MAINTAINER toto toto@cyber-neurones.org
RUN dnf install postgresql -y
RUN dnf install postgresql-server postgresql-contrib -y
# Modification de la configuration
RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
RUN echo "listen_addresses='*'" >> /var/lib/pgsql/data/postgresql.conf
RUN echo "tcpip_socket = true" >> /var/lib/pgsql/data/postgresql.conf
USER postgres
ENV PGDATA /var/lib/pgsql/data
#RUN initdb --data-checksums
# Le port en ecoute
EXPOSE 5432
# Ajout des volumes pour faire un backup
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/pgsql"]
# Pour lancer postgres
CMD ["/var/lib/pgsql/bin/postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"]
Le but étant de faire fonctionner le connexion à la base par la suite.
J’aime ça :
J’aime chargement…