How to run NodeJS command-line tools inside Docker?


I am creating a big Docker Compose for our dev env. We use:

  • Docker. Send mail from host
  • Rancher agent can't run on master server?
  • (docker-compose + nginx load balancing) + Jenkins?
  • Launch Docker containers to handle HTTP requests
  • I want to get the artifact version deployed by Jenkins
  • How to make sbt-native-packager combine two layers of docker image
    • nginx
    • PHP
    • MySQL
    • MongoDB

    to serve the web app we develop.

    We also use some command-line tools using NodeJS:

    • Grunt (to compile SCSS)
    • JSHint

    So I have a docker-compose.yml, and five Dockerfile (one for each service and one containing NodeJS). Building works.

    Docker configuration


    version: '2'
        build: ../app-web/
         - "80:80"
        tty: true
        # Add a volume to link php code on the host and inside the container
         - /var/www/my_app:/usr/share/nginx/html/my_app
         - /var/www/my_app/docker_files/docker-assistant:/usr/share/nginx/html/assistant
        # Add hostnames to allow devs to call special url to open sites
         - "localhost:"
         - "assistant.docker:"
         - ""
         - custom-php
         - custom-mysql
         - custom-mongo
         - custom-node
         - custom-php:custom-php
         - custom-mysql:custom-mysql
         - custom-mongo:custom-mongo
        build: ../app-php/
         - "50:50"
         - /var/www/my_app:/usr/share/nginx/html/my_app
         - /var/www/my_app/docker_files/docker-assistant:/usr/share/nginx/html/assistant
        build: ../app-mysql/
        #tty: true
         - /var/lib/mysql:/var/lib/mysql
         - /var/www/my_app/sql:/usr/share/sql
         - MYSQL_ROOT_PASSWORD=rootpwd
         - MYSQL_DATABASE=appdb
         - MYSQL_USER=app
         - MYSQL_PASSWORD=apppwd
        build: ../app-mongo/
         - /var/lib/mongodb:/data/db
        build: ../app-npm/
        tty: true

    Node Dockerfile

    FROM node:7.8.0-alpine
    MAINTAINER DarckCrystale ""
    CMD [ "node" ]


    How can I run a NodeJS command-line tool from a Docker container (e.G. Grunt)?

    For example, when I need to compile SCSS, without Docker, I go to my app folder:

    cd /var/www/my_app_folder

    And I run grunt:



    grunt watch

    But I can’t figure out how to do this with my node Docker container.

  • Dynamically assigns different value for the environment variable upon a Docker service scale
  • docker stack/swarm golang port not exposed
  • Spring Boot Array Environment Variables on Docker / Marathon
  • Manage docker images from “docker jenkins container”?
  • Docker access localhost
  • PDOException SQLSTATE[HY000] [2002] No such file or directory. Whilst running laravel via isolated docker containers
  • 2 Solutions collect form web for “How to run NodeJS command-line tools inside Docker?”

    You can do the same by adding CMD and WORKDIR in your Dockerfile. For your example specific, try adding this to your Dockerfile

    WORKDIR /var/www/my_app_folder
    CMD grunt
    // or 
    CMD grunt watch

    for more detail you can refer this link by Digital Ocean

    ————– Edit —————

    In case if you want execute it based on your development scenario try the following:

    @darkckcrystale I see well I was giving you production level solution, well in dev case try running following
    // This will list all the running containers
    $ docker ps
    // output
    CONTAINER ID        IMAGE                        COMMAND                
    CREATED              STATUS              PORTS               NAMES
    4c01db0b339c        ubuntu:12.04                 bash                   
    17 seconds ago       Up 16 seconds       3300-3310/tcp       webapp
    d7886598dbe2        crosbymichael/redis:latest   /redis-server --dir    
    33 minutes ago       Up 33 minutes       6379/tcp            
    $ docker exec -it 4c01db0b339c grunt
    // docker exec [options] [container id/name] <command>

    for more details check this link

    With docker exec you can run commands inside containers e.g.:

    docker exec my-container-name grunt

    More info:

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.