· Joseph · DevOps  · 3 min read

Using Firebase and Firestore with NextJS and Docker - Part 1 - Setup firebase in docker

Last year, I got a case to use firebase and firestore with Next.js. I've been fullstack many years, so I haven't tried to use firebase and firestore. There was a great chance to give it a try.

In this article I'll show how to use firebase and firestore in Docker and Next.js. If you don't have backend support, or you don't want to build whole backend, database, and infrastructure, you would probably think this is a useful way.

Last year, I got a case to use firebase and firestore with Next.js. I’ve been fullstack many years, so I haven’t tried to use firebase and firestore. There was a great chance to give it a try.

In this article I’ll show how to use firebase and firestore in Docker and Next.js. If you don’t have backend support, or you don’t want to build whole backend, database, and infrastructure, you would probably think this is a useful way.

TOC

Create a firebase project

Let’s start it. Go to firebase console and follow the step to create a new project. create project step 1

It is just for demo, so I uncheck google analytics in step 2.

A few minutes later, it shows you the firebase console. Next we have to add firebase to webapp by clicking the button. add firebase to webapp

We just need to focus on Step 1 and Step 4 in the following image, because we step 2 config you can get it from project setting again and step 3 we will use docker later. create firebase app

step 4 commands:

  • firebase login
  • firebase init
  • firebase deploy

After creating firebase app, we have to create firestore database for our app (we use test-mode and locale use asia-east1): create-firestore

Cool, but in this article we won’t show how to create a nextjs project and setup docker-compose, you can find it in my previous blog. Then, let’s add firebase docker to docker-compose.

Add firebase to docker-compose

Instead of npm install -g firebase-tools, we use docker-compose. The following code shows how to integrate firebase-tools to docker-compose.

See also: https://hub.docker.com/r/andreysenov/firebase-tools

version: '3.8'
services:
  frontend:
    container_name: frontend
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./app:/app
      - ./app/node_modules:/app/node_modules
    command: sh -c 'npm start'
    ports:
      - "3000:3000"
    stdin_open: true
  firebase:
    container_name: firebase
    image: andreysenov/firebase-tools
    user: node
    #command: firebase emulators:start
    command:·tail·-f·/dev/null
    ports:
      - 9005:9005
      - 9099:9099
      - 9199:9199
      - 5001:5001
      - 5000:5000
      - 8080:8080
      - 8085:8085
      - 9000:9000
      - 3003:3003
      - 4000:4000
    volumes:
      - ./:/home/node

Look at our file structure: project-structure

You can type docker-compose build to build docker container now. And don’t forget to install npm into app: docker-compose run --rm frontend npm install

Before we firebase login and firebase init, use command: tail -f /dev/null. Once we have firebase.json we can use command: firebase emulators:start.

Okay, after docker-compose up, try to use firebase-tool to login first:

docker-compose exec firebase firebase login docker-compose exec firebase firebase init

firebase-init

If you get this message: Error: It looks like you haven’t used Cloud Firestore in this project before. Please check your Default GCP resource location in Project Setting.

By the way, notice these two questions:

  1. ? What do you want to use as your public directory? app/build
  2. ? Configure as a single-page app (rewrite all urls to /index.html)? Yes

It’s almost done. We have to add host: 0.0.0.0 to each service of emulators in firebase.json:

    "firestore": {
      "host": "0.0.0.0",
      "port": 8080
    },
    "database": {
      "host": "0.0.0.0",
      "port": 9000
    },
    "hosting": {
      "host": "0.0.0.0",
      "port": 5000
    },
    "storage": {
      "host": "0.0.0.0",
      "port": 9199
    },
    "ui": {
      "enabled": true,
      "host": "0.0.0.0",
      "port": 4000
    },
    "singleProjectMode": true

Time to comment and uncomment our command part in docker-compose.yml, and restart docker-compose up. Yeah! After clicking http://localhost:4000/, we’ll see the result!

result

Okay! Firebase is ready for our project. Next artile we will demostrate how to use firebase with Next.js!

Back to Blog

Related Posts

View All Posts »

Install Docker / Docker-compose on CentOS 8

新的一篇文章來講講最近踩的雷,起因手邊有個案子開了一台CentOS 8機器給我,讓我在上面設定docker跟跑起服務。實在跟CentOS很不熟的我決定寫篇備忘錄。 一開始就先開台乾淨的CentOS VM來準備被我玩爛... install centos Install Docker 然後,讓我慢慢安裝docker。

Switching from vim to Neovim!

Switching from vim to Neovim!

I've been using Vim for a long time and finally switched to Neovim. The initial thought of switching came after the author of VIM passed away in August 2023, as I didn’t have the time to try other editors. After a year, “vibe coding” grew up, so I started thinking how to integrate AI into my editor and surveying how to use AI in Vim, which led to this journey. TOC Main differences Tab vs. Buffer: ref: https://www.reddit.com/r/neovim/comments/13iy0p0/why_some_people_use_buffers_for_tabs_and_not_vim/ In vim, I used vsp or tabnew to open or edit a file and mapped tabprevious command with Tab key to navigate between tabs. However, in Neovim, I used buffer instead of tabs and mapped BufferLineCyclePrev with Shift + h for switching buffers. coc vs. native lsp: I configured many coc settings to support TypeScript and JavaScript language servers, including linting and Prettier on save, go-to definition or reference, and codelens. After using Neovim, I converted all of these settings to nvim-lspconfig and mason, among others. Lua supports: Although I’m not familiar with Lua, it allows me to write more readable configuration files using modules, functions, and tables (objects) for Neovim. AI supports: Vibe coding! I found many plugins to integrate LLMs and finally selected three to use: avante.nvim Let your Neovim be a Cursor AI IDE – it's undergoing rapid iterations and many exciting features will be added successively; it updates almost every day! CodeCompanion CodeCompanion can customize workflows and prompts, and its Action Palette is a really useful tool. gp.nvim GP means GPT Prompt, and it's an Neovim AI plugin. It helps me write prompts through Neovim's cmdline. I usually switch between these plugins, and I'm still thinking about my 'vibe-way' to use them. Because of supporting Ollama LLM, all of them can be used offline. For now, I've used Neovim for three months, and got to know Neovim. In my experience, Neovim is really more productive than vim. Reference AI in Neovim How to set up Neovim for coding React ZazenCodes Neovim Rewrite by Avante + Ollama gemma3:4b ------ For a long time, I relied on Vim for my coding. However, after Bram Moolenaar’s passing in August 2023 – a significant influence in the Vim community – I decided it was time for a change. I began exploring alternative editors, ultimately settling on Neovim. This journey wasn’t just about switching editors; it was about integrating AI into my workflow and redefining my coding experience. Key Differences: Tabs vs. Buffers One of the first things I noticed was the shift from tabs to buffers in Neovim. In Vim, I frequently used vsp or tabnew to open or edit files, navigating between tabs with the Tab key and Tabprevious command. Neovim, however, utilizes buffers, offering a more streamlined approach. I configured BufferLineCyclePrev with Shift + h for seamless buffer switching, alongside nvim-tree/nvim-web-devicons and akinsho/bufferline.nvim. Leveraging Language Servers with coc and nvim-lspconfig I configured many coc settings to support TypeScript and JavaScript language servers, including linting and Prettier on save, go-to definition or reference, and codelens. Recognizing the power of language servers, I then converted all of these settings to nvim-lspconfig and mason.nvim, streamlining my development environment. Lua Configuration for Readability Although I’m relatively new to Lua, it allows me to write more readable configuration files using modules, functions, and tables (objects) for Neovim. Here’s a snippet of my configuration: AI-Powered Productivity with avante.nvim, CodeCompanion, and gp.nvim To truly elevate my coding experience, I integrated several AI plugins. I selected: avante.nvim**: This plugin transforms Neovim into a Cursor AI IDE, undergoing rapid iterations and adding exciting new features daily. CodeCompanion**: This plugin allows for customizable workflows and prompts, with a particularly useful Action Palette. gp.nvim**: (GPT Prompt) – This plugin helps me write prompts through Neovim’s command line interface, leveraging folke/noice.nvim. Because of supporting Ollama LLM, all of these plugins can be used offline. I’m still experimenting with how to best utilize these plugins – a “vibe-way” to coding! Resources for Further Exploration AI in Neovim How to set up Neovim for coding React ZazenCodes Neovim

AWS Cloud Development Kit (CDK) project structure

AWS Cloud Development Kit (CDK) project structure

Previously blog I used NodeJs/Typescript as a backend and deployed with AWS Cloud Development Kit (AWS CDK). The same framework, but more complex than the sample, is used on our Firstage. So this post I will show how we structure our AWS CDK project codebase. Project Structure project structure