Sample docker file for node project
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm","start"]Docker compose file sample
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- "4001:8081"
Connection a node server to a redis server within docker
const app = express();
const client = redis.createClient({
host: 'redis-server',
port: 6379
});Getting docker compose to build the container
docker-compose up --build
run in back ground mode
docker run -d redus
get running containers
docker ps
stop a running container
docker stop <container id>
Start compose in backround
docker-compose up -d
check containers running by running docker ps
dcoker ps
Stoping compose
docker-compose down
version: '3'
services:
redis-server:
restart: always
image: 'redis'
node-app:
restart: always
build: .
ports:
- "4001:8081"
0 = we exited and everything is OK
1,2,3 etc = we existed because something went wrong!
---development --> testing --> production --|
|----------------<<<------------------------|
Generqate empty react app for testing
npm install -g create-react-app
create-react-app frontend
Starts a development server for development purpose only
npm run start
Runs tests associated with the project
npm run test
Builds a production version of the application
npm run build
used only for development dockerfile.dev
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm","run","start"]Building the development docker file
docker build -f dockerfile.dev .
Running the container
docker run -p 3000:3000 -v /app/node_modules -v $(pwd):/app <image id>
note:
-v /app/node_modules
this means use the /app/node_modules contained within the container instaead of the app directory
this is a placeholder for a folder in the container
if you are running on Windows, please read this: Create-React-App has some issues detecting when files get changed on Windows based machines. To fix this, please do the following:
In the root project directory, create a file called .env
Add the following text to the file and save it: CHOKIDAR_USEPOLLING=true
That's all!
For more on why this is required, you can check out: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-start-doesn-t-detect-changes
Doing the same with docker compose
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "3000:3000"
volumes:
- /app/node_modules
- .:/app
docker run -it <imnage id> npm run test
version: '3'
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "3000:3000"
volumes:
- /app/node_modules
- .:/app
tests:
build:
context: .
dockerfile: Dockerfile.dev
volumes:
- /app/node_modules
- .:/app
command: ["npm", "run", "test"]