How to run NodeJS command-line tools inside Docker?


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

  • Nginx Net::ERR_INCOMPLETE_CHUNKED_ENCODING when trying to show large JSON
  • Docker container for SQL Server Linux keeps exiting
  • PDO connection error when running PHINX migration on a Docker Container
  • Link nodejs app to Rethinkdb from another container
  • Does a docker container have its own TCP/IP stack?
  • Bamboo on Windows Machine Unable to create docker container
    • 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.

  • How i can install Actor Platform with Docker
  • How to attach visual studio 2015 debugger to previously deployed core docker container
  • How to manage code changes in rails with docker
  • Accessing rabbitmq running on local machine from docker container
  • Docker for a one shot CLI application
  • 2 Docker environments in same VPC
  • 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.