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

The command docker-node is not a standard Linux executable. It conceptually refers to the process of running Node.js applications within Docker containers. A typical usage pattern involves:
docker run [OPTIONS] [IMAGE_NAME] [NODE_APP_COMMAND]
or to build an image:
docker build [BUILD_OPTIONS] . -t [IMAGE_NAME]

PARAMETERS

-p <host_port>:<container_port>
    Maps a port from the container to the host machine, making the Node.js application accessible from the host. (Used with docker run)

-v <host_path>:<container_path>
    Mounts a host directory as a volume in the container, commonly used for live code changes or persistent data. (Used with docker run)

-it
    Allocates a pseudo-TTY and keeps STDIN open, allowing for interactive sessions with the container. (Used with docker run)

--rm
    Automatically removes the container when it exits. (Used with docker run)

--name <container_name>
    Assigns a name to the container for easier identification. (Used with docker run)

--env <key>=<value>
    Sets environment variables inside the container, useful for configuration. (Used with docker run)

-t <image_name>
    Names and optionally tags the image in the 'name:tag' format. (Used with docker build)

[IMAGE_NAME]
    The name or ID of the Docker image containing the Node.js application to be run.

[NODE_APP_COMMAND]
    The command to execute inside the container, typically node app.js or npm start.

DESCRIPTION

The term docker-node refers to the practice of leveraging Docker to build, run, and deploy Node.js applications. It's not a standalone command but rather a conceptual way to describe how Docker's containerization technology benefits Node.js development by providing isolated environments. This approach ensures dependency consistency, simplifies deployment across different environments (development, staging, production), and avoids 'works on my machine' issues. Users typically define a Dockerfile to specify how their Node.js application should be built into a Docker image, including installing Node.js, copying application code, installing npm dependencies, and defining the application's entry point. Once an image is built, it can be run as a container, providing a lightweight, portable, and self-sufficient runtime for the Node.js application.

CAVEATS

The term docker-node is not a standalone executable command; it represents a conceptual workflow using Docker. Using Docker adds an abstraction layer and a learning curve. Initial setup can be complex, and debugging can sometimes be more challenging than with local execution. Performance overhead, though generally minimal, might be a consideration for very high-performance applications, and image size can grow large if not managed properly with techniques like multi-stage builds.

DOCKERFILE FOR NODE.JS

A Dockerfile is a script of instructions used to build a Docker image. For Node.js, it typically includes:
FROM node:latest (or a specific version)
WORKDIR /app (sets the working directory within the container)
COPY package*.json ./ (copies manifest files to leverage Docker's build cache)
RUN npm install (installs Node.js dependencies)
COPY . . (copies the rest of the application code)
EXPOSE <port> (informs Docker that the container listens on the specified network ports at runtime, e.g., 3000)
CMD ["node", "app.js"] (defines the default command to execute when the container starts).

BEST PRACTICES

Key best practices for containerizing Node.js applications include using small, official base images (e.g., node:alpine) to minimize image size. Implementing multi-stage builds is crucial to reduce the final image size by separating build-time dependencies (like compilers) from runtime dependencies. It's also vital to ensure applications run as a non-root user inside the container for security. Lastly, leverage .dockerignore to exclude unnecessary files (e.g., local node_modules, .git, .env files) from the build context, speeding up builds and reducing image size.

HISTORY

Docker emerged in 2013, revolutionizing application deployment through containerization. For Node.js applications, Docker quickly became indispensable due to the complexities of managing node_modules and ensuring consistent runtime environments across development, testing, and production. Early adoption focused on simplifying dependency management and overcoming 'environment drift.' The evolution of Dockerfiles and best practices, such as multi-stage builds and smaller base images, has continuously optimized the process of containerizing Node.js applications, making it a standard practice in modern web development.

SEE ALSO

docker(1), node(1), npm(1), yarn(1), docker-compose(1)

Copied to clipboard