How to run NodeJS command-line tools inside Docker?


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

  • Docker for Mac with WebStorm Node Remote Interpreter
  • Gitlab Mocha tests and Docker Tag problems
  • Is it possible to shut down the host machine by executing a command on one of its docker container?
  • cannot ADD file in current directory or in the directory with full path
  • Connect from web app to Oracle DB docker container
  • Difference between docker-compose and manual commands
    • 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.

  • Permission denied: docker-machine create
  • Run Docker by using shell script from remote machine?
  • setting system variables in MySQL running as docker container
  • Can I mount docker host directory as copy on write/overlay?
  • Rails app under Docker - how to handle container dependencies?
  • How to let a non-root process in a Linux docker container bind to a <1024 port?
  • 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.