简体   繁体   中英

How to run a javascript client inside a Docker container?

So, I have 3 files:

  • client.js
  • app.js
  • database.js

In the app.js file, I have created an express server with multiple services that connect to the database. The client.js file runs the services, using node-fetch.

So far, I have created a Dockerfile to run app.js:

FROM node:10
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node app
EXPOSE 3000

Perhaps this is a dumb question but, is there any way to create another Dockerfile to run the client.js inside a container?

Thank you.

Use docker-compose and put all the services in a same network.

Here's a gross outline of how it should look:

version: "3.3"

services:
  app:
    image: #image built for client, I recommend node:alpine
    ports:
      - "MACHINE_PORT:CONTAINER_PORT"
  network:
      - your_network
  database: 
    image: #image of database system you're using
           #use docker pull NAME_OF_DATABASE_SYSTEM
    network:
      - your_network
    ports:
      ...
  client: #image of the client built with node:alpine
    network:
      - your_network

    ports: 
      ...

networks:
  - your_network

A basic way to structure a project like this is with docker-compose.

Create a docker-compose.yml file in your project root:

version: "3.9"

services:
  app:
    image: sarah/app
    build: app
    ports:
    - 3000:3000
    environment:
      SERVICES: http://services:3000

  services:
    image: sarah/services
    build: services
    environment:
      DATABASE: database:2345

  database:
    image: mysql # whatever....

Then create a folder app with a Dockerfile and move app.js there And a folder services and move the other.js files there. I have added environment variables demonstrating how one can configure the services with the correct connection strings to the other services. Implementing the conviguration via env vars is up to you.

docker-compose up will start 3 containers - one each for services, app and database.
docker-compose build will rebuild any images that the Dockerfiles (or other files) have changed.

In your app container use the connection string "services:" to connect to the service app, as each container is added to a private dns using its service name, so "database:" will also be the address used to access your db from whichever container.

http://localhost:3000 on a desktop browser will navigate to the app on its published port.

In compose v3 the services dont need to be explicitly added to networks, or linked as they are automatically added to a "default" network that you will see created when you first deploy (up) the compose file.

Apologies to @gordon_freeman for the redundant answer, but I wanted to present an example with up to date syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM