From a47c9aff847f76d86e85a9764ea22dcdd892754c Mon Sep 17 00:00:00 2001 From: Ndahimana Bonheur Date: Fri, 17 May 2024 06:29:20 +0200 Subject: [PATCH 1/2] Setup docker Setup Docker image and hosted it on Docker Hub --- Dockerfile.database | 10 ++++++++++ Dockerfile.nodejs | 12 ++++++++++++ README.Docker.md | 22 ++++++++++++++++++++++ docker-compose.yml | 26 ++++++++++++++++++++++++++ package-lock.json | 11 +++++++++++ src/databases/config/connection.ts | 2 +- 6 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 Dockerfile.database create mode 100644 Dockerfile.nodejs create mode 100644 README.Docker.md create mode 100644 docker-compose.yml diff --git a/Dockerfile.database b/Dockerfile.database new file mode 100644 index 00000000..185f08e8 --- /dev/null +++ b/Dockerfile.database @@ -0,0 +1,10 @@ +FROM postgres:latest + +ARG DB_USER +ARG DB_PASSWORD +ARG DB_NAME + +# Set environment variables +ENV POSTGRES_USER=$DB_USER +ENV POSTGRES_PASSWORD=$DB_PASSWORD +ENV POSTGRES_DB=$DB_NAME \ No newline at end of file diff --git a/Dockerfile.nodejs b/Dockerfile.nodejs new file mode 100644 index 00000000..16b2f1ec --- /dev/null +++ b/Dockerfile.nodejs @@ -0,0 +1,12 @@ +FROM node:20-alpine + +WORKDIR /app + +COPY package.json ./ +RUN npm install + +COPY . . + +EXPOSE 5000 + +CMD npm start \ No newline at end of file diff --git a/README.Docker.md b/README.Docker.md new file mode 100644 index 00000000..fa3048f6 --- /dev/null +++ b/README.Docker.md @@ -0,0 +1,22 @@ +### Building and running your application + +When you're ready, start your application by running: +`docker compose up --build`. + +Your application will be available at http://localhost:3000. + +### Deploying your application to the cloud + +First, build your image, e.g.: `docker build -t myapp .`. +If your cloud uses a different CPU architecture than your development +machine (e.g., you are on a Mac M1 and your cloud provider is amd64), +you'll want to build the image for that platform, e.g.: +`docker build --platform=linux/amd64 -t myapp .`. + +Then, push it to your registry, e.g. `docker push myregistry.com/myapp`. + +Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/) +docs for more detail on building and pushing. + +### References +* [Docker's Node.js guide](https://docs.docker.com/language/nodejs/) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..28a182df --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: latest + +services: + backend: + build: + context: . + dockerfile: Dockerfile.nodejs + ports: + - "5000:5050" + env_file: + - .env + depends_on: + - database + + + + database: + build: + context: . + dockerfile: Dockerfile.database + args: + DB_USER: ${DB_USER} + DB_PASSWORD: ${DB_PASSWORD} + DB_NAME: ${DB_NAME} + ports: + - "3000:4000" \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5bdc32fc..7bcac46a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1706,6 +1706,17 @@ "node": ">=0.10" } }, + "node_modules/dashdash": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", + "integrity": "sha512-jRFi8UDGo6j+odZiEpjazZaWqEal3w/basFjQHQEwVtZJGDpxbH1MeYluwCS8Xq5wmLJooDlMgvVarmWfGM44g==", + "dependencies": { + "assert-plus": "^1.0.0" + }, + "engines": { + "node": ">=0.10" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/src/databases/config/connection.ts b/src/databases/config/connection.ts index 5710430c..9c86e79f 100644 --- a/src/databases/config/connection.ts +++ b/src/databases/config/connection.ts @@ -15,4 +15,4 @@ export const syncDatabase = async () => { sequelize.sync({ force: true }); }; -export default sequelize; +export default sequelize; \ No newline at end of file From e44f2abe0e176711c6bcde8d37a25235227fa2c2 Mon Sep 17 00:00:00 2001 From: Ndahimana Bonheur Date: Fri, 17 May 2024 12:30:51 +0200 Subject: [PATCH 2/2] [Finishes #187584909] Completed to setup docker --- Dockerfile.database | 9 --------- Dockerfile.nodejs => Dockerfile.server | 7 ++++--- docker-compose.yml | 26 ++++++++++++++------------ 3 files changed, 18 insertions(+), 24 deletions(-) rename Dockerfile.nodejs => Dockerfile.server (53%) diff --git a/Dockerfile.database b/Dockerfile.database index 185f08e8..add33403 100644 --- a/Dockerfile.database +++ b/Dockerfile.database @@ -1,10 +1 @@ FROM postgres:latest - -ARG DB_USER -ARG DB_PASSWORD -ARG DB_NAME - -# Set environment variables -ENV POSTGRES_USER=$DB_USER -ENV POSTGRES_PASSWORD=$DB_PASSWORD -ENV POSTGRES_DB=$DB_NAME \ No newline at end of file diff --git a/Dockerfile.nodejs b/Dockerfile.server similarity index 53% rename from Dockerfile.nodejs rename to Dockerfile.server index 16b2f1ec..ce92b044 100644 --- a/Dockerfile.nodejs +++ b/Dockerfile.server @@ -2,11 +2,12 @@ FROM node:20-alpine WORKDIR /app -COPY package.json ./ +COPY ./package.json . + RUN npm install COPY . . -EXPOSE 5000 +EXPOSE 3000 -CMD npm start \ No newline at end of file +CMD ["npm", "start"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 28a182df..b258d36d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,26 +1,28 @@ -version: latest +version: "3" services: - backend: - build: + server: + build: context: . - dockerfile: Dockerfile.nodejs + dockerfile: Dockerfile.server ports: - - "5000:5050" + - "3000:3000" env_file: - .env depends_on: - database - - database: build: context: . dockerfile: Dockerfile.database - args: - DB_USER: ${DB_USER} - DB_PASSWORD: ${DB_PASSWORD} - DB_NAME: ${DB_NAME} ports: - - "3000:4000" \ No newline at end of file + - "5432:5432" + environment: + DATABASE_USER: ${DOCKER_DATABASE_USER} + DATABASE_NAME: ${DOCKER_DATABASE_NAME} + DATABASE_PASSWORD: ${DOCKER_DATABASE_PASSWORD} + POSTGRES_USER: ${DOCKER_DATABASE_USER} + POSTGRES_NAME: ${DOCKER_DATABASE_NAME} + POSTGRES_PASSWORD: ${DOCKER_DATABASE_PASSWORD} + POSTGRES_HOST_AUTH_METHOD: ${DOCKER_DATABASE_HOST_AUTH_METHOD} \ No newline at end of file