Web programming course project for DevOps training. 2022-2023 Fall semester.
This is our Dockerfile:
FROM node:16-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build-css
CMD node server.js
This is our .dockerignore file:
node_modules
npm-debug.log
I ran this command to build the image:
docker build -t airways-spa .
but I got this error:
- ERROR [internal] load metadata for docker.io/library/node:16-slim
I solved this problem by running this command:
rm ~/.docker/config.json
Then everything was fine and I could build the image. To see the image, I ran this command:
docker images
This is our nginx.conf file:
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost 127.0.0.1;
location / {
add_header Access-Control-Allow-Origin $http_origin;
proxy_pass http://frontend:3001;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
This is our docker-compose.yml file:
version: "3"
services:
nginx:
image: nginx:latest
container_name: nginx_container
depends_on:
- frontend
volumes:
- ./frontend/nginx.conf:/etc/nginx/nginx.conf
ports:
- "8000:80"
restart: on-failure
frontend:
image: airways-spa
build:
context: .
dockerfile: ./frontend/Dockerfile
restart: on-failure
Then I ran this command to run the SPA and NGINX:
docker-compose up
Now we can see the SPA in the browser by going to this address:
http://localhost:8000
We use reverse proxy and port forwarding to access the SPA.
first we have to pull redis and postgres images with below commands:
sudo docker pull redis
sudo docker pull postgres
then we run the images on some containers with below commands:
sudo docker run --name rds -p 9001:6379 -d redis
sudo docker run --name pg1 -d -p 9002:5432 postgres -v /path/to/csv/files:/csv postgres
sudo docker run --name pg2 -d -p 9002:5432 postgres -v /path/to/csv/files:/csv postgres
to run these containers, we should use these commands:
sudo docker start rds
sudo docker start pg1
sudo docker start pg2
to access bash of a container, following command will help us:
sudo docker exec -it container-name bash
in order to define tables in postgres containers, after running above command and entering pg bash, use these:
su postgres
psql
and then copy&paste ticket.sql in shell!
as we have map /csv root of container to the path of csv files in our system, we can use below commands to imprt csv files to databases:
COPY table-name FROM '/csv/file-name.csv' DELIMITER ',' CSV HEADER;
db and redis containers are ready now!
first we create app images with following command:
sudo docker build -t image-name -f /path/to/dockerfile /path/to/start
then we run it on a container:
sudo docker run --name container-name -d image-name
then we create a network and connect redis, postgres and server containers to that:
sudo docker network create --driver bridge network-name
sudo docker network connect --alias container-tag network-name container-name
to create reverse proxy we sholud create default.conf to redirect requests to where we want:
server {
listen 80;
location / {
charset utf-8;
root /usr/share/nginx/html;
}
location /node/ {
add_header Access-Control-Allow-Origin $http_origin;
proxy_pass http://nodejs:3000;
}
.
.
.
}
1 | Mohammad Amin Lotfi | 98171075 |
2 | Mehdi Abdi | 98105892 |
3 | Mahdi Teymoory Anar | 99101354 |