How can I use image / container for Postgresql docker?

I'm new to docker. I'm still trying to wrap all this around me.

I am creating a node application (REST api) using Postgresql to store my data.

I spent several days studying dockers, but I'm not sure what I am doing as I should.

So here are my questions:

  • I use the official postgres 9.5 image as the basis for creating my own (my Dockerfile only adds plpython on top of it and installs a custom python module for use in plpython stored procedures). I created a container as suggested by the postgres image docs:

    docker run --name some-postgres -e POSTGRES_PASSWORD = mysecretpassword -d postgres

    After I stop the container, I cannot start it again using the above command, because the container already exists. So I launch it using docker launch instead of docker launch. Is this a normal way to do something? Typically, do I use docker to start the first time and launch docker at another time?

  • Persistence: I created a database and populated it on a running container. I did this using pgadmin3 to connect. I can stop and start the container, and the data is saved, although I'm not sure why and how this happens. I can see in the Dockerfile of the official postgres image that the volumes are created (VOLUME / var / lib / postgresql / data), but I'm not sure if the reason for perseverance works. Could you briefly explain (or point out an explanation) how this all works?

  • Architecture: from what I read, it seems that the most appropriate architecture for such an application would be to run 3 separate containers. One for the database, one for saving database data, and one for the node application. Is this a good way to do this? How does using a data container improve the situation? AFAIK my current setup works fine without it.

  • Is there anything else I should pay attention to?

thanks

EDIT: adding to my confusion, I just launched a new container from the official debian image (without Dockerfile, only docker launched -i -t -d -name-debian debian / bin / bash). When the container is running in the background, I connected to it using docker attach debtest, and switched to apt-get install postgresql. After installation, I ran (still from inside the container) psql and created a table in the default postgres database and populated it with 1 record. Then I left the shell, and the container stopped automatically, since the shell no longer started. I started the container using the docker arrangement, then tied it to it and finally ran psql again. I found that everything is saved from the first run. Postgresql is installed, my table is there, and besides, the record that I inserted is also there. I am really confused about why I need VOLUME to save data, since this quick test did not use one and everything to work fine. Did I miss something?

Thanks again

+5
source share
2 answers

1.

docker run --name some-postgres -e POSTGRES_PASSWORD = mysecretpassword -d postgres

After I stopped the container, I can not start it again using the above because the container already exists.

Right. You named it ( --name some-postgres ), so before starting a new one, the old one must be deleted, for example. docker rm -f some-postgres

So, I'm starting to use docker launch instead of docker launch. Is this a normal way to do things? Will I usually use docker starting the first time and docker starting every time?

No, this is by no means normal for dockers. Typically, docker containers should be ephemeral , which are easily discarded and restarted.

  1. Persistence: ... I can stop and start the container and the data is saved, although I'm not sure why or how this happens ....

This is because you are reusing the same container. Remove the container and the data is gone.

  1. Architecture: from what I read, it seems that the most suitable architecture for such an application will consist of 3 separate containers. One for the database, one for saving the database and one for the node application. Is this a good way to do this? How to make using a data container improve the situation? AFAIK my current setup works fine without it.

Yes, this is a good way to make separate containers for individual problems. This is very useful in many cases, for example, when, for example, you need to update the base postgres image without losing your data (in particular, when the data container begins to play its role).

  1. Is there anything else I should pay attention to?

When you become familiar with the basics of dockers, you can take a look at Docker comp or similar tools to help you run multi-content applications more easily.

+5
source

Short and simple:

  • What you get from the official postgres image is postgres ready to install, as well as some tricks that you can configure using environment variables. With docker run you create a container. Container Lifecycle Commands: docker start/stop/restart/rm Yes, this is the way Docker things.
  • Everything inside the volume is saved. Each container can have an arbitrary number of volumes. Volumes are directories defined within the Docker file, the parent Docker file, or the docker run ... -v /yourdirectoryA -v /yourdirectoryB ... . All external volumes are lost with docker rm . Everything, including volumes, is lost with docker rm -v
  • This is easier to show than to explain. See This is a readme with Docker commands on Github, read how I use the official PostgreSQL image for Jira, and also add NGINX to the mix: Jira with Docker PostgreSQL.Also , a data container is a cheap trick to remove, rebuild, and update the container unnecessarily. move saved data.
  • Congratulations, you were able to understand the basics! Keep it on! Try docker builds to better manage these nasty docker run ... commands and be able to manage multiple containers and data containers.

Note. A blocking flow is required to keep the container running! Either this command must be explicitly specified inside the Docker file, see CMD, or given at the end of the docker run -d ... /usr/bin/myexamplecommand . If your command is blocked by NON, for example. /bin/bash , then the container will always stop immediately after running the command.

+4
source

Source: https://habr.com/ru/post/1242405/


All Articles