darckcrystale
darckcrystale

Reputation: 1722

How to run NodeJS command-line tools inside Docker?

Context

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

to serve the web app we develop.

We also use some command-line tools using NodeJS:

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 "[email protected]"

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.

Upvotes: 1

Views: 7976

Answers (2)

iyogeshjoshi
iyogeshjoshi

Reputation: 539

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

Upvotes: 1

Frenus
Frenus

Reputation: 830

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/

Upvotes: 2

Related Questions