Home > Mobile >  Deploy ReactJS application on CentOS 7 server using Docker
Deploy ReactJS application on CentOS 7 server using Docker

Time:03-04

I have deployed a ReactJS (with neo4j database) application on CentOS 7 server. I want to deploy another instance of the same application on the same server using docker. I have installed docker (version 20.10.12) on the server (CentOS 7).

On the server, I have cloned my reactjs project and created following Dockerfile:

FROM node:16 as builder
WORKDIR /app

COPY package.json .
COPY package-lock.json .
RUN npm install
COPY . .
RUN npm run build

FROM httpd:alpine
WORKDIR /var/www/html
COPY --from=builder /app/build/ .

and following docker-compose.yaml file:

version: '3.1'
services:
  app-test:
    hostname: my-app-test.com
    build: .
    ports:
      - '80:80'
    command: nohup node server.js &
    networks:
      - app-test-network
  app-test-neo4j:
    image: neo4j:4.4.2
    ports:
      - '7474:7474'
      - '7473:7473'
      - '7687:7687'
    volumes:
      - ./volumes/neo4j/data:/data
      - ./volumes/neo4j/conf:/conf
      - ./volumes/neo4j/plugins:/plugins
      - ./volumes/neo4j/logs:/logs
      - ./volumes/neo4j/import:/import
      - ./volumes/neo4j/init:/init
    networks:
      - app-test-network
    environment:
      - NEO4J_AUTH=neo/password
      - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
      - NEO4J_dbms_security_procedures_unrestricted=apoc.*
      - NEO4J_dbms_security_procedures_whitelist=apoc.*
      - NEO4J_dbms_default__listen__address=0.0.0.0
      - NEO4J_dbms_connector_bolt_listen__address=:7687
      - NEO4J_dbms_connector_http_listen__address=:7474
      - NEO4J_dbms_connector_bolt_advertised__address=:7687
      - NEO4J_dbms_connector_http_advertised__address=:7474
      - NEO4J_dbms_default__database=neo4j
      - NEO4JLABS_PLUGINS=["apoc"]
      - NEO4J_apoc_import_file_enabled=true
      - NEO4J_apoc_export_file_enabled=true
      - NEO4J_apoc_import_file_use__neo4j__config=true
      - NEO4J_dbms_shell_enabled=true
networks:
  app-test-network:
    driver: bridge

But, after running docker-compose up , i get following error:

ERROR: for app-repo_app-test Cannot start service app-test: driver failed programming external connectivity on endpoint app-repo_app-test (2cffe4fa4299d6e53a784f7f564dfa49d1a2cb82e4b599391b2a3206563d0e47): ErroCreating app-repo_app-test-neo4j ... done

ERROR: for app-test  Cannot start service app-test: driver failed programming external connectivity on endpoint app-repo_app-test (2cffe4fa4299d6e53a784f7f564dfa49d1a2cb82e4b599391b2a3206563d0e47): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use
ERROR: Encountered errors while bringing up the project.

Can anyone give me any clue what went wrong here ? And, is it the correct approach to deploy the reactjs application on CentOS 7 server using Docker ?

CodePudding user response:

as said in my comment above: You are not using the first container in the Multi-Stage container.

See this article: https://docs.docker.com/develop/develop-images/multistage-build/#name-your-build-stages

And particularly the example code:

# syntax=docker/dockerfile:1
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go    ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"] 

The COPY --from=builder will do the trick for you.

  • Related