Skip to content

Docker 101

Wed 05 Jun 2024  
🎉 Hi: ... 🎉

Docker command

Run docker

คำสั่ง docker run ใช้สำหรับการสร้าง docker container

docker run -p [port:port] [image]

bash
docker run -d -p 11111:10000 johanp/webmin

Exec docker

คำสั่ง ssh เข้าไปด้่านใน container

docker exec -it [Container name] bash

bash
docker exec -it my-container bash

Start/Stop docker

คำสั่ง Start/Stop container

docker star/stop [Container name]

bash
docker stop my-container

Remove docker

คำสั่ง Remove container

docker rm [Container name]

bash
docker rm my-container

Inspect docker

คำสั่งแสดงรายละเอียดของ container

docker inspect [Container name]

bash
docker inspect my-container
json
// Example Output
{
  "Id": "70532...",
  "Created": "20xx-xx-17T01:31:20",
  "Path": "/docker-xxxx.sh",
  "Args": ["kong", "docker-start"]
}

Stat docker

คำสั่ง แสดงข้อมูลการใช้ทรัพยากรของ docker container

docker stats [Container name]

bash
docker stats my-container
bash
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT   MEM %     NET I/O         BLOCK I/O        PIDS
705...   kong-3    7.87%     254MiB / 1.894GiB   13.09%    157MB / 143MB   37.5MB / 279kB   5

List docker

คำสั่ง แสดงรายการ docker container

bash
docker ps -a
bash
# Output #
CONTAINER ID   IMAGE  COMMAND CREATED STATUS PORTS NAMES
d5d...   portainer/portainer-ce  "/portainer..."   5 weeks ago Up 2 days 0.0.0.0:xxxx->xxxx/tcp portainer
c75...   grafana/promtail "/usr/bin/promtail -…"   5 weeks ago Up 2 days promtail

Docker image

Search image

คำสั่งค้นหา docker image

docker search [images name]

bash
docker search nginx
bash
# Output #
NAME                               DESCRIPTION                                     STARS     OFFICIAL
nginx                              Official build of Nginx.                        19975     [OK]
unit                               Official build of NGINX Unit: Universal Web   32        [OK]
nginx/nginx-ingress                NGINX and  NGINX Plus Ingress Controllers fo…   92

Pull image

คำสั่งดาวน์โหลด docker image

docker pull [Image name]

bash
docker pull nginx
bash
# Output #
Using default tag: latest
latest: Pulling from library/nginx
f11c1adaa26e: Pull complete
c6b156574604: Pull complete
ea5d7144c337: Pull complete
1bbcb9df2c93: Pull complete
537a6cfe3404: Pull complete
767bff2cc03e: Pull complete
adc73cb74f25: Pull complete
Digest: sha256:67682...
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

List image

คำสั่งแสดง docker image ที่ได้ดาวน์โหลดมาแล้ว

docker image ls

bash
docker image ls
bash
# Output #
REPOSITORY               TAG             IMAGE ID       CREATED         SIZE
nginx                    latest          fffffc90d343   12 days ago     188MB

Remove image

คำสั่งลบ docker image

docker image rm [Image name]

bash
docker image rm nginx
bash
# Output #
Untagged: nginx:latest
Untagged: nginx@sha256:67682...
Deleted: sha256:fffff...
Deleted: sha256:9f4b5...
Deleted: sha256:8e6e1...
Deleted: sha256:dcec8...
Deleted: sha256:4994a...
Deleted: sha256:8b2bc...
Deleted: sha256:a4197...
Deleted: sha256:32148...

Build prune

คำสั่ง ลบไฟล์ cache จากการ build ที่ไม่ได้ใข้งาน

bash
docker builder prune

Docker network

Create network

คำสั่งการสร้าง Docker network

docker network create [driver] [subnet] [gateway] [network name]

bash
docker network create --driver=bridge \
--subnet=10.0.88.0/24 --gateway=10.0.88.1 my-network

List network

คำสั่งแสดงรายการ network

bash
docker network ls
bash
# Output #
NETWORK ID     NAME                 DRIVER    SCOPE
f4e9cd62b...   bridge               bridge    local
a931486ef...   host                 host      local
ef7561502...   none                 null      local
fcd33dc00...   my-network      bridge    local

Inspect network

คำสั่งดูรายละเอียดของ docker network

docker network inspect [network name]

bash
docker network inspect my-network

Remove network

คำสั่งลบ docker network

docker network rm [network name]

bash
docker network rm my-network

Docker compose

docker-compose.yml

การใช้ docker compose ด้วย docker image จากตัวอย่างจะมีการเลิอกใช้ docker network ที่สร้างขึ้นมาและมีการใช้ Volume จากตำแหน่งปัจจุบัน

yml
networks:
  my-network:
    external: true

services:
  my-www:
    image: nginx:1.24.0-alpine
    container_name: my-www
    restart: always
    volumes:
      - ./www:/var/www/html
    ports:
      - '80:80'
    environment:
      - NODE_ENV=production
    networks:
      my-network:
        ipv4_address: 10.1.1.88

Up/Down

คำสั่งให้ docker compose เริ่มทำงาน

bash
docker compose up -d

คำสั่งให้ docker compose หยุดทำงาน

bash
docker compose down

Built with: VitePress.