This picture was taken by my Rolleiflex Automat II

A Very Basic Browser-Sync Development Environment Based on Docker

The purpose is to set up a very basic environment based on Docker for me to play HTML, CSS and Javascript like Codepen or plunkr. However, embedding images in these site is cumbersome.

The requirement is: 1. browser should automatically refresh when HTML, CSS and Javascript files change, 2. being able to use images in the html file, 3. I want to keep my own machine minimal , that is I don’t want install node.js and browser-sync locally.

The first step is to build a docker image with browser-sync install. The following Dockerfile should do.

The most important line is which installs browser-sync globally.

In the directory where the Dockerfile resides, using following command to create the image. Note the trailing dot which is necessary.

The option designates the tag of the newly created image. Now the image is ready.

To run the container, use the following command.

A complicated command, um…! Explanation is necessary for me, because I definitely forget some day.

tells docker to run this container interactively with a terminal.

tells docker to delete this container after the container is stopped.

binds a host directory to a directory in the container. The format is host-directory:directory-in-container. With this option, files in the host and in the container are actually the same copy. Changes of the files synchronize.

tells docker to map the port 3000 (the first 3000 before the column :) of the host to the port 3000 (the second 3000 after the column :) of the container. Browser-sync uses port 3000 to serve the site as default.

tells docker to map the port 3001 (the first 3001 before the column :) of the host to the port 3001 (the second 3001 after the column :) of the container. Browser-sync uses port 3001 to provide a UI to manage its options.

is the image tag that is just created.

is the command to be executed after the container is initiated.

tells browser-sync to start a static server for basic HTML/JS/CSS websites.

tells browser-sync to watch all files with extension of html, js, css, jpg; also watch the files in directories , , and with the same extension. In short, this option tells browser-sync to watch all static files in current working directory and sub-directories. is for escaping.

tells browser-sync not to open a browser because it is run in a environment without a browser.

Now a very simple server that serves static files runs, and I can see the following image. I can simple right click the link to open the website.

message emitted by browser-sync

For convenience, I store the command which runs the container in a shell script named start-browser-sync in my home directory. Make the script executable with the following command.

Now I can use this simple web server with one command. Make a directory with a with the following content.

Then to this directory, use the shell script with the command,

A simple static web server is now running, and it watches all changes I make.

Another way to run the command is to use alias. Just copy and paste following code. Don’t forget the escaping character . This annoys me for months.