Digital Graveyards, Firebase tools & Docker

Luis G. Valle
4 min readMar 31, 2018

We, developers, are constantly trying out new stuff. Testing the latest framework, tool or library. Playing with the new shinny toy for a weekend and quickly forgetting it when Monday brings us back to The Reality™.

If you’re not careful your computer quickly becomes an expensive digital graveyard. You can, of course, spring clean your hard drive removing working in progress projects you’re never going to finish. If you’re more disciplined than me, you’ll probably have a./workspace/wip directory to group all of them.

The problem is not the projects though. It’s the environment. The hundreds of little dependencies and libraries those projects need. That weekend you spent trying to build your own blog with Ghost is only a memory now, but Homebrew, Node, Grunt, Ghost and God-knows-what are still installed in your computer.

This year I got a new laptop and my goal is to keep it a clean as possible from zombie environments.

Docker to the rescue

I’m an Android developer so my main environment dependencies are the Android and Java SDKs. Those are installed natively in my computer and that’s fine. For all the other non essential stuff, I want Docker to help me keeping my laptop dependencies free.

I’m playing a lot recently with Firebase and Firebase Cloud Functions. In this post I’ll share how to create a docker image for Firebase Tools with your user logged in.

Spinning up a new Firebase Tools environment should be as easy as one simple command to run and my computer should be isolated from all the needed dependencies.

You need Docker installed for this tutorial: https://docs.docker.com/install

And if you want to learn more about Docker I highly recommend the Docker Technologies for DevOps and Developers course.

1. Build from a Dockerfile and name the image

We’ll start by building on top of the basic node:alpine image. Create a Dockerfile file with the content below

It’s important to name the resulting image, as the name will be used later.
In this case I’ll be naming it lgvalle/firebase-tools-docker

FROM node:9.10-alpineEXPOSE 9005RUN npm install -g firebase-tools

Build the Dockerfile and name the image

> docker build -t lgvalle/firebase-tools-docker .

It will create an image like this

> docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
lgvalle/firebase-tools-docker latest dc8673fe6ec9 18 minutes ago 152MB

2. Bash into the container and login with Firebase

docker run -p 9005:9005 -u node -it lgvalle/firebase-tools-docker sh

This command executes sh(effectively opening a sh console) in the image lgvalle/firebase-tools-docker , but also:

  • Maps the host’s port 9005 to the corresponding container port. This is important for Firebase login as the oauth callback uses this port
  • Sets the username as node— we want to avoid running stuff as root if possible

If all goes well a terminal within the container should be opened.

Now we need to login from the container

/ $ firebase login
? Allow Firebase to collect anonymous CLI usage and error reporting information? Yes
Visit this URL on any device to log in:
https://accounts.google.com/o/oauth2/auth?client_id=xxxxxo849e6.apps.googleusercontent.com&scope=email%20openid%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloudplatformprojects.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Ffirebase%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&response_type=code&state=176507547&redirect_uri=http%3A%2F%2Flocalhost%3A9005
Waiting for authentication…

Copy & paste that url into a browser. A Google auth window should appear.
Complete the login process and go back to the terminal. A message like this will be printed if everything goes well:

Waiting for authentication…✔ Success! Logged in as luis@novoda.com

3. Commit container changes as new image

Every time you spin an image, docker creates a new container based in that image to execute the work. Any state changes inside the container are lost when you exit if you don’t commit them *

* That is not technically correct, as you can always re-run an old container. In this case, what we want is to have a docker image with our user already logged in; and for that, we need to commit the changes occurred during the login process

Exit the container typing exit, find the container id with docker ps -l and commit the changes:

docker ps -l 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f1ac27f6fa4e lgvalle/firebase-tools-docker “sh” 42 minutes ago Exited (0) 33 minutes ago youthful_saha
docker commit —message “Add firebase login” f1ac27f6fa4e lgvalle/firebase-tools-docker

This will replace our original lgvalle/firebase-tools-docker image with the content of the container f1ac27f6fa4e

4. Verify the new image has valid login details

Bash into the container and list your firebase projects

luis@mbp > docker run -p 9005:9005 -u node -it lgvalle/firebase-tools-docker sh/ $ firebase list
┌──────────────────────┬─────────────────────────────┬─────────────┐
│ Name │ Project ID / Instance │ Permissions │
├──────────────────────┼─────────────────────────────┼─────────────┤
│ bonfire │ bonfire-b4111 │ Editor │
├──────────────────────┼─────────────────────────────┼─────────────┤
│ Firebase Demo Project │ fir-demo-project-ad555 │ Viewer │
├──────────────────────┼─────────────────────────────┼─────────────┤
│ FirebaseNotificationsDemo │ fir-notificationsdemo-66111 │ Owner │
├──────────────────────┼─────────────────────────────┼─────────────┤
│ MyAwesomeProject │ myawesomeproject-771111 │ Owner │
└──────────────────────┴─────────────────────────────┴─────────────┘

If a list of projects like this is shown it means everything worked well! ✔

5. Bonus: launch firebase tools environment for the current directory

Create an alias like this to automatically open a firebase tools console for the current directory:

alias fire='docker run -v $PWD:/usr/app -w /usr/app -p 9005:9005 -u node -it lgvalle/firebase-tools-docker sh' 
  • -v $PWD:/usr/app : maps current directory to the path /usr/app inside the container
  • -w /usr/app: sets the container working dir to /usr/app

Credit to the alias idea to this gist https://gist.github.com/lgvalle/f4d3934c2aeab64acfcff7658b42d355#file-extra-info-md

Do you have any other useful Docker or Firebase tips like this? I’d love to hear them! Feel free to ping on me Twitter @lgvalle

--

--