Open SimonLab opened 5 years ago
We can add a section on how to run postgres in a Docker container. This is useful when you don't want to setup Postgres on your machine.
1 Get Postgres from docker: docker pull postgres
docker pull postgres
2 Start a new postgres container: docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=mydb -d -p 5432:5432 postgres
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=mydb -d -p 5432:5432 postgres
where --rm clean up the container, ie remove the container when it stops, see https://docs.docker.com/engine/reference/run/#clean-up-rm
--name is the name of the container, you can choose the name you want
-e for defining some environment variables, here we are setting the password and the database name
-d to run the container as a deamon, ie as a background process
-p to expose the port of the container, this allow us to connect to postgres with psql for example
3 Check the container has been created with docker container ls
docker container ls
4 Connect to the database with psql psql -h localhost -U postgres -d mydb
psql -h localhost -U postgres -d mydb
5 To stop postgres and the container: docker stop pg-docker where pg-docker is the name of the container
docker stop pg-docker
pg-docker
@SimonLab any chance you can add this to https://github.com/dwyl/learn-docker ? ð
We can add a section on how to run postgres in a Docker container. This is useful when you don't want to setup Postgres on your machine.
1 Get Postgres from docker:
docker pull postgres
2 Start a new postgres container:
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=mydb -d -p 5432:5432 postgres
where --rm clean up the container, ie remove the container when it stops, see https://docs.docker.com/engine/reference/run/#clean-up-rm
--name is the name of the container, you can choose the name you want
-e for defining some environment variables, here we are setting the password and the database name
-d to run the container as a deamon, ie as a background process
-p to expose the port of the container, this allow us to connect to postgres with psql for example
3 Check the container has been created with
docker container ls
4 Connect to the database with psql
psql -h localhost -U postgres -d mydb
5 To stop postgres and the container:
docker stop pg-docker
wherepg-docker
is the name of the container