1

I made this unit file.

[Unit]
Description=myservice
Requires=docker.service
After=docker.service

[Service]
Restart=always

# Remove old containers, images and volumes
ExecStartPre=/usr/local/bin/docker-compose -f my.yml down -v
ExecStartPre=/usr/local/bin/docker-compose -f my.yml rm -v
ExecStartPre=-/bin/bash -c 'docker volume rm $(docker volume ls -q)'
ExecStartPre=-/bin/bash -c 'docker rmi $(docker images | grep "<none>" | awk \'{print $3}\')'
ExecStartPre=-/bin/bash -c 'docker rm -v $(docker ps -aq)'

# Compose up
ExecStart=/usr/local/bin/docker-compose -f my.yml up

# Compose down, remove containers and volumes
ExecStop=/usr/local/bin/docker-compose -f my.yml down -v

[Install]
WantedBy=multi-user.target

Before running this file, I create the migration like this:

docker-compose -f my.yml run --rm django python manage.py migrate

But, after rebooting the OS, I need to restart the migration, because they are not detected. What can be wrong?

Narnik Gamarnik
  • 1,049
  • 1
  • 16
  • 35
  • 1
    You do not mention where you keep your database, but those commands in the posted service file will remove all docker containers (`docker rm -v $(docker ps -aq)`) and all docker volumes currently on your machine (`docker volume rm $(docker volume ls -q)`). if your database is created within a started container or otherwise on an associated volume (and not in an actual image), it looks like you are clearing everything out each time you start or stop the service. – GracefulRestart May 30 '18 at 01:34

1 Answers1

0

After the experiments, I made here such a unit file, which works as it should.

[Unit]
Description=myservice
Requires=docker.service
After=docker.service

[Service]
Restart=always

# Compose down
ExecStartPre=/usr/local/bin/docker-compose -f my.yml down

# Compose up
ExecStart=/usr/local/bin/docker-compose -f my.yml up

# Compose down
ExecStop=/usr/local/bin/docker-compose -f my.yml down

[Install]
WantedBy=multi-user.target
Narnik Gamarnik
  • 1,049
  • 1
  • 16
  • 35