Search code examples
dockerstrapi

How do I retain my content types within a dockerized strapi


I've been using strapi for docker (https://github.com/strapi/strapi-docker), but whenever I rebuild my container the data all disappears. I can still see it in the database, but the admin isn't recognizing it.

I tried recreating the content type, and then the records from the database appeared, but when I rebuild the container again the content type disappears

Where are content definitions stored? Is this a bug with the app? (I think strapi-docker is using an alpha release)

How to I get strapi to retain my content definitions in the database, so I can use a stateless container?

UPDATE

I tried looking at the attached volume -

  api:
    build: .
    env_file: './dev.env'
    ports:
      - 1337:1337
    volumes:
      - ./strapi-app:/usr/src/api/strapi-app
      #- /usr/src/api/strapi-app/node_modules
    restart: always

But there's nothing in it -

Aidans-MacBook:strapi-docker aidan$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                    NAMES
02b098286ada        strapi-docker_api   "docker-entrypoint.s…"   24 minutes ago      Up 5 minutes (healthy)   0.0.0.0:1337->1337/tcp   strapi-docker_api_1
Aidans-MacBook:strapi-docker aidan$ docker inspect -f "{{.Mounts}}" 02b098286ada
[{bind  /Users/aidan/Documents/Code/beefbook/strapi-docker/strapi-app /usr/src/api/strapi-app  rw true rprivate}]
Aidans-MacBook:strapi-docker aidan$ ls /Users/aidan/Documents/Code/beefbook/strapi-docker/strapi-app
Aidans-MacBook:strapi-docker aidan$ 

Solution

  • you need to mount the directory to keep the file persistent.

    - ./strapi-app:/usr/src/api/strapi-app For application

    - ./db:/data/db For DB

    
    version: '3'
    
    services:
      api:
        build: .
        image: strapi/strapi
        environment:
          - APP_NAME=strapi-app
          - DATABASE_CLIENT=mongo
          - DATABASE_HOST=db
          - DATABASE_PORT=27017
          - DATABASE_NAME=strapi
          - DATABASE_USERNAME=
          - DATABASE_PASSWORD=
          - DATABASE_SSL=false
          - DATABASE_AUTHENTICATION_DATABASE=strapi
          - HOST=localhost
        ports:
          - 1337:1337
        volumes:
          - ./strapi-app:/usr/src/api/strapi-app
          #- /usr/src/api/strapi-app/node_modules
        depends_on:
          - db
        restart: always
    
      db:
        image: mongo
        environment:
          - MONGO_INITDB_DATABASE=strapi
        ports:
          - 27017:27017
        volumes:
          - ./db:/data/db
        restart: always
    
    

    Run the docker-compose up and you will see the data is now has been persistent.

    updated:

    After investigation by @Aidan

    it's used the APP_NAME env var (the default is "strapi-app"). so the correct mount is /usr/src/api/beef-content (since, in my case, the APP_NAME is beef-content). I'll use that to mount my volume