- 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 is a template of application.
- Similar to
class
in OOP.
- Container is an instance from Image.
- Similary to
object
in OOP.
- Dockerfile is a text document specifying
command
s that user can call.
- Repository for Image.
- Similar to
GitHub
, but it is for Docker Image.
- List running docker container(s)
docker pull <image-name>[:<tag>]
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.
docker run <image-name>[:<tag>]
- Instantiate docker image on the background
docker run --detach <image-name>[:<tag>]
docker stop <CONTAINER ID>
- 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"]
- Build Docker image
docker build -tag <Image name:tag> <Dockerfile location>
docker build -t app:1.0 ./
- 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>
docker run --detach --publish 3000:3000 app:1.0