Tasks
- create directories holding the data later on
- create network
- start container
mkdir:
/Users/[your_username]/Develop
/Users/[your_username]/Develop/postgres_data
/Users/[your_username]/Develop/postgres_data/13.2
create network:
docker network create dev-network
docker run --restart always --name postgres13.2 --net dev-network -v /Users/[your_username]/Develop/postgres_data/13.2:/var/lib/postgresql/data -p 5432:5432 -d -e POSTGRES_PASSWORD=[your_password] postgres:13.2
Here is what each parameter in that command means:
--restart always will restart this container any time Docker is started, such as for a laptop reboot or if Docker gets shut down and started again. Leave this parameter out if you want to start your own containers every time.
--name postgres13.2 assigns the name “postgres13.2” to your container instance. This is the way it will appear in the Docker dashboard. Adding the version number to the name is handy if you’re planning on running multiple versions in containers on your Mac.
--network dev-network will join this container to your local Docker network that you created.
-v /Users/[your_username]/Develop/postgres_data/13.2:
/var/lib/postgresql/data will bind that data folder inside the container volume (/var/lib/postgresql) to the local folder you created on your laptop in the previous step, so that data will persist if the container gets shut down and restarted for any reason.
-p 5432:5432 will bind the Postgres port of the container (5432) to the same port on your Mac. This will make things easier in treating this container as a “localhost” in Postgres.
-d will run this container in detached mode so that it runs in the background.
-e POSTGRES_PASSWORD=[your_password] sets an environment variable (in this case, the PostgreSQL root password inside the container).
postgres:13.2 indicates that the official DockerHub Postgres version tag 13.2 is the one to install. Simply replace the :13.2 with a different version if you’re creating a new instance (e.g. :12.0) and make sure you’ve created a data folder on your Mac that matches so you can bind to it.