Docker Community Forums

Share and learn in the Docker community.

Problem when building my image

docker

(Skahrz) #1

I m actually having some problem when trying to create an image with a simple remote project.

In , fact during the process, I try to launch “npm test”. Actually, the software telles me that the “package.json” doesn’t exist.

In fact, it exists, npm install works just like 3 little below.

Can you help me ?

My DockerFile :

FROM phusion/baseimage:0.9.17

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

# Install corresponding packages
# Git, pm2, curl
RUN apt-get update && apt-get install -y \
  git \
  curl

# Downloading NodeJs v0.12
RUN curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
RUN apt-get install -y nodejs

# Cloning repository, could change with the good project git repository
RUN git clone https://github.com/Skahrz/test-docker.git

# Install global dependencies
RUN npm install -g mocha gulp

# Move inside of the project
RUN cd test-docker && npm install

# Install local dependencies
RUN npm install esdoc

# Pre-production tasks && testing
RUN npm test

# Documentation generation
RUN esdoc -c esdoc.json

# Start the project as a daemon
#more informations to have pm2 reports on the github page : https://github.com/Unitech/pm2
RUN pm2 start dist/app.js


# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

(Ranjandas) #2

The issue you are facing is when you are running RUN npm test you are not inside test-docker directory.

RUN cd test-docker && npm install

In the above step the npm install worked for you as you cd’ed intothe test-docker directory in the same step, or in Docker terminology in the same layer. So you are in the directory test-docker in just this layer. In all subsequent instructions your RUN commands are run from the “/” directory in container.

To fix this issue, you can do either of the following.

# run npm test after changing into test-docker directory
RUN cd test-docker && npm test

In the above step changing into test-docker is a repeat. which is somewhat useless.

The best way i feel is to use the WORKDIR instruction inside Dockerfile

.....
RUN npm install -g mocha gulp

# WORKDIR will change your working directory to test-docker

WORKDIR test-docker

# Now you are in test-docker so no need to cd
RUN  npm install

# Install local dependencies
RUN npm install esdoc

# Pre-production tasks && testing. This will work as still your working directory is test-docker
RUN npm test

Hope this helps.

Regards