Skip to content

Instantly share code, notes, and snippets.

@charleslouis
Last active October 23, 2017 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charleslouis/8a5d6a39b3b316a263cbad9ea56fcac4 to your computer and use it in GitHub Desktop.
Save charleslouis/8a5d6a39b3b316a263cbad9ea56fcac4 to your computer and use it in GitHub Desktop.
Docker, postgres, sails.js

list containers

docker ps -a

Postgres

  • docker pull postgres
  • docker build -t postgres .

Connect to Postgres

docker exec -it postgres psql -U postgres

Run postgres

docker container run -it --rm -p 6666:5432 --name postgres postgres

\list or \l: list all databases \dt: list all tables in the current database You will never see tables in other databases, these tables aren't visible. You have to connect to the correct database to see its tables (and other objects).

To switch databases:

\connect database_name See the manual about psql.

From https://dba.stackexchange.com/questions/1285/how-do-i-list-all-databases-and-tables-using-psql#1288

Mongo

  • docker pull mongo
  • docker build -t mongo .
  • docker container run -it --rm -p 5555:27017 --name mongo mongo

running mongo shell in Docker

docker exec -it mongo mongo

Creat new mongo db

Containers

Run bash in container

docker exec -d 0a9e85052493 bash

Clean exited containers

docker rm $(docker ps -q -f status=exited)

Images

Delete imgage with children

see https://forums.docker.com/t/cant-delete-image-with-children/16667/4 open your terminal and enter : docker inspect --format=’{{.Id}} {{.Parent}}’ $(docker images --filter since=<image_id> -q) where you replace <image_id> by your image id (Yeah, man, obviously) It will kill every children then , write : docker rmi <image_id> if it tell you that it is link to a docker so remove it too with the command : docker rm < docker_id> and then go back to point 2) to end the process .

Fetch data

Ex: from frontend Universal React (Express) server to Sails.js API

You can acces http://127.0.0.1:1337/ from browser ? But the front App CAN NOT ? Docker container has its own network IPs, including its own localhost. In the current situation The stack is (api(sailjs) + db(postgres) + app(Universal React Next)). All of them are in dedicated docker containers. All the containers are behind a 4th one named server, working as a proxy. So when APP fetches API server side, it does in in the docker network. This is why you can acces http://127.0.0.1:1337/ from browser (front)? But the front App CAN NOT (because the fetch is server side) ?

  • List containers docker ps -a
  • Inspect the API docker container inspect <container_id>
  • Get the Gateway": "172.21.0.1" which is the "IPAddress": "172.21.0.5" where the last bit is replaced by 1
  • Fetch on Gatewy - React example const res = await fetch('http://172.21.0.1:9999/api/user')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment