Skip to content

Instantly share code, notes, and snippets.

@mkol5222
Last active May 25, 2020 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkol5222/d2ffaaeb03eae9eda3e7e47c89f711c0 to your computer and use it in GitHub Desktop.
Save mkol5222/d2ffaaeb03eae9eda3e7e47c89f711c0 to your computer and use it in GitHub Desktop.

First Docker Image

Requirements

  • Docker Hub account, note your username your-usernamename
  • Docker installed on desktop - instructions

Reference

  • article Dockerizing a Node.js web app
  • repo mkol5222/nodeinfo

Steps

Clone template

git clone https://github.com/mkol5222/nodeinfo.git
cd nodeinfo

Review Dockerfile


FROM node:10

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

Build your image

# build
docker build -t <your-username>/nodeinfo .
# see locally
docker images

Run your image

# run
docker run -p 58080:8080 -d <your-username>/nodeinfo
# see running
docker ps
# see logs
docker logs <your container id>
# open browser to http://localhost:58080 or
curl -i localhost:58080

Output of `docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
eeb57d9b2b7d        <your-username>/nodeinfo   "docker-entrypoint.s…"   32 seconds ago      Up 29 seconds       0.0.0.0:58080->8080/tcp   practical_liskov

Enter container

# Enter the container
$ docker exec -it <container id> /bin/bash

Publishing container

# login with Docker Hub credentials
docker login
# upload image
docker push <your-username>/nodeinfo
# visit image at https://hub.docker.com
# e.g. https://hub.docker.com/r/mkol5222/nodeinfo

Run on K8S cluster

# run your image
kubectl run nodeinfo --image=mkol5222/nodeinfo
pod/nodeinfo created
# get info including IP and container creation information
kubectl describe pod/nodeinfo
# access inside cluster (e.g. "minikube ssh" first)
curl -i <pod-ip>:8080

Alternative container image repositories

I have uploaded(pushed) image also to GitHub Packages container image repository https://github.com/mkol5222/nodeinfo/packages

# pull
docker pull docker.pkg.github.com/mkol5222/nodeinfo/nodeinfo:1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment