Skip to content

Instantly share code, notes, and snippets.

@velp
Last active April 22, 2020 08:18
Show Gist options
  • Save velp/7e11fd9a55d303f8b18c89cdee320a63 to your computer and use it in GitHub Desktop.
Save velp/7e11fd9a55d303f8b18c89cdee320a63 to your computer and use it in GitHub Desktop.
How to add Dockerfile and test it locally

Step 1: check/create Dockerfile in root

Your repository should have Dockerfile in the root directory. Specification of Dockerfile you can find here: https://docs.docker.com/engine/reference/builder/

For Node.JS application it can be something like this:

FROM node:12.10.0

# Create directory where we will place the application
WORKDIR /usr/src/app

# Copy application source code to the image
COPY . .

RUN npm install && \
    npm run build
# Which port the application expose
EXPOSE <PORT_EXPOSED_BY_APPLICATION>

# How we will run application
ENTRYPOINT npm run start

Where <PORT_EXPOSED_BY_APPLICATION> is a port which application listen inside container.

Step 2: build docker image locally

To build docker image locally ypou already should have installed docker-daemon on your laptop. How to do it you can find here: https://docs.docker.com/engine/install/

Then you need to go to the root of your repository and run command:

docker build -t test-app:latest .

As result you will have docker image with name test-app and tag latest.

Step 3: run docker image locally to test

After previous steps you should check docker image. To do this you have to run docker image on your laptop by command:

docker run --rm -pPORT_INSIDE:80 test-app:latest

Where PORT_INSIDE is port from Dockerfile from line EXPOSE.

After running application should be run on http://localhost, so you can open this URL in your browser and check the application. If you see correct web application there you can add this Dockerfile to the repo.

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