Run PostgreSQL in Docker

To run PostgreSQL on docker, create a directory for saving the data presistant

run docker container

In above, change the value for POSTGRES_DB, POSTGRES_USER and POSTGRES_PASSWORD as needed.

Connect to PostgreSQL server

To connect to PostgreSQL server, run

Now you are inside PostgreSQL conainer, to login, run

It will ask for password. Once you enter password, you will be in PostgreSQL command line.

See docker

Docker compose start container on boot

I have a docker container, that i need to start on server boot.

The docker-compose.yml file i used to create this docker container is

With above docker-compose.yml file. i have to start docker container manually after server reboot.

To make it auto start, add the line

Here is the modified docker-compose.yml file.

You need to rebuild docker container based on this docker-compose.yml file

Change to the folder where your docker-compse.yml file is, in my case it was

Run

docker-compose: error while loading shared libraries

When running docker-compose on CentOS 7, i get following error

To fix the error, do the following

Now create a new file

Add following content

Make it executable

Create temp folder

Now docker-compose will work.

python-flask-docker

Create Python Flask Docker Container

Create a folder and change to the folder

Create file

Add following content

Create file requirements.txt, add “Flask” to it.

Now lets create our Python Flask Application

Add following content

To test Flask application locally, install Flask using pip

Now run the application using

Now you will be able to see web application at

Press CTRL+C to stop the application.

To build Docker image, run

-t specify tag.

If all worked properly, you will see

You can see the container image listed

Now your docker image is ready. To build a container using this image, run

You can access your python application running inside docker container on URL

To stop the application, run

To start it, run

gcloud-container-clusers-create

Create Kubernetes cluser using gcloud

Login to gcloud with

Set default region

To create a cluser, run

To create a cluster with 2 nodes, run

To specify compute engine type during cluster creation, run

You can also specify scopes, this allow you to give the cluster permission to access other google cloud resources. For example to allow cluster access Cloud Source Repositories and Google Container Registry. run

To list your container, run

gcloud clusters

Before you can use cluser, you need to get authentication credentials with command

To interact with Kubernetes, you need kubectl, on Ubuntu, yu can install it with

To deploy a container, run

This will deploy google provided hello-app container to the cluser.

To make the application available to public, create a loadbalancer service.

Now find IP of the LoadBalacner service with command

From the result, we found EXTERNAL-IP for LoadBalancer is 35.232.226.216. To access the application, go to url


http://35.232.226.216:8080

See Kubernetes

Docker Nginx Proxy

Docker Nginx Proxy allow you to run multiple docker containers on same server behind nginx proxy. This is done using

https://github.com/jwilder/nginx-proxy

To do this, you need a server with port 80 and 443 unused.

To setup nginx proxy, run following

This will start nginx proxy. You can modify .env file if you want.

Starting a Docker Web App Behind Proxy

To start a web app, all you need is to start docker container on same network as nginx proxy. By default it is “webproxy”.

Here is an example command to start a web server.

This will start a test web server. You need to point the domain specified to this servers IP, then only nginx proxy can get LetsEncrypt SSL installed.

Replace test.serverok.in with your actual domain.

If you don’t want LetsEncrypt SSL installed, you can remove following 2 options

Docker Build

docker build command is used to create a docker image from Dockerfile.

To create an image, first lets create a working directory and change to the directory.

Create a file with name Dockerfile

Put following content in it

Now build the docker image with

This will create a docker image with name “my-ubuntu”.

To run this, we need to create a container using this image, this can be done with command

Here i used -ti as we need to run interactively using terminal, this is because our the command we set to start is “/bin/bash” in the Dockerfile.

Docker