There are two options how to run application. Develop and deploy mode.
Both modes are using same docker volume (same database is loaded).
.env.example .env 로 복사하고 바꿉니다client/.env.development.example 에 client/.env.development 개발 모드에서 앱을 실행하려는 경우client/.env.production.example client/.env.production 으로 복사 및 바꾸기 모드 에서 앱을 실행하려는 경우.${REACT_APP_GOOGLE_API} 에 대한 값을 얻으려면 다음 단계를 따르십시오 This will just run db and pgadmin containers. React and server instances are running separately, outside of containers.
.env 에서 : localhost 로 ${DB_HOST} 의 값을 변경하십시오docker-compose -f docker-compose.dev.yml up -dnpm i && npm run server${REACT_APP_BASE_URL} 의 포트를 client/.env.development 파일에 따라 ${SERVER_PORT} 에 따라 조정하는 것을 잊지 마십시오 .env 파일에 지정되었습니다.cd client && npm i && cd .. && npm run clientlocalhost:3000 This will run db, pgadmin, server and build react with disabled redux devtools.
.env 에서 postgres 로 ${DB_HOST} 의 값을 변경하십시오.docker-compose up -d .localhost:${SERVER_PORT} .올바른 응용 프로그램 기능을 위해서는 먼저 데이터베이스를 채워야합니다. 우리는 명령 줄을 사용하여이를 달성 할 것입니다.
루트 디렉토리에서 내보낸 데이터가있는 디렉토리로 이동하십시오.
cd dummy 그런 다음 데이터를 실행중인 Postgres 컨테이너에 복사하십시오 (개발 모드를 위해 dev_postgres_container 사용하십시오).
docker cp dbexport.sql postgres_container:/
그런 다음 컨테이너를 입력하십시오 (개발 모드의 경우 dev_postgres_container 사용하십시오).
docker exec -it postgres_container /bin/bash
이미 인구가 많은 데이터베이스로 데이터를 가져 오려면 먼저 기존 테이블을 제거한 다음 새 테이블을 만들어야합니다. 컨테이너 콘솔에서 기존 데이터베이스를 떨어 뜨립니다. (아직 데이터베이스를 작성하지 않은 경우이 단계를 건너 뛰십시오)
dropdb -U ${DB_USER} ${DB_NAME}Postgres 서버에 사용자로 로그인하십시오.
su - ${DB_USER}그런 다음 새 데이터베이스를 작성하십시오.
createdb ${DB_NAME}Postgres 서버 콘솔 종료.
exit새 데이터로 데이터베이스를 채우십시오.
psql -d ${DB_NAME} -U ${DB_USER} -f dbexport.sql 이제 응용 프로그램에 로그인 할 수 있습니다. 기본 name 과 password 는 모두 : admin 입니다.
localhost:${DB_PORT}${DB_USER}${DB_PASSWORD}http://localhost:${PGADMIN_PORT}${PGADMIN_EMAIL}${PGADMIN_PASSWORD}postgres${DB_PORT}${DB_NAME}${DB_PASSWORD} Docker 파일은 프로젝트의 루트 레벨에 있으며 배포 모드에만 사용됩니다.
# Dockerfile
FROM node:latest
WORKDIR /server
COPY . ./
RUN npm install
RUN npm install --prefix client
CMD npm run build 처음에는 최신 노드가 다운로드되고 /server 작업 디렉토리가 새로 생성 된 컨테이너로 작성됩니다. 이 작업 디렉토리에서 전체 소스 코드가 복사되며 client/.dockerignore 에 지정된 파일에 대해 기대합니다. 이 파일은 생략됩니다. 그런 다음 노드 종속성은 서버 및 클라이언트 인스턴스 모두에 대한 작업 디렉토리에 설치됩니다. 마지막으로, npm run build 명령은 package.json 에 위치합니다. 이 명령은 먼저 React Application을 빌드 한 다음 서버를 트리거합니다. 서버 가이 방식으로 시작되면 환경 변수 NODE_ENV production 으로 설정되고 빌드 된 버전의 React 응용 프로그램이 사용됩니다.
우리는 두 개의 docker-compose 파일을 사용하고 있습니다. docker-compose.dev.yml 개발 모드 및 docker-compose.yml 루트 디렉토리에 위치한 생산 모드 용입니다. 각 파일은 .env 파일에 지정된 환경 변수를로드합니다.
# docker-compose.dev.yml
version : " 3.5 "
# inspired by https://github.com/khezen/compose-postgres
services :
postgres :
container_name : postgres_container
image : postgres:11
environment :
POSTGRES_USER : ${DB_USER}
POSTGRES_PASSWORD : ${DB_PASSWORD}
PGDATA : /data/postgres
volumes :
- postgres:/data/postgres
ports :
- " ${DB_PORT}:5432 "
networks :
- postgres
restart : always
pgadmin :
container_name : pgadmin_container
image : dpage/pgadmin4
environment :
PGADMIN_DEFAULT_EMAIL : ${PGADMIN_EMAIL}
PGADMIN_DEFAULT_PASSWORD : ${PGADMIN_PASSWORD}
PGADMIN_CONFIG_SERVER_MODE : " False "
volumes :
- pgadmin:/root/.pgadmin
ports :
- " ${PGADMIN_PORT}:80 "
networks :
- postgres
restart : always
server :
container_name : server_container
build :
context : .
dockerfile : Dockerfile
depends_on :
- postgres
ports :
- " ${SERVER_PORT}:${SERVER_PORT} "
networks :
- postgres
restart : always
networks :
postgres :
driver : bridge
volumes :
postgres :
pgadmin : 이 구성 구성은 영구 데이터를 저장하기 위해 3 개의 컨테이너, 1 개의 네트워크 및 1 권을 만듭니다. 모든 컨테이너는 다른 컨테이너와 통신하기 위해 동일한 네트워크를 사용하고 있습니다. 고장 후 각 컨테이너가 자동으로 다시 시작됩니다. 컨테이너 postgres_container Postgresql Server를 생성합니다. 여기서 영구 데이터는 볼륨으로 저장됩니다. SQL 클라이언트 인 컨테이너 pgadmin_container 도 있습니다. server_container 라는 마지막 컨테이너는 기본 앱이 실행중인 곳입니다. 이미지 대신 이전 섹션에서 논의 된 DockerFile에 정의 된 빌드를 사용합니다.
개발 모드를위한 Compose Configuration은 배포 모드와 매우 유사합니다. 서버와 클라이언트가 Docker에서 실행되는 별도의 인스턴스이기 때문에 server_container 가 없다는 것입니다.
.env.example .env 로 복사하고 바꾸고 postgres 로 ${DB_HOST} 의 값을 설정하십시오.client/.env.production.example client/.env.production 으로 복사하여 바꿉니다.client/.env.production 에서 ${REACT_APP_GOOGLE_API} 에 대한 값을 얻으려면 다음 단계를 따르십시오. Jenkins 및 Application Server에 대한 액세스를 전달하려면 Reverse Proxy를 설정해야합니다. ./env 에 지정된 포트는 Nginx 리버스 프록시 구성의 포트와 일치해야합니다.
먼저 사용 가능한 사이트 구성 디렉토리로 이동하십시오.
cd /etc/nginx/sites-available/ 구성 파일을 만듭니다. 당신은 당신이 whish로 이름을 지정할 수 있습니다. 모범 사례는 DNS 주소와 같은 방식으로 이름을 지정하는 것입니다. 우리의 경우, 그것은 lora.fiit.stuba.sk 입니다.
touch lora.fiit.stuba.sk 다음 코드를 새로 만든 파일에 복사하여 붙여 넣습니다. .env 에 지정된 포트와 일치하도록 각 위치에 대한 proxy_pass 의 구성을 변경하십시오. DNS와 일치하도록 server_name 조정하는 것을 잊지 마십시오.
upstream jenkins {
keepalive 32; # keepalive connections
server 127.0.0.1:8080; # jenkins ip and port
}
# Required for Jenkins websocket agents
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
server_name lora.fiit.stuba.sk www.lora.fiit.stuba.sk;
# this is the jenkins web root directory
# (mentioned in the /etc/default/jenkins file)
root /var/run/jenkins/war/;
access_log /var/log/nginx/jenkins/access.log;
error_log /var/log/nginx/jenkins/error.log;
# pass through headers from Jenkins that Nginx considers invalid
ignore_invalid_headers off;
location /jenkins/ {
autoindex on ;
sendfile off ;
proxy_pass http://jenkins/jenkins/;
proxy_redirect default ;
proxy_http_version 1.1 ;
# Required for Jenkins websocket agents
proxy_set_header Connection $connection_upgrade ;
proxy_set_header Upgrade $http_upgrade ;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto $scheme ;
proxy_max_temp_file_size 0 ;
#this is the maximum upload size
client_max_body_size 10m ;
client_body_buffer_size 128k ;
proxy_connect_timeout 90 ;
proxy_send_timeout 90 ;
proxy_read_timeout 90 ;
proxy_buffering off ;
proxy_request_buffering off; # Required for HTTP CLI commands
proxy_set_header Connection "" ; # Clear for keepalive
}
location / {
#Add serving gzipped files
gzip_static on ;
gzip on ;
gzip_proxied no-cache no-store private expired auth;
gzip_min_length 256 ;
gzip_types
text/plain
text/css
application/json
application/javascript
text/xml
application/xml
application/xml+rss text/javascript
application/atom+xml
application/geo+json
application/x-javascript
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/xhtml+xml
font/eot
font/otf
font/ttf
image/svg+xml;
proxy_pass http://127.0.0.1:5000/;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto $scheme ;
# enable WebSockets
proxy_http_version 1.1 ;
proxy_set_header Upgrade $http_upgrade ;
proxy_set_header Connection "upgrade" ;
}
listen 443 ssl; # managed by Certbot
# SSL Configuration
ssl_certificate /etc/letsencrypt/live/lora.fiit.stuba.sk/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/lora.fiit.stuba.sk/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
# SSL redirect from HTTP to HTTPS
server {
if ( $host = lora.fiit.stuba.sk) {
return 301 https:// $host$request_uri ;
} # managed by Certbot
listen 80 ;
server_name lora.fiit.stuba.sk www.lora.fiit.stuba.sk;
return 404 ; # managed by Certbot
}파일을 저장하고 nginx 서비스를 다시 시작하십시오
sudo systemctl restart nginx Jenkins 응용 프로그램은 lora.fiit.stuba.sk/jenkins 에서 액세스 할 수 있어야합니다. 귀하의 경우 lora.fiit.stuba.sk 대신 DNS 이름입니다.
Jenkins의 기본 주택을 변경하려면 JENKINS_HOME 변수를 변경할 수 있습니다. 이 가이드는이 튜토리얼에서 크게 영감을 받았습니다.
Jenkins를 집으로 옮기려는 새로운 디렉토리를 만듭니다. 우리의 경우 /data/jenkins 입니다.
mkdir /data/jenkins새로 만든 디렉토리의 소유권을 변경하십시오.
sudo chown jenkins:jenkins /data/jenkins Old Jenkins Home Directory, /var/lib/jenkins/ 의 내용을 다음 명령을 사용하여 새로운 Jenkins 홈 디렉토리 /data/jenkins/ 에 복사하십시오.
sudo cp -prv /var/lib/jenkins /data/jenkins/다음으로 Jenkins 사용자 집을 변경하십시오.
sudo usermod -d /data/jenkins/ jenkins /etc/default/jenkins 에서 새로운 Jenkins 홈 디렉토리 경로를 업데이트하십시오. 선택한 편집자를 사용할 수 있습니다. 우리의 경우, 우리는 VI를 사용하고 있습니다.
sudo vi /etc/default/jenkinsJenkins Home 위치로 스크롤하여 새 홈 디렉토리 경로를 업데이트하십시오.
# defaults for Jenkins automation server
# pulled in from the init script; makes things easier.
NAME=jenkins
# arguments to pass to java
# Allow graphs etc. to work even when an X server is present
JAVA_ARGS= " -Djava.awt.headless=true "
# JAVA_ARGS="-Xmx256m"
# make jenkins listen on IPv4 address
# JAVA_ARGS="-Djava.net.preferIPv4Stack=true"
PIDFILE=/var/run/$NAME/$NAME.pid
# user and group to be invoked as (default to jenkins)
JENKINS_USER=$NAME
JENKINS_GROUP=$NAME
# location of the jenkins war file
JENKINS_WAR=/usr/share/$NAME/$NAME.war
# jenkins home location
JENKINS_HOME=/data/$NAME
# set this to false if you don't want Jenkins to run by itself
# in this set up, you are expected to provide a servlet container
# to host jenkins.
RUN_STANDALONE=true다음 명령을 사용하여 Jenkins 서비스를 시작하십시오.
sudo service jenkins start
lora.fiit.stuba.sk/jenkins 의 브라우저에서 Jenkins를 방문하십시오.
응용 프로그램에 로그인하십시오.
화면 왼쪽에있는 Manage Jenkins 하려면 탐색하십시오.
Security group 에 위치한 Manage Credentials 탭을 클릭하십시오.
그런 다음 테이블에있는 (global) 링크를 클릭하십시오.
화면 왼쪽의 addCredentials 버튼을 클릭하십시오.
Kind 드롭 다운 메뉴에서 Secret file 옵션을 선택하십시오.
이행 된 .env 파일을 업로드하고 ID 필드를 env 로 설정하십시오.
6 단계를 반복 한 다음 충족 된 client/.env.production 파일을 업로드하고 ID 필드를 envClient 로 설정하십시오.
다른 자격 증명을 만들지 만 이번에는 드롭 다운 메뉴에서 SSH Username with private key 선택하십시오. ID 필드를 lora-application-server-ssh 로 설정하십시오. 리포지토리에 대한 SSH 키 (응용 프로그램 서버의 소스 코드가있는 위치)를 얻으려면이 지침을 따르십시오.
대시 보드에서 화면 왼쪽에있는 New Item 버튼을 클릭하십시오.
원하는 이름을 입력하고 Pipeline 버튼을 클릭하고 확인 버튼을 클릭하십시오.
파이프 라인 스크립트 페이스트 다음 코드에
node {
/*** 1 Pull new changes from git branch deploy ***/
stage ( 'Pull master branch' ) {
git credentialsId : 'lora-application-server-ssh' , url : '[email protected]:danielhros/lora-application-server.git'
}
/*** 2 Add secret enviroment variables stored securely in Jenkins ***/
stage ( 'Add enviroment variables' ) {
/* 2.1 Remove .env file if exists */
sh 'rm -f -- .env'
/* 2.2 Add .env file to server */
withCredentials ( [ file ( credentialsId : 'env' , variable : 'env' ) ] ) {
sh "cp $env ./"
}
/* 2.2 Add .env file to client */
withCredentials ( [ file ( credentialsId : 'envClient' , variable : 'envClient' ) ] ) {
sh 'cp $envClient client/.env.production'
}
}
/*** 3 Build docker image ***/
stage ( 'Build docker image' ) {
sh '(docker-compose build)'
}
/*** 4 Run docker image in production ***/
stage ( 'Run docker images' ) {
sh '(docker-compose up -d)'
}
/*** 5 Clean after build ***/
stage ( 'Clean workspace' ) {
sh 'docker system prune -f'
cleanWs ( )
}
}변경 사항을 저장하고 첫 번째 파이프 라인을 실행하십시오
축하해요! 파이프 라인이 응용 프로그램을 성공적으로 빌드하고 배포 한 후 앱은 lora.fiit.stuba.sk 에서 액세스 할 수 있습니다.