DOCKER - 5 1 Shelf life of a container and give persistent 1

This lesson is the solution to the previous exercise — creating our first Dockerfile to package a Node.js web app. Following the project's instructions, we start with the official Node image, expose the right port, install the small tini init system, copy the source, install dependencies and define the start command. A few attempts may be needed before the first build is fully green — that's perfectly normal.

The complete Dockerfile

FROM node:6-alpine
EXPOSE 3000
RUN apk add --update tini \
    && mkdir -p /usr/src/app \
    && rm -rf /var/cache/apk/*
WORKDIR /usr/src/app
COPY package.json package.json
RUN npm install && npm cache clean --force
COPY . .
CMD ["tini", "--", "node", "./bin/www"]

Key choices: the install and cleanup commands are chained on the same RUN line so they all end up in one image layer — that keeps the image small. We also set WORKDIR right after creating the directory, which saves cd calls later in the Dockerfile.

Now build the image, run it, and check the result:

docker image build -t testnode .
docker container run --rm -p 80:3000 testnode

Open localhost in a browser — the Node app responds. docker image ls shows our fresh image at the top.

  • Retag with your Docker Hub account: docker image tag testnode myaccount/testnode
  • Push: docker image push myaccount/testnode
  • Verify it appears on Docker Hub, then prove the round trip works

To finish, delete the local image with docker image rm myaccount/testnode, then run it again from scratch: docker container run --rm -p 80:3000 myaccount/testnode. Docker will pull the image from the Hub and start it. Refresh the browser — the page still works, end to end.