Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I lose data if docker restarts?

Tags:

docker

I am new to docker and would need some advice. I am about to set up my own personal git repository with Gitea. The docker compose file can be seen below. When I first run the file everything is fine. The problem occurs when I restart my computer or Docker then the page loads but it is empty no repos, nothing. Also it is not possible to log in with the credential that were set after first installation. It looks like the connection to the database is lost after restarting.

docker compose file:

version: "2"

networks:
  gitea:
    external: false

volumes:
  gitea:
    driver: local

services:
  server:
    image: gitea/gitea:latest
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - DB_TYPE=mysql
      - DB_HOST=db:3306
      - DB_NAME=gitea
      - DB_USER=gitea
      - DB_PASSWD=gitea
    restart: always
    networks:
      - gitea
    volumes:
      #- ./gitea:/data
       - gitea:/data
    ports:
       - "3000:3000"
       - "222:22"
    depends_on:
      - db

  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=gitea
      - MYSQL_USER=gitea
      - MYSQL_PASSWORD=gitea
      - MYSQL_DATABASE=gitea
    networks:
      - gitea
    volumes:
      - ./mysql:/var/lib/mysql
like image 788
jerry_k Avatar asked Jun 10 '19 06:06

jerry_k


1 Answers

You should declare one of your local folder as a volume in order to get persistent data. As in here for instance:

docker run -v /host/directory:/container/directory -other -options image_name command_to_run

Otherwise, any data written on top of an existing image layer is discarded by default when the container is removed.
This is using a union filesystem: (see "Digging into Docker layers")

https://cdn-images-1.medium.com/max/1091/1*st_fZmKOMykQGF8kZKglvA.png

In the case of gitea (docker), make sure you have a local data and mysql folder, to be mounted by the Docker images.

volumes:
 - ./data:/data
volumes:
 - ./mysql:/var/lib/mysql

You have used

volumes:
      #- ./gitea:/data
       - gitea:/data

That would declare a volume (instead of a bind mount), named "gitea", stored into your Docker installation.

You could try the same for the db part:

volumes:
 - mysql:/var/lib/mysql
like image 177
VonC Avatar answered Sep 20 '22 16:09

VonC