DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Setting Apache Nifi on Docker Containers
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • The Importance of Persistent Storage in Kubernetes- OpenEBS

Trending

  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  • Creating a Web Project: Caching for Performance Optimization
  • Go 1.24+ Native FIPS Support for Easier Compliance
  • Advancing Your Software Engineering Career in 2025
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Enabling Docker Volume Local Directory for a Docker React Application

Enabling Docker Volume Local Directory for a Docker React Application

Docker Volume Local Directory setup helps make the development workflow a lot better for developers. We see how it can be achieved with a simple step.

By 
Saurabh Dashora user avatar
Saurabh Dashora
DZone Core CORE ·
Jul. 28, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, we will look at setting up a Docker volume local directory for a Docker container. In order to demonstrate the same, we will use the example of a React application running in our Docker container.

The advantage of enabling Docker volumes is that it allows us to share data between the host and the Docker container.

A typical example in the case of React application could be that when we update our code in the application, the changes automatically gets reflected in the running Docker container. Basically, this allows developers to test their incremental changes directly on a running container.

We already set up a Docker local development workflow for React earlier. Therefore, we will use it as a base for this post.

1. How Does Docker Volumes Work?

Docker volumes provide a way for us to map the file system inside the container to the file system of the host machine. Basically, by bookmarking volumes we can share files between our host and the running container.

Below illustration explains Docker volumes with the example of a React application that we created in a previous post.

Docker Container FlowAs you can see from the above, by bookmarking volumes, we can make our Docker containers use the files and folders on our host machine.

In the case of our React development work, this means that we can reflect our changes immediately on the running container. In other words, each time we make a code change, we don’t have to rebuild our Docker image.

2. Running Container With Docker Volume Local Directory

To run a container with Docker volume local directory mapping, we need to use the below command.

docker run -p 3000:3000 -v /app/node_modules -v $(pwd):/app ad98ac0c301e


Let’s understand the above command:

  • The -p flag maps the port in the container to the port on the host.
  • Next, the -v flag is used to bookmark the volumes.
  • Here, first we are mapping the /app/node_modules. Basically, we are telling the container to use the node_modules folder inside the container itself rather than on the host machine.
  • Next, we use -v ($pwd):/app to map the rest of the pwd or present working directory with the container. In other words, we are telling the container to check for the contents of the app folder in the present working directory of the host machine where our source code actually resides.
  • Lastly, we have the image id for the image that was built.

However, a point to be noted here is that the above command will not work on Windows or Mac. It will work on a Linux host machine.

3. Testing With Code Changes in React

In this step, we can test our setup to see if it works as expected. If you execute the above command, you should see the React application starting up. It will be accessible on http://localhost:3000.

React Application Started Up

To check our setup, we make a slight change in the React application as below. Basically, we change Learn React to You Should Learn React.

<a
  className="App-link"
  href="https://reactjs.org"
  target="_blank"
  rel="noopener noreferrer">
  You Should Learn React
</a>


As soon as we save the changes, an automatic compile will trigger just like it does when you run React application locally. The changes will reflect on the page.

Automatic Compile Triggered

The main difference, however, is that this time the refresh is happening directly in the Docker container. We didn’t need to rebuild the image.

Conclusion

With this, we have successfully demonstrated the use of Docker volume local directory mapping. We used the feature of bookmarking volumes to automatically reflect code changes in a React application on a running container.

This makes things easy for a developer as they can directly test their changes on a Docker container rather than in a local machine. Also, this setup removes the need for multiple builds of the Docker image.

If you have any comments or queries, please sound off in the comments section below.

Docker (software) application React (JavaScript library) Directory Host (Unix) File system

Published at DZone with permission of Saurabh Dashora. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Setting Apache Nifi on Docker Containers
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Hardware-Accelerated OpenGL Rendering in a Linux Container
  • The Importance of Persistent Storage in Kubernetes- OpenEBS

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!