Enabling Live Reload in a Docker Container

When we use the term “live reload,” what we really mean is that we want to be able to change our source code, save, and see that change live without having to stop or restart a container.

To achieve this, there are a few things that need to happen:

Your updated code needs to be in the container. The easiest way to do this is using Docker volume mounting.

You need a tool to watch for code changes to restart your application if a file changes. Support for live reloading is dependent on the programming language and platform you are using. For Node, you can use NPM package nodemon, for Go, you can use the Gin utility https://github.com/codegangsta/gin to recompile when code changes, and for ASP.NET, at the time of this writing, you can use a tool named dnx-watch which watches for changes and restarts your application.

If you add a new library using a package manager like NPM for Node, or NuGet for .NET, you need to run a restore command to ensure the new library is added into your container.

Before you get started running the sample project, you need to start the Elasticsearch backend container. You can do this with the following docker run command that pulls the official elasticsearch image, runs it detached (in the background), and listens on port 9200.

docker run –d –p 9200:9200 elasticsearch

You’ll learn how to do this in a simpler way using linked containers and Docker Compose later in the chapter. With the data store now up, you can start the product catalog service by running:

docker run –d -t -p 80:80 –e "server.urls=http://*:80" –v
/c/Users/danielfe/Documents/DockerBook/ProductCatalog/Product
Catalog/src/ProductCatalog:/app thedanfernandez/productcatalog


Image Note

The above command is wrapped over several lines due to formatting in the printed book. You should type it as a single command on a single line.


This docker run command creates a detached container (-d) with tty (-t) support where port 80 of the VirtualBox VM will listen and forward a request to port 80 in the container. It uses the -e flag to set an environment variable to tell the ASP.NET web server to listen on port 80, mounts a volume with the -v flag, and bases the container on the publicly available thedanfernandez/productcatalog Docker image.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.211.239