HOW TO CREATE A DOCKER CONTAINER WITH PostgreSQL:

Bmwitcher
3 min readJul 22, 2020

--

while persisting the data volume

Requirements: *Docker Desktop Installed

Before we start I am an aspiring junior cloud engineer and currently just over 50% through a docker course and attempting to prove my skill set and enhance it through projects, labs, and hopefully certifications.

Let’s get started….

  1. Navigate to your local terminal and create a directory/folder to work in for this lab so that is it easy to locate.
  1. Navigate to the Docker Hub and search for PostgreSQL: https://hub.docker.com/_/postgres
  2. Navigate back to your local terminal (I am using macOS) run the command found on the docker hub: docker pull postgres ( i will use the tag alpine simply because it is the smallest in file size, [docker pull postgres:alpine])
  1. Next, we will create a volume and identified its location so that we can mount a persistent volume in case the container is stopped and we need to connect it to another container.
  1. Next, we will run the docker run command and I will explain each tag and process below.

docker run — name my_pg — mount type=volume,source=docker_lab_vol,destination=/root postgres:alpine sh -c ‘echo hello > /root/message.txt && cat /root/message.txt’ -d .

— name = naming our container

— mount — attaching a volume to the container

Source= this is to target the volume we just created if there was no volume it would create one with the name after the equal sign

Destination = is the targeted directory inside of the container

postgres:alpine is the image name we previously downloaded from docker hub

sh is the shell command we would use inside of the container to run any commands

In this step to ensure that we have connected inside of the container, we are telling the shell to print out “hello” under the root directory inside of the container from the file message.txt

-d is the detached mode

You have now created a docker container that is attached to a persisted volume. You can detach this container from the volume and attach another container by running a similar command with a different name and/or the same or different volume.

7. Now that we received our “hello” let’s check for our volume and container that we created. There are several ways to view containers, volumes, and all of the system information:

Containers:

docker ps — a or docker container ls -a = view all containers (both commands work the same)

Volumes:

docker volume ls

System info:

docker system df or docker system df -v (more detail)

To tear this lab down, I would recommend using the verbose (-v) option above. You will need to stop your container and delete them docker container stop [name or container id], docker container rm [name or container id]. You can now run docker volume prune to remove all unused volumes. If you want to remove the image that we downloaded use the command docker rmi [name of image]

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Bmwitcher
Bmwitcher

Written by Bmwitcher

DevSecOps Professional — AWS Certified DevOps Professional/Security Specialty/SA Pro, Gitlab Certified, Terraform Associate GCP-ACE Certfied and more…

No responses yet

Write a response