Docker redis nodejs

Hề lô không lô, lại là mình đây ^^. ngày thứ 6 lỡ tắt máy ở công ty nên về nhà không ssh vào để lấy mã tiếp. D. Tình hình là cái one man army project mình nhắc tới ở bài trước đã chạy được khoảng tầm hơn một tháng rồi. Mà làm gì chỉ có một mình mình làm mà thời gian lại có hơi gấp nên những lúc sau giờ làm mình thường hay mở mã nguồn ra để gõ tiếp vài dòng. D. Thứ 7 chủ nhật này xui xẻo sao tắt máy ở công ty nên coi như nghỉ giải lao vậy. D Tiện ích có thể viết luôn bài blog trong lúc rảnh rỗi

À thường thì sau giờ làm mình sẽ đi đánh cầu lông với mấy anh chị trong công ty chủ nhật thì hay đi đá bóng với lũ bạn hồi cấp ba chứ cuộc sống không chỉ xoay quanh công việc gõ code suốt ngày đâu. D

Được rồi, quay lại chủ đề chính của bài blog này. Dự án hiện tại mình đang làm được áp dụng khá nhiều công nghệ cũ-người-mới-ta đối với mình. Mình cũng kha khá thời gian để bắt đầu nó, trong bài này mình sẽ nói những điểm ưu việt của công việc gom tất tần tật các dịch vụ vào container của docker và làm sao để làm được việc đó. Còn nhược điểm là gì thì mình chưa gặp cũng như chưa tìm hiểu nên sẽ không nhắc tới trong này, hi vọng mọi người có thể comment cho mình biết nhược điểm của nó với ^^

À, dạo này bác quá nên mọi người chủ động tìm hiểu khái niệm docker, container các kiểu là gì nha. D Mình sẽ để link tại đây về Docker ^^

I. Tại sao Docker?

Mình xin trích dẫn một đoạn trong document của docker

Developing apps today requires so much more than writing code.
Multiple languages, frameworks, architectures, and discontinuous
interfaces between tools for each lifecycle stage creates enormous
complexity. Docker simplifies and accelerates your workflow, while
giving developers the freedom to innovate with their choice of tools,
application stacks, and deployment environments for each project.

Việc đang phát triển bây giờ khác ngày xưa nhiều lắm, từ việc website ban đầu chỉ là cục bộ, tiến lên server chạy (người ta hay gọi là Monolithic Architecture ý), khi nào đó khi giao tiếp với một trang web thì chúng ta sẽ gửi một . Việc này khiến cho thời gian chờ đợi để tải cả một trang web lên rất lâu, rồi máy chủ cũng phải đảm nhiệm cả công việc render html lên nữa,… Sau này khi người ta thấy quá nhiều nhược điểm cũng như khả năng mở rộng quy mô của nó . Từ đó, Microservice Architecture ra đời để giải quyết các vấn đề trên, mỗi dịch vụ sẽ làm đúng và duy nhất nhiệm vụ của mình, đảm bảo khả năng mở rộng quy mô dễ dàng hơn

Ơ lại lan man rồi =)) Quay lại vấn đề chính nào

Trong quá trình phát triển, chúng ta thường sẽ hay gặp sự cố “It works on my machine” đúng hơm. D Hai người cùng cài đặt một dịch vụ nhưng lỡ có phiên bản khác nên ứng dụng bị treo ngay. Docker & container sinh ra để giải quyết vấn đề này. Docker cung cấp các container giúp các nhà phát triển chỉ cần cài đặt một lần duy nhất và sẽ chạy trên tất cả các môi trường khác nhau, miễn phí là có thể cài đặt được Docker

Nghe giống hệt slogan của Java đúng không =))) “Viết một lần chạy khắp nơi”. Docker cũng vậy, điều tuyệt vời của Docker là giúp thành viên mới khi tham gia vào dự án đỡ tốn thời gian thiết lập hơn, việc triển khai cũng dễ dàng hơn nữa chú. Hồi xưa mình làm dự án về Ruby, mỗi lần cài lại máy là xem như mất luôn một ngày chỉ để ngồi cài đặt từng module để có thể code được =)))

II. Bắt đầu một dự án với Docker được xây dựng trên Nestjs, Redis, Postgresql

1. Chuẩn bị một Dockerfile

Được rồi, để triển khai Docker vào dự án, chúng ta cần khai báo tên tệp là

# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

3. Docker will based on this command in this build to an image. Hình ảnh này sẽ được sử dụng để chạy bên trong contaier của chúng tôi

Nội dung của

# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

3 cũng không có gì đặc biệt, đây là file
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

3 của mình

# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

Mọi người có thế dựa vào Nodejs Docker App hoặc Dockerfile tham khảo để tự build

# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

3 của mình, à ngoài ra cũng nên follow theo Nodejs Docker best pratice nữa nhé ;)

Được rồi, chúng ta đã có một container bao gồm Nodejs và Postgreql để sử dụng rồi, việc tiếp theo là định nghĩa

# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

7 để có thể chạy multi-container

2. Viết docker-compose. yml

Với chức năng soạn thư, chúng ta có thể định nghĩa và chạy nhiều vùng chứa và kết nối chúng lại với nhau. Vì mỗi vùng chứa sẽ sử dụng một hình ảnh riêng biệt, và vấn đề mà chúng ta cần giải quyết là làm sao để các vùng chứa có thể giao tiếp được với nhau. Lúc này thì docker-compose sẽ giúp mình làm việc đó, nó sẽ tự động tạo ra một mạng, nơi mà tất cả các container được xác định trong này có thể nói chuyện với nhau y như thể nó được cài đặt trên cùng một cái . ))

Khó hiểu đúng không? . Công ty cấp cho mình một cái PC có cài đặt Nodejs, thế nhưng do ổ cứng hết dung lượng nên không thể cài đặt thêm Postgresql và Redis được nên phải đi mượn ông Một cái laptop đề cài đặt lên. Vấn đề xảy ra lúc này là ứng dụng chạy trên nền tảng Nodejs ở máy công ty không thể kết nối tới Postgresql trên laptop của anh A để thao tác xóa dữ liệu đã chỉnh sửa được, thì docker-compose sẽ là cái được cấu hình sẵn cho phép . Việc mình cần làm là định nghĩa nhưng dịch vụ nào được phép nói chuyện với nhau cho thằng docker-compose biết. Ok hỉu hơm hỉu hơm ^^

Được rồi he, đây là file

# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

8 mà mình đã dùng để định nghĩa những cái service trên

version: '3'

volumes:
  postgres_data_local: {}
  postgres_backup_local: {}

services:
  db:
    image: postgres:latest
    container_name: db_local
    restart: always
    ports:
      - '5432:5432'
    env_file:
      - ./.env
    volumes:
      - postgres_data_local:/var/lib/postgresql/data
      - postgres_backup_local:/backups

  api:
    build: .
    image: api_local
    container_name: api_local
    restart: always
    depends_on:
      - db
      - redis
    links:
      - db
      - redis
    volumes:
      - ./:/home/node/app
    env_file:
      - ./.env
    ports:
      - '3000:3000'
    command: './start.sh'

  redis:
    restart: always
    image: redis
    container_name: redis_local
    ports:
      - '6379:6379'
    env_file:
      - ./.env
    command: 'redis-server --requirepass ${REDIS_PASSWORD}'

Giải thích một chút, tập tin

# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

9 này sẽ bao gồm ba dịch vụ. Api, Db và redis. Ngoài ra mình có định nghĩa một cái lable là
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

0, lable này dùng trong việc sao lưu dữ liệu, để mỗi lần mình dừng container sau đó start lại thì dữ liệu của mình vẫn còn nguyên ở đấy. D Ngoài ra, api của mình còn phải phụ_on các cậu
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

1 và
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

2 nữa. Mục đích của công việc này là buộc anh chàng
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

3 phải phụ thuộc vào
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

2 và
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

1, tức là khi
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

6 thì anh chàng
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

1 và anh chàng
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

2 sẽ phải khởi động trước anh chàng
# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

3. Các bạn có thể đọc thêm về

Các lệnh để sử dụng trong

# Init database for the first time.
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:14.11.0

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app
WORKDIR /home/node/app

# Install app dependencies & setup.
COPY --chown=node package.json /home/node/app
RUN yarn install

# Bundle app source code
COPY --chown=node . .

7 các bạn có thể tìm đọc thêm ở phần docker-compose docs này

IV. end

Phần cài đặt Docker & sử dụng Docker-compose mình xin kết thúc tại đây, trên đây chỉ là những cái khái niệm cơ bản khi sử dụng docker mà mình tìm hiểu được, kiến ​​thức còn hạn chế, mong mọi người có thể góp ý

Anw, nếu có thắc mắc gì thì cứ inbox Facebook mình hoặc comment ở dưới nha ^^ Mình sẽ giải đáp cho các bạn. D