Docker Project build document - Oliver-Mustoe/Oliver-Mustoe-Tech-Journal GitHub Wiki

In this page I describe the steps I took to setup a PostgreSQL container database that's contents are displayed on a webpage via a PHP & Apache Web server container.

Code/Github setup

First, on a Windows machine, I used Visual Studios to create the following files in a folder named "Code_for_docker_project"

  • docker-compose.yml
  • dockerproj_dirmaker.bash
  • index.php
  • SOURCES.txt
  • Dockerfile
  • class_to_table.sql
  • postgresql_apache_setup.bash

I populated each file with the following:
(Some comments might be cut out of the screenshot, GitHub links included with the file names.)

docker-compose.yml - The docker compose file for this project that performs some setup and downloads/builds the needed images.
1

dockerproj_dirmaker.bash - A bash script that generates the directory structure that the project needs and copies all needed files to it.
2

index.php - A PHP file that will display results of the created PostgreSQL table in a webpage.
3

SOURCES.txt - The sources used on the code (no image since this step is a formality not essential to the project working as intended)

Dockerfile - A Dockerfile that takes the php-apache image that I need and performs installation of needed extensions for "index.php" to work when built in the docker-compose file.
4

class_to_table.sql - A sql file that connects to the created database, creates a table with the needed columns, and inserts some data in to the table.
5

postgresql_apache_setup.bash - A bash file that creates a network for the two containers to function in, connects them to it, runs a .sql file against the database which creates a table with data, copies needed files to mapped Web server root directory, and opens the needed port.
6

I stored the code folder in a folder called "SYS265", then added the code to my repository by going to the code section > clicking "Upload Files" > drag the folder into the section and click the green "Commit changes" button.

Install and setup on docker01

(NOTE: What each file does is described above.)

I first installed "git" on docker01 with the command:

  • sudo apt install git-all

Then I used git to clone the repository with the command:

I then moved into the needed directory (Oliver-Mustoe-Tech-Journal/SYS265/Code_for_docker_project) and ran the file "dockerproj_dirmaker.bash" with the command:

  • bash dockerproj_dirmaker.bash

I then navigated to the newly created directory structure (~/dockerproject) and ran the "docker-compose" file (needs sudo privileges) with the command:

  • sudo docker-compose up -d

I checked here that the containers were working with the command "docker ps" (which I saw two containers running.)

Then I ran the file "postgresql_apache_setup.bash" with the command:

  • bash postgresql_apache_setup.bash

After this I was able to open up a browser, navigate to "docker01-oliver:8008" and was greeted with the following web page:
web

Sources

https://github.com/zhao-lin-li/postgresql-with-docker-compose/blob/master/docker-compose.yml

https://levelup.gitconnected.com/creating-and-filling-a-postgres-db-with-docker-compose-e1607f6f882f

https://docs.docker.com/samples/wordpress/

https://codeburst.io/http-server-on-docker-with-https-7b5468f72874

https://zhao-li.medium.com/getting-started-with-postgresql-using-docker-compose-34d6b808c47c

https://zetcode.com/db/postgresqlphp/read/

https://www.php.net/manual/en/pgsql.examples-basic.php

https://www.guru99.com/create-drop-table-postgresql.html
f https://docs.opennms.com/horizon/29/deployment/core/getting-started.html

https://www.tutorialworks.com/container-networking/#:~:text=To%20allow%20two%20Docker%20containers,tables%20on%20your%20operating%20system.

https://forums.docker.com/t/send-commands-to-container/44385

https://stackoverflow.com/questions/48692741/how-can-i-make-all-line-endings-eols-in-all-files-in-visual-studio-code-unix

https://docs.docker.com/engine/reference/commandline/network_rm/

https://stackoverflow.com/questions/34688465/how-do-i-run-a-sql-file-of-inserts-through-docker-run

https://gist.github.com/ben-albon/3c33628662dcd4120bf4

https://benpetering.com/posts/2020/10/25/php-and-postgres-with-docker.html

https://docs.docker.com/compose/gettingstarted/

https://www.w3schools.com/sql/sql_insert.asp

http://joshualande.com/create-tables-sql

https://stackoverflow.com/questions/12410695/new-line-n-in-php-is-not-working

https://www.geeksforgeeks.org/php-nl2br-function/#:~:text=The%20nl2br()%20is%20an,using%20any%20of%20the%20following.