Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to safely stop/start my postgres server when using docker-compose

I sometimes stop/start docker very often when I am release new features in my application.

docker-compose up -d
docker-compose stop

I am using pretty much the bare bones postgres docker setup (see below). I am mapping the /data folder to my host.

Is there anything I should be worried about if I stop/start docker many times in a day in terms of data getting corrupted?

Is calling docker-compose stop the best way to be stopping my postgres instance?

My postgres service in my docker-compose looks like this:

  db:
    image: postgres:9.4
    volumes:
      - "/home/deploy/data/pgdata:/var/lib/postgresql/data"
    restart: always

This setup currently is running smoothly in development, but once it goes to production I want to make sure I am following best practices etc.

like image 532
Blankman Avatar asked Sep 30 '18 16:09

Blankman


1 Answers

Use,

docker-compose down -v

What it does is basically removes all the volumes you added. If you don't those volumes will hang on and eat up your space. It only removes the volume inside the docker container. The volume in your host stays and survives container removal in case if you want that data to survive container removal.

Whenever you create a docker container by docker run, Docker creates a volume/ directory to keep the details about the containers. After you execute docker run, if you look into /var/lib/docker/containers, you will see one directory for each container you started. If you have not removed the volumes for previous container, you will see many directories under the "container" directory. The name of these directories will be very long random letters and number. So, if you don't tell the docker to remove these directories when you stop the container, it will be there forever. The v option I mentioned above, will delete these directories when you take down the container. Keep in mind, you can view the contents of the directory /var/lib/docker only as a root user. To change to root user, use sudo -i before you attempt to view the contents of the directory.

like image 102
techtabu Avatar answered Oct 10 '22 09:10

techtabu