Run the command in terminal to install PostgreSQL in Docker(map a port).
docker run --name some-postgres -e POSTGRES_PASSWORD=secret -d -p 5432:5432 postgres
So now you have mapped the port 5432 of your container to port 5432 of your server. -p <host_port>:<container_port> .So now your postgres is accessible from your public-server-ip:5432
I have described the option explanation below :-
--name - The container name
-e - It refers to Postgres User name and password
-d - I will run in Detached mode (background) . if you run it without using it, the docker container will be stopped when you press the Ctrl + C combination or close the terminal screen. This is uncommon because the container should be running in the background.
-p - 5432 port is used by default . but it can be changed this according to preference. Local port and container port can be set separately. The first port is for local, and the second for container.
Postgres - image name
To
test: Run the postgres database (command below).
Now connect to container using container id :-
docker exec -it 5556f4c78c1f bash
Go inside your container and create a database:
root@05b3a3471f6f:/# psql -U postgres
postgres-# CREATE DATABASE mytest;
postgres-# \q
Now connect to container using container id :-
postgres=# \q
root@5556f4c78c1f:/# hostname -i
172.17.0.2
root@5556f4c78c1f:/#
Go to your localhost (where you have some tool or the psql client).
How to connect to PostgreSQL in docker container from outside #
First, check the container id:
root@worldpgdb:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5556f4c78c1f postgres "docker-entrypoint.s…" 3 days ago Up 3 days 5432/tcp some-postgre
root@worldpgdb:~#
Suppose, if we want to verify the Ubuntu version from the
container image the below steps we need to follow:
first, connect to the
container using container ID
Then,
check the release version from /etc/*release* as below commands
Remove container image from docker by step by step:
The
below commands to delete the container which is no longer used
Since it is (image id "b97bae343e06") referenced
to multiple repositories, we have to use the force option to delete the image
permanently which I verified was not working.
Now the image is successfully
deleted with the force option.