Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
testProject
FROM ubuntu
MAINTAINER David Weinstein <david@bitjudo.com>
# install our dependencies and nodejs
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install python-software-properties git build-essential
RUN add-apt-repository -y ppa:chris-lea/node.js
RUN apt-get update
RUN apt-get -y install nodejs
# use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/
# From here we load our application's code in, therefore the previous docker
# "layer" thats been cached will be used if possible
WORKDIR /opt/app
ADD . /opt/app
EXPOSE 3000
CMD ["node", "server.js"]
{
"name": "testProject",
"description": "this project is awesome",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node server.js"
},
"dependencies": {
"async": "*"
}
}
// this is our simulated application code
//
var async=require('async');
// uncomment the following line after building the project
// docker will use the cache for the modules...
// console.log('hello');
@onlinejudge95
Copy link

onlinejudge95 commented Mar 4, 2020

How about making all the RUN commands in a single statement
Instead of

RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install python-software-properties git build-essential
RUN add-apt-repository -y ppa:chris-lea/node.js
RUN apt-get update
RUN apt-get -y install nodejs
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list && \
         apt-get update && \
         apt-get -y install python-software-properties git build-essential && \
         add-apt-repository -y ppa:chris-lea/node.js && \
         apt-get update && \
         apt-get -y install nodejs

@atharva-u-01
Copy link

atharva-u-01 commented Jan 12, 2022

@onlinejudge95 You can do that as well but this would result in whole image building process to boil down to a single layer.
This multi layer building concept which is being used in multi statement format has many advantages, as explained in this blog article.
For more info refer this official documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment