Skip to content

Instantly share code, notes, and snippets.

@zmts
Last active October 14, 2023 11:32
  • Star 51 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zmts/509f224950f85f3cfe4365e2b80081d1 to your computer and use it in GitHub Desktop.
Docker, TypeScript, Node.js

Docker, TypeScript, Node.js

Preconditions:

  • TS application listening port: 7777
|-- dist
|-- src
|-- .dockerignore
|-- Dockerfile
|-- package.json
|-- package-lock.json
`-- tsconfig.json

package.json scripts

"scripts": {
  "build": "tsc",
  "start": "node ./dist/main.js"
}

Dockerfile

FROM node:10-alpine

# update packages
RUN apk update

# create root application folder
WORKDIR /app

# copy configs to /app folder
COPY package*.json ./
COPY tsconfig.json ./
# copy source code to /app/src folder
COPY src /app/src

# check files list
RUN ls -a

RUN npm install
RUN npm run build

EXPOSE 7777

CMD [ "node", "./dist/main.js" ]

Docker commands

Images

Build docker image

docker build -t test-image-name .

Run image in interactive mode

docker run -it -p 7777:7777 test-image-name

Or run image in silent(daemon) mode

docker run -d -p 7777:7777 test-image-name

List all images

docker image ls

Remove all images at once

docker rmi $(docker images -q)

Containers

List all active containers

docker ps

List all active and dead containers

docker ps -a

Stop all running containers

docker stop $(docker ps -a -q)

Delete all stopped containers:

docker rm $(docker ps -a -q)

Other

Install help utils

apt-get install iputils-ping nmap

Jump into container shell

docker exec -it CONTAINER_ID /bin/sh
@Edr2
Copy link

Edr2 commented Oct 8, 2020

why you copied # copy configs to /app folder if you not npm install after? Usually we copy configs and then run npm install and then we copy code. In that order we can use docker cached layer for npm install command. In dockerfile above you will npm install every time when your code changed..

@ztane
Copy link

ztane commented Aug 31, 2021

@Edr2 you're right... it should rather be

COPY package*.json .
npm install

@Edr2
Copy link

Edr2 commented Sep 1, 2021

🙏

@Ramandeep69
Copy link

Nice project

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