LinuxCommandLibrary

docker-node

Manage nodes in a Docker Swarm cluster

TLDR

List nodes in the swarm

$ docker node ls
copy

List tasks running on one or more nodes, defaults to the current node
$ docker node ps [node1 node2 node3 ...]
copy

Display detailed information on one or more nodes
$ docker node inspect [node1 node2 node3 ...]
copy

Promote one or more nodes to manager in the swarm
$ docker node promote [node1 node2 node3 ...]
copy

Demote one or more nodes from manager in the swarm
$ docker node demote [node1 node2 node3 ...]
copy

Remove one or more nodes from the swarm
$ docker node rm [node1 node2 node3 ...]
copy

Update metadata about a node, such as its availability, labels, or roles
$ docker node update --[availability|role|label-add|...] [active|worker|foo|...] [node1]
copy

SYNOPSIS

docker build -t .
docker run -d -p :

PARAMETERS


    Name to assign to the built Docker image.


    Port on the host machine to forward traffic to.


    Port that the Node.js application listens on within the container.

DESCRIPTION

While 'docker-node' isn't a standard Linux command, it represents a common practice: using Docker to containerize and run Node.js applications. The core idea revolves around creating a Docker image based on a Node.js base image (e.g., `node:-alpine` or `node:-slim`). This image includes the Node.js runtime, your application's code, and any necessary dependencies defined in `package.json`. Dockerfiles typically use commands like `FROM`, `WORKDIR`, `COPY`, `RUN npm install`, `CMD` or `ENTRYPOINT` to construct the image. A common approach involves copying `package.json` and `package-lock.json` first, then running `npm install` or `yarn install` to leverage Docker's caching and speed up subsequent builds. The `CMD` instruction then specifies the command to start the Node.js application, such as `node server.js` or `npm start`. Running the container using `docker run` executes the Node.js app within the isolated Docker environment, ensuring consistent behavior across different systems. Docker Compose is frequently used to orchestrate Node.js applications with dependent services like databases.

CAVEATS

Requires a Dockerfile in the current directory. Ensure your application correctly handles signals for graceful shutdown within the Docker container.

DOCKERFILE EXAMPLE

FROM node:16-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
This Dockerfile copies your application to the /app directory, install the dependencies using npm, exposes the port 3000 and starts your app using npm start

RUNNING WITH DOCKER COMPOSE

Docker Compose allows for managing multi-container applications. Here's an example docker-compose.yml:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: postgres:13
docker-compose up starts the entire application, including the Node.js web app and a PostgreSQL database.

SEE ALSO

Copied to clipboard