Categories
Docker

How to add hot reloading with docker-compose?

Sometimes, we want to add hot reloading with docker-compose

In this article, we’ll look at how to add hot reloading with docker-compose

How to use docker-compose to build multiple services?

To use docker-compose to build multiple services, we can create a Dockerfile for each service.

Then we create a docker-compose.yml to build each service with the Dockerfile for each service.

For instance, if we have the frontend and backend service in the frontend and backend folder respectively, then we create a docker-compose.yml in the root folder with

version: "3"
services:
  frontend:
    build: ./frontend
    ports:
      - "8091:8091"
  backend:
    build: ./backend
    ports:
      - "8090:8090"
    volumes:
      - ./backend:/usr/src/app

We set build to the folder with the Dockerfile we want to run.

ports map the port from the Docker container to the port exposed to the outside.

volumes map the folder in the project to the folder in the Docker container.

Mapping the volumes with volumes lets us add hot reloading.

In the frontend folder, we add a Dockerfile by writing

FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

EXPOSE 8091
CMD [ "http-server", "dist", "-p 8091"]

to build the frontend container.

And in the backend folder, we add a Dockerfile by writing

FROM node:16

WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install
RUN npm i -g nodemon
COPY . .

EXPOSE 8090
CMD [ "nodemon", "-L", "server.js" ]

to build the backend container.

We use the -L option when running nodemon to let it watch for changes in mapped volumes.

And then we run docker-compose up to build both containers.

Conclusion

To use docker-compose to build multiple services, we can create a Dockerfile for each service.

Then we create a docker-compose.yml to build each service with the Dockerfile for each service.

Categories
Docker

How to specify the Dockerfile to use for each service with docker-compose?

Sometimes, we want to specify the Dockerfile to use for each service with docker-compose

In this article, we’ll look at how to specify the Dockerfile to use for each service with docker-compose

How to specify the Dockerfile to use for each service with docker-compose?

To use docker-compose to build multiple services, we can create a Dockerfile for each service.

Then we create a docker-compose.prod.yml to build each service with the Dockerfile for each service.

For instance, if we have the frontend and backend service in the frontend and backend folder respectively, then we create a docker-compose.yml in the root folder with

version: "3"
services:
  frontend:
    build: ./frontend
    ports:
      - "8091:8091"
  backend:
    build:
      context: ./backend
      dockerfile: ./Dockerfile.prod
    ports:
      - "8090:8090"
    volumes:
      - ./backend:/usr/src/app

We set build to the folder with the Dockerfile we want to run.

ports map the port from the Docker container to the port exposed to the outside.

volumes map the folder in the project to the folder in the Docker container.

We specify the Dockerfile to use in the ./backend folder with

build:
  context: ./backend
  dockerfile: ./Dockerfile.prod

In the frontend folder, we add a Dockerfile by writing

FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

EXPOSE 8091
CMD [ "http-server", "dist", "-p 8091"]

to build the frontend container.

And in the backend folder, we add a Dockerfile.prod file by writing

FROM node:16

WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install
RUN npm i -g forever
COPY . .

EXPOSE 8090
CMD [ "forever", "server.js" ]

to build the backend container.

And then we run docker-compose -f docker-compose.prod.yml up to build both containers.

Conclusion

To use docker-compose to build multiple services, we can create a Dockerfile for each service.

Then we create a docker-compose.yml to build each service with the Dockerfile for each service.

Categories
Docker

How to use docker-compose to build multiple services?

Sometimes, we want to use docker-compose to build multiple services.

In this article, we’ll look at how to use docker-compose to build multiple services.

How to use docker-compose to build multiple services?

To use docker-compose to build multiple services, we can create a Dockerfile for each service.

Then we create a docker-compose.yml to build each service with the Dockerfile for each service.

For instance, if we have the frontend and backend service in the frontend and backend folder respectively, then we create a docker-compose.yml in the root folder with

version: "3"
services:
  frontend:
    build: ./frontend
    ports:
      - "8091:8091"
  backend:
    build: ./backend
    ports:
      - "8090:8090"
    volumes:
      - ./backend:/usr/src/app

We set build to the folder with the Dockerfile we want to run.

ports map the port from the Docker container to the port exposed to the outside.

volumes map the folder in the project to the folder in the Docker container.

In the frontend folder, we add a Dockerfile by writing

FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

EXPOSE 8091
CMD [ "http-server", "dist", "-p 8091"]

to build the frontend container.

And in the backend folder, we add a Dockerfile by writing

FROM node:16

WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install
RUN npm i -g nodemon
COPY . .

EXPOSE 8090
CMD [ "nodemon", "-L", "server.js" ]

to build the backend container.

And then we run docker-compose up to build both containers.

Conclusion

To use docker-compose to build multiple services, we can create a Dockerfile for each service.

Then we create a docker-compose.yml to build each service with the Dockerfile for each service.

Categories
Python Answers

How to convert datetime.timedelta to minutes, hours in Python?

Sometimes, we want to convert datetime.timedelta to minutes, hours in Python.

In this article, we’ll look at how to convert datetime.timedelta to minutes, hours in Python.

How to convert datetime.timedelta to minutes, hours in Python?

To convert datetime.timedelta to minutes, hours in Python, we can use the timedelta’s total_seconds method to get the datetime difference in seconds.

For instance, we write

seconds = duration.total_seconds()
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60

to call total_seconds on the duration timedelta object.

And then we get the hours, minutes, and seconds by calculating the values from the seconds.

Conclusion

To convert datetime.timedelta to minutes, hours in Python, we can use the timedelta’s total_seconds method to get the datetime difference in seconds.

Categories
Python Answers

How to count frequency of values in Python Pandas DataFrame column?

Sometimes, we want to count frequency of values in Python Pandas DataFrame column.

In this article, we’ll look at how to count frequency of values in Python Pandas DataFrame column.

How to count frequency of values in Python Pandas DataFrame column?

To count frequency of values in Python Pandas DataFrame column., we can use the value_counts method.

For instance, we write

counts = df['status'].value_counts().to_dict()
print(counts)

to call value_counts on the df data frame’s status column to count all the items in the status column.

And then we call to_dict to return the item as the keys and the count of each item in status as the values.

Conclusion

To count frequency of values in Python Pandas DataFrame column., we can use the value_counts method.