Every day localhost doesn't work

I’m new to Docker Desktop (or Docker whatsoever), the thing is that everyday my URLs don’t start my locally installed Directus. I try 0.0.0.0:8055 / 0.0.0.0:8080 / 127.0.0.1:8055 but it fails, unless I open Docker Desktop and hit play on the Directus script. Is this normal? I mean, the script needs to run all the time? I also have the issue that Docker Desktop not always opens up, and I have to end it in Task Manager first.

I’d like some enlightment on this matter. Thanks

1 Like

You are pretty close.

What’s about all those folders: Docker container usually host a single service inside a virtual machine. Data stored by this services would be lost whenever the container is restarted, so you have to map those folders to a “real” folder on your physical machine (or a folder inside a docker volume, more on this later). For the most basic setup that’s, given by your example:

MySQL service:

  • ./mysql_data:/var/lib/mysql => persists your actual database, and more stuff you don’t have to care about

Directus service:

  • ./uploads:/directus/uploads => persists all media you upload to directus
  • ./extensions:/directus/extensions => persists all extensions installed in directus

The first part before the colon is the path on your physical file system, in our case relative to the location of docker-compose.yml file denoted by ./: ./mysql_data
the second part is the folder inside the virtual machine you need to persist: /var/lib/mysql

For simplicity and easy learning I would ignore docker volumes and it’s docker compose notation (they are kind of virtual harddisks for your containers) for now.

For even more simplicity, i removed the Redis cache from the services which you can add later if needed for scalability and performance.

So the most basic Directus+MySQL compose stack I can come up with is:

services:
  directus:
    image: directus/directus:11.12.0
    container_name: directus
    ports:
      - "8055:8055"
    environment:
      ADMIN_EMAIL: "admin@example.com" # 👈 Change to your admin email
      ADMIN_PASSWORD: "password" # 👈 Change to a secure password

      DB_CLIENT: "mysql"
      DB_HOST: "mysql"
      DB_PORT: 3306
      DB_DATABASE: "directus"
      DB_USER: "directus"
      DB_PASSWORD: "directus"

      SECRET: "your-random-secret" # 👈 Change for production

    depends_on:
      - mysql
    restart: unless-stopped
    volumes:
      - ./directus_data/uploads:/directus/uploads
      - ./directus_data/extensions:/directus/extensions

  mysql:
    image: mysql:8.0
    container_name: directus_mysql
    environment:
      MYSQL_ROOT_PASSWORD: "rootpassword" # 👈 Change this
      MYSQL_DATABASE: "directus"
      MYSQL_USER: "directus"
      MYSQL_PASSWORD: "directus"
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./mysql_data:/var/lib/mysql
    restart: unless-stopped

Since now all persisted folders are mapped to actual folders inside the compose folder, you can easy copy the compose folder over to another machine. So to pass the work on to your colleague, you simply send him the compose folder with all it’s content and all he got to do is run docker compose up -d inside of it.

Thank you Nik, not only you have provided me with the right code for a Directus+MySQL setup, but you also made me understood the logic about Directus folders and how to use Docker.

This community looks promising, I hope you will have patience for my upcoming wannabe-questions

:star_struck:

Criss

You most likely didn’t set the restart policy on your docker container when it doesn’t start after power cycling.

Yeah, that’s most likely because Docker Desktop doesn’t close it’s frontend when the user closes it, but rather minimizes itself into the taskbar tray. Starting the app again does in that case nothing and it seems like you have to close it via task manager, although it’s already running. Watch out for that docker desktop icon in the taskbar and double click it to reopen:

image

I have opened the terminal inside Docker and have set docker “docker run -d --restart unless-stopped redis” but I still had to press play when opening DockerDesktop (now even on one more script “redis”), otherwise localhost wouldn’t open Directus

That’s just affecting the redis container. You have to make sure that the directus and and db (postgres/mysql/…) container are also started with an restart policy like “always” or “unless-stopped”.

It might tho be easier to orchestrate and also share your config if you move to docker-compose deployment. That way you don’t have to deal with individual container on the command line but rather set the whole stack config in one file.

@Nik, don’t go too much into technical details because I’m not a developer. :=) I’m using the tools and trying to copy/paste solutions without really knowing about alternative solutions. I’m preparing some basic Directus locally, then I’ll forward it to a dev. That said, I have tried just the command “docker run -d --restart always” but it didn’t like it: “requires at least 1 argument” (I thought that “always” was enough for an argument)

I’ll try my best:

  1. Shut down all directus related docker containers you might have currently running via docker desktop (directus, redis, postgres, mysql…)
  2. Create a folder somewhere on you machine
  3. Inside that folder create a docker-compose.yml file
  4. Paste following content into that docker-compose.yml
version: "3.8"

services:
  directus:
    image: directus/directus:11.12.0
    container_name: directus
    ports:
      - "8055:8055" # Change if you want a different host port
    environment:
      # Must change these for first admin login
      ADMIN_EMAIL: "admin@example.com" # 👈 Change to your admin email
      ADMIN_PASSWORD: "password" # 👈 Change to a secure password

      # Database connection — match postgres settings below
      DB_CLIENT: "pg"
      DB_HOST: "postgres"
      DB_PORT: 5432
      DB_DATABASE: "directus"
      DB_USER: "directus"
      DB_PASSWORD: "directus"

      # Redis cache
      REDIS_ENABLED: "true"
      REDIS_HOST: "redis"

      # Security keys — recommended to change for production
      SECRET: "your-random-secret"

    depends_on:
      - postgres
      - redis
    restart: unless-stopped
    volumes:
      - ./directus_data/uploads:/directus/uploads
      - ./directus_data/extensions:/directus/extensions

  postgres:
    image: postgres:16
    container_name: directus_postgres
    environment:
      POSTGRES_DB: "directus"
      POSTGRES_USER: "directus"
      POSTGRES_PASSWORD: "directus"
    volumes:
      - ./db_data:/var/lib/postgresql/data
    restart: unless-stopped

  redis:
    image: redis:7
    container_name: directus_redis
    volumes:
      - ./redis_data:/data
    restart: unless-stopped

volumes:
  db_data:
  redis_data:
  1. Change ADMIN_EMAIL and ADMIN_PASSWORD values to your needs and save the file.
  2. Open a terminal inside that created folder
  3. Run docker compose up -d
  4. Wait for the containers to spin up, then navigate your browser to http://localhost:8055

thanks but that’s exactly how I installed Directus on my PC. I have created a folder with subfolders (database, extensions, uploads) and a .yml file. My code is much simpler:

services:
directus:
image: directus/directus:11.12.0
ports:

  • 8080:8055
    volumes:
  • ./database:/directus/database
  • ./uploads:/directus/uploads
  • ./extensions:/directus/extensions
    environment:
    KEY: “replace-with-random-value”
    SECRET: “replace-with-random-value”
    ADMIN_EMAIL: “admin@example.com”
    ADMIN_PASSWORD: “d1r3ctu5”
    DB_CLIENT: “sqlite3”
    DB_FILENAME: “/directus/database/data.db”
    WEBSOCKETS_ENABLED: “true”

….then I open Docker Desktop, I run the script, and open Directus in the browser. Should I rewrite the .yml file with the full code you suggest?

It depends what DB you wanna use and which additional settings you want or have to set.

My example is using a simply barebone Directus/Postgres/Redis with container auto restart set and I can confirm it working.
So yes, if it’s just for testing purpose use my setup, so that we can easier debug if something is not working as intended.

I have no database on my PC, I’m just building Directus on my desktop so I don’t know how it’s registering data in that folder. For the website I will use MySQL but, again, I’m just going to pass all my folders to the developer.

Pasting your code makes no difference: unless I start DockerDesktop and run the script, my URL doesn’t open up Directus.

Or maybe I’m just misunderstanding everything?

When you run docker compose up -d inside the terminal you should see an output like this:

Snag_ffc1b68

If your output is different, lemme know.

all right, probably something was not installed properly, as I run your command and it started to install the databases (pulled), ending up with that output, plus it added some folders.

However, though I still need to open DirectusDesktop (not a big deal) in order to enable the URL, this is now showing a brand new Directus installation. Have I lost the customization I did in my first installation? (saved as the .yml copy)

Yes, because directus is now running against a different database (postgres). If you already customized Directus you would need to run against the sqlite database you had setup before again.

Just replace

     # Database connection — match postgres settings below
      DB_CLIENT: "pg"
      DB_HOST: "postgres"
      DB_PORT: 5432
      DB_DATABASE: "directus"
      DB_USER: "directus"
      DB_PASSWORD: "directus"

with

And make sure the path to the database /directus/database/data.db is still in correct.

I need to understand what is going on. My old code was a simple one but didn’t create any folders, whereas your code has created many folders/files. By “reading” it it sounds to me that it includes several database options (postgres, redis). If I change sqlite3 the browser still shows the brand new Directus installation.

Is there just a code that is MySQL ready?

(sorry for dumb questions)

UPDATE: I found this, Let me see if it works. It doesn’t matter I lost the previous data, it was just some settings….

version: ‘3’
services:
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: ‘directus’
MYSQL_USER: ‘directus’
MYSQL_PASSWORD: ‘directus’
MYSQL_ROOT_PASSWORD: ‘root’
ports:

  • “3306:3306”
    volumes:
  • mysql_data:/var/lib/mysql

cache:
image: redis:6

directus:
image: directus/directus:latest
ports:

  • “8055:8055”
    depends_on:
  • mysql
  • cache
    environment:
    DIRECTUS_APP_ENV: ‘production’
    DIRECTUS_DATABASE_HOST: ‘mysql’
    DIRECTUS_DATABASE_PORT: ‘3306’
    DIRECTUS_DATABASE_NAME: ‘directus’
    DIRECTUS_DATABASE_USERNAME: ‘directus’
    DIRECTUS_DATABASE_PASSWORD: ‘directus’
    DIRECTUS_CACHE_ENABLED: ‘true’
    DIRECTUS_CACHE_STORE: ‘redis’
    DIRECTUS_CACHE_REDIS: ‘redis://cache:6379’
    DIRECTUS_ADMIN_EMAIL: ‘admin@example.com’
    DIRECTUS_ADMIN_PASSWORD: ‘d1r3ctu5’
    volumes:
  • ./uploads:/directus/uploads

volumes:
mysql_data: