Skip to content

Latest commit

 

History

History
116 lines (81 loc) · 2.43 KB

Docker.md

File metadata and controls

116 lines (81 loc) · 2.43 KB

Docker

  • Main purpose of Docker is to run application the same way over all kinds of devices.
  • Docker Desktop enables to run application built for different OS. (E.g, Application built for Linux running on Windows)

Image

  • Image is a template of application.
  • Similar to class in OOP.

Container

  • Container is an instance from Image.
  • Similary to object in OOP.
  • Dockerfile is a text document specifying commands that user can call.
  • Repository for Image.
  • Similar to GitHub, but it is for Docker Image.

Command

  • List downloaded images
docker images
  • List running docker container(s)
docker ps -a
  • Download docker image
docker pull <image-name>[:<tag>]
  • Remove docker resource
docker stop <container_id> # 1. Docker container should be stopped.
docker rm <container_id> # 2. Docker container should be removed in advance.
docker rmi <image_id> # 3. Actually removing image.
  • Instantiate docker image
docker run <image-name>[:<tag>]
  • Instantiate docker image on the background
docker run --detach <image-name>[:<tag>]
  • Stop docker container
docker stop <CONTAINER ID>

Building Docker Image

  1. Dockerfile
# FROM sets the base image of newly creating image
# Includes operating system at the tag (E.g, Alpine Linux)
FROM node:20-alpine


# COPY copies file or directory to image
# Syntax: COPY <file/directory name> <location in image>
COPY package.json /App/
COPY src /App/

# WORKDIR moves current location in CLI (Same as cd)
WORKDIR /App

# RUN execute any CLI command
RUN npm i

# CMD sets command to execute when the image is instantiated to container
CMD ["node","server.js"]
  1. Build Docker image
docker build -tag <Image name:tag> <Dockerfile location>
  • Example
docker build -t app:1.0 ./

Port Binding

  • Application runs on isolated container with its own network, having different port than local network.
  • To link port between container port and local computer port, user need to bind the port. To link a port from a Docker container to a port on your local computer, user need to bind the ports when you run the container.
docker run -p <Local Computer Port>:<Container Port>
  • Example
docker run --detach --publish 3000:3000 app:1.0