How to run NodeJS command-line tools inside Docker?

Context

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

  • docker-machine: No route to host to default docker machine (using host-only vbox network) on some wifi networks
  • Generating new (not renewed) SSL Certificates with “letsencrypt certonly” in non-interactive mode
  • Python logic error in 'if' evaluation [closed]
  • Docker Lavarel Storage Permission Error
  • Docker EE Admin UI vs Portainer
  • Docker Ubuntu update-alternatives
    • 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

    docker-compose.yml

    version: '2'
    
    services:
      web:
        build: ../app-web/
        ports:
         - "80:80"
        tty: true
        # Add a volume to link php code on the host and inside the container
        volumes:
         - /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
        extra_hosts:
         - "localhost:127.0.0.1"
         - "assistant.docker:127.0.0.1"
         - "my_app.my_company.dev:127.0.0.1"
        depends_on:
         - custom-php
         - custom-mysql
         - custom-mongo
         - custom-node
        links:
         - custom-php:custom-php
         - custom-mysql:custom-mysql
         - custom-mongo:custom-mongo
    
      custom-php:
        build: ../app-php/
        ports:
         - "50:50"
        volumes:
         - /var/www/my_app:/usr/share/nginx/html/my_app
         - /var/www/my_app/docker_files/docker-assistant:/usr/share/nginx/html/assistant
    
      custom-mysql:
        build: ../app-mysql/
        #tty: true
        volumes:
         - /var/lib/mysql:/var/lib/mysql
         - /var/www/my_app/sql:/usr/share/sql
        environment:
         - MYSQL_ROOT_PASSWORD=rootpwd
         - MYSQL_DATABASE=appdb
         - MYSQL_USER=app
         - MYSQL_PASSWORD=apppwd
    
      custom-mongo:
        build: ../app-mongo/
        volumes:
         - /var/lib/mongodb:/data/db
    
      custom-node:
        build: ../app-npm/
        tty: true
    

    Node Dockerfile

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

    Question

    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
    

    or

    grunt watch
    

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

  • Babelrc file in Docker builds
  • How to change the ownership of Docker-Tomcat log files?
  • How to Increase the Speed of Docker Builds While Using Mounted Volumes
  • How to run multiple agents in multiple Minecraft environment with python threading in docker container?
  • How to use Dockerfile to link main container to a db container?
  • Problems with database setup on cookiecutter-django
  • 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            
    redis,webapp/db
    
    $ 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: https://docs.docker.com/engine/reference/commandline/exec/

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