Project Setup
Install create-react-app
1 | npm install -g [email protected] |
Creating a new app
1 | create-react-app docker-app |
Docker
Add a Dockerfile
to the project root
1 | # base image |
Add a .dockerignore
to speed up the Docker build process as our local dependencies will not be sent to the Docker Daemon
1 | node_modules |
Build and tag the Docker Image
1 | docker build -t docker-app:1.0.0 |
Then spin up the container once the image is built.
1 | docker run -it -v ${PWD}:/usr/src/app -v /usr/src/app/node_modules -p 3000:3000 --rm docker-app:1.0.0 |
Now you can visit your app http://localhost:3000
Docker Compose
Add a docker-compose.yml
to the project root.
1 | version: '3.3' |
Take note of the volumes. Without the data volume /usr/src/app/node_modules
, the node_modules
directory would be overwritten by the mounting of the host directory at runtime that were installed when the container was built.
BUild the image and fire up the container
1 | docker-compose up -d --build |
Ensure the app is running in the browser.
Bring down the container before moving on
1 | docker-compose stop |
Production
Let’s create a seperate Dockerfile
for use in production called Dockerfile-prod
1 | # build environment |
Using the production Dockerfile, build and tag the Docker Image:
1 | docker build -f Dockerfile-prod -t docker-app-prod . |
Spin up the container
1 | docker run -it -p 80:80 --rm docker-app-prod |
Add Prod Docker Compose as `docker-compose-prod.yml’
1 | version: "3.3" |
Fire up the container
1 | docker-compose -f docker-compose-prod.yml up -d --build |