1

I have the following docker-compose.yml:

version: '3'
services:
  frontend:
    build: 
      context: ./
      dockerfile: docker/Dockerfile
    image: tsl.web.frontend.image
    container_name: tsl.web.frontend.container
    ports:
      - "8080:80"

Notice the context: ./

And this is my docker-compose command:

docker-compose build --force-rm --build-arg ENVIRONMENT=development frontend

Here is the structure of my src code:

enter image description here

Notice I have red underlined two important aspects of this structure.

Now here is a part of my Dockerfile, I will include the entire Dockerfile below:

# https://stackoverflow.com/a/35774741/1258525
# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
COPY ./package.json /tmp/package.json
RUN npm --prefix /tmp install /tmp
RUN cp -rn /tmp/node_modules ./
#RUN npm --prefix ./ run build-development ./
RUN npm run build-development #failing here

COPY dist /usr/share/nginx/html

So the RUN npm run build-development command is failing because it cannot find package.json:

enter image description here

This is where I am confused about the execution context of the RUN statement. I was able to copy the package.json file to /tmp in the previous Dockerfile command COPY ./package.json /tmp/package.json. I think my context is still where my docker-compose.yml file lives but obviously it is not...

enter image description here

Complete Dockerfile:

FROM centos:7
MAINTAINER Brian Ogden

# Not currently being used but may come in handy
ARG ENVIRONMENT
ENV NODE_VERSION 6.11.1

RUN yum -y update; yum clean all
RUN yum -y install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm; yum -y makecache
RUN yum -y install nginx-1.12.0

# Cleanup some default NGINX configuration files we don’t need
RUN rm /etc/nginx/conf.d/default.conf

COPY ./docker/conf/frontend.conf /etc/nginx/conf.d/frontend.conf
COPY ./docker/conf/nginx.conf /etc/nginx/nginx.conf

#############################################
# NodeJs Install
#############################################
RUN yum install -y \
        wget

#Download NodeJs package
RUN wget https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz

#extract the binary package into our system's local package hierarchy with the tar command. 
#The archive is packaged within a versioned directory, which we can get rid of by passing the --strip-components 1 option. 
#We will specify the target directory of our command with the -C command:
#This will install all of the components within the /usr/local branch
RUN tar --strip-components 1 -xzvf node-v* -C /usr/local
#############################################

# https://stackoverflow.com/a/35774741/1258525
# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
COPY ./package.json /tmp/package.json
RUN npm --prefix /tmp install /tmp
RUN cp -rn /tmp/node_modules ./
#RUN npm --prefix ./ run build-development ./
RUN npm run build-development

COPY dist /usr/share/nginx/html

EXPOSE 8080

CMD ["nginx"]

2 Answers 2

1

You can run intermediate container with id 4e14504665eb and explore the filesystem.

$ docker run -it --rm 4e14504665eb  bash

or you can directly run

$ docker run -it --rm 4e14504665eb  ls -la /tmp

But these commands look a little bit weird

RUN npm --prefix /tmp install /tmp
RUN cp -rn /tmp/node_modules ./
RUN npm run build-development

You probably should run build command from /tmp directory

P.S. Your Dockerfile looks not optimal. I would suggest to use more optimized version

3
  • thanks for your optimized Docker file, if you look at my answer below I used many of your optimizations, you will probably also see more of what my issue was with npm run build-development Commented Dec 20, 2017 at 21:54
  • notice that your version of / && did not work and also CMD ["nginx", "-g", "daemon off;"] did not work for me, nginx did not start up Commented Dec 20, 2017 at 22:25
  • Without logs I can't help you Commented Dec 21, 2017 at 11:31
0

I believe the foundation of my misunderstanding was really the execution context of the Docker COPY command versus the RUN command.

I thought that the RUN command was running in the same execution context as COPY which would be the Dockerfile or the context stated in my docker-compose.yml because COPY was in this context.

That is not the case though, basically COPY knows ./ to the Dockerfile. RUN however does not know about anything outside of the Docker image being built.

I was trying to RUN a command against files I had not copied into the Docker image I was building. This section of my Dockerfile was the secret to success:

WORKDIR /app
COPY . /app

RUN npm run build-$ENVIRONMENT

And here is my complete working Dockerfile, thanks to ALex_hha for some of the syntax optimizations:

FROM centos:7
MAINTAINER Brian Ogden

# Not currently being used but may come in handy
ARG ENVIRONMENT
ENV NODE_VERSION 6.11.1

RUN yum -y update && \
    yum clean all && \
    yum -y install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm \
    yum -y makecache && \
    yum -y install nginx-1.12.0 wget

# Cleanup some default NGINX configuration files we don’t need
RUN rm /etc/nginx/conf.d/default.conf

#############################################
# NodeJs Install
#############################################

#Download NodeJs package
RUN wget -q -O - https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz \
    | tar --strip-components=1 -xzf - -C /usr/local
#pipe tar is extracting the binary package into our system's local package hierarchy with the tar command. 
#The archive is packaged within a versioned directory, which we can get rid of by passing the --strip-components 1 option. 
#We will specify the target directory of our command with the -C command:
#This will install all of the components within the /usr/local branch

# https://stackoverflow.com/a/35774741/1258525
# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
COPY ./package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir /app && cp -a /tmp/node_modules /app/

WORKDIR /app
COPY . /app

RUN npm run build-$ENVIRONMENT

RUN cd /app && cp -a dist/* /usr/share/nginx/html
COPY ./docker/conf/frontend.conf /etc/nginx/conf.d/frontend.conf
COPY ./docker/conf/nginx.conf /etc/nginx/nginx.conf


EXPOSE 80

CMD ["nginx"]

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.