TungNT (Blue)

tungnt.blue@gmail.com

User Tools

Site Tools


development:system:docker

Docker

Cài đặt Docker

Các câu lệnh hay dùng

Xem thông tin docker

TungNT:~ tungnt$ docker info
Client:
 Debug Mode: false
 
Server:
 Containers: 5
  Running: 4
  Paused: 0
  Stopped: 1
 Images: 10
 Server Version: 19.03.5
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.9.184-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.952GiB
 Name: docker-desktop
 ID: SDQB:DI7E:PGBQ:JEP3:FCVG:P3RL:QFRX:OUZR:HD3S:QAZZ:6JGR:HHRY
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 61
  Goroutines: 66
  System Time: 2020-05-22T04:06:51.4447299Z
  EventsListeners: 2
 HTTP Proxy: gateway.docker.internal:3128
 HTTPS Proxy: gateway.docker.internal:3129
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

Danh sách các Image

TungNT:~ tungnt$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
downloadhtmlfreecom_web1   latest              f34c75e446de        8 months ago        648MB
downloadhtmlfreecom_web3   latest              e58f64793629        8 months ago        648MB
downloadhtmlfreecom_web2   latest              8fde6ad35de0        8 months ago        648MB
ubuntu                     18.04               2ca708c1c9cc        8 months ago        64.2MB
mysql                      8.0.15              7bb2586065cd        14 months ago       477MB
TungNT:~ tungnt$ docker image ls
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
downloadhtmlfreecom_web1   latest              f34c75e446de        8 months ago        648MB
downloadhtmlfreecom_web3   latest              e58f64793629        8 months ago        648MB
downloadhtmlfreecom_web2   latest              8fde6ad35de0        8 months ago        648MB
ubuntu                     18.04               2ca708c1c9cc        8 months ago        64.2MB
mysql                      8.0.15              7bb2586065cd        14 months ago       477MB

Danh sách tất cả các Container

TungNT:~ tungnt$ docker container ls --all
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                      PORTS                               NAMES
68e2b006200b        ubuntu:18.04               "/bin/bash"              7 months ago        Exited (255) 7 months ago                                       focused_almeida
e19358882f52        downloadhtmlfreecom_web1   "/venv/start.sh bash"    7 months ago        Up 2 hours                  0.0.0.0:1011->80/tcp                web1
f47f82d50d41        downloadhtmlfreecom_web3   "/venv/start.sh bash"    7 months ago        Up 2 hours                  0.0.0.0:1013->80/tcp                web3
42c5add366c0        downloadhtmlfreecom_web2   "/venv/start.sh bash"    7 months ago        Up 2 hours                  0.0.0.0:1012->80/tcp                web2
2b4c4b6a5854        mysql:8.0.15               "docker-entrypoint.s…"   7 months ago        Up 2 hours                  33060/tcp, 0.0.0.0:1001->3306/tcp   database

Danh sách các Container đang hoạt động

TungNT:~ tungnt$ docker container ls
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                               NAMES
e19358882f52        downloadhtmlfreecom_web1   "/venv/start.sh bash"    7 months ago        Up 2 hours          0.0.0.0:1011->80/tcp                web1
f47f82d50d41        downloadhtmlfreecom_web3   "/venv/start.sh bash"    7 months ago        Up 2 hours          0.0.0.0:1013->80/tcp                web3
42c5add366c0        downloadhtmlfreecom_web2   "/venv/start.sh bash"    7 months ago        Up 2 hours          0.0.0.0:1012->80/tcp                web2
2b4c4b6a5854        mysql:8.0.15               "docker-entrypoint.s…"   7 months ago        Up 2 hours          33060/tcp, 0.0.0.0:1001->3306/tcp   database

Xem log Container đang hoạt động

tungnt@MacBook-Pro-cua-Nguyen-2 deploy % docker logs -f signoz-frontend
2024/10/17 07:33:02 [notice] 1#1: using the "epoll" event method
2024/10/17 07:33:02 [notice] 1#1: nginx/1.26.2
2024/10/17 07:33:02 [notice] 1#1: built by gcc 13.2.1 20240309 (Alpine 13.2.1_git20240309) 
2024/10/17 07:33:02 [notice] 1#1: OS: Linux 6.10.4-linuxkit
2024/10/17 07:33:02 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/10/17 07:33:02 [notice] 1#1: start worker processes
2024/10/17 07:33:02 [notice] 1#1: start worker process 6
2024/10/17 07:33:02 [notice] 1#1: start worker process 7
...

Khởi động lại các dịch vụ

docker-compose down
docker-compose up -d

Xoá Image

TungNT:~ tungnt$ docker rmi hello-world hello-world

Chạy 1 Image

TungNT:~ tungnt$ docker run ubuntu:18.04

Khởi tạo 1 Container từ 1 Image

TungNT:~ tungnt$ docker run -it ubuntu:18.04 /bin/bash
root@a8637dcd924d:/# uname
Linux

Rename Container

TungNT:docker-ubuntu-18.04 tungnt$ docker rename silly_lovelace kafka
TungNT:docker-ubuntu-18.04 tungnt$ docker start kafka
kafka
TungNT:docker-ubuntu-18.04 tungnt$ docker container ls
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                                          NAMES
7d28428756b4        ubuntu:18.04               "/bin/bash"              5 minutes ago       Up 8 seconds                                                       kafka
134fe9f385ba        docker-ubuntu-1804_web1    "/venv/start.sh bash"    11 days ago         Up 6 hours          0.0.0.0:1618->80/tcp, 0.0.0.0:1619->8888/tcp   docker-ubuntu-18.04-web1
e19358882f52        downloadhtmlfreecom_web1   "/venv/start.sh bash"    8 months ago        Up 6 hours          0.0.0.0:1011->80/tcp                           web1
f47f82d50d41        downloadhtmlfreecom_web3   "/venv/start.sh bash"    8 months ago        Up 6 hours          0.0.0.0:1013->80/tcp                           web3
42c5add366c0        downloadhtmlfreecom_web2   "/venv/start.sh bash"    8 months ago        Up 6 hours          0.0.0.0:1012->80/tcp                           web2
2b4c4b6a5854        mysql:8.0.15               "docker-entrypoint.s…"   8 months ago        Up 5 hours          33060/tcp, 0.0.0.0:1001->3306/tcp              database
TungNT:docker-ubuntu-18.04 tungnt$ docker exec -it kafka /bin/bash
root@7d28428756b4:/# 

Truy cập vào Container đang hoạt động

TungNT:docker-ubuntu-18.04 tungnt$ docker container ls
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                                          NAMES
134fe9f385ba        docker-ubuntu-1804_web1    "/venv/start.sh bash"    15 minutes ago      Up 15 minutes       0.0.0.0:1618->80/tcp, 0.0.0.0:1619->8888/tcp   docker-ubuntu-18.04-web1
e19358882f52        downloadhtmlfreecom_web1   "/venv/start.sh bash"    7 months ago        Up 9 hours          0.0.0.0:1011->80/tcp                           web1
f47f82d50d41        downloadhtmlfreecom_web3   "/venv/start.sh bash"    7 months ago        Up 9 hours          0.0.0.0:1013->80/tcp                           web3
42c5add366c0        downloadhtmlfreecom_web2   "/venv/start.sh bash"    7 months ago        Up 9 hours          0.0.0.0:1012->80/tcp                           web2
2b4c4b6a5854        mysql:8.0.15               "docker-entrypoint.s…"   7 months ago        Up 9 hours          33060/tcp, 0.0.0.0:1001->3306/tcp              database
TungNT:docker-ubuntu-18.04 tungnt$ docker exec -it 134fe9f385ba /bin/bash
root@134fe9f385ba:/venv# 

Chúng ta có thể thấy rằng chúng ta đã đăng nhập vào container thông qua dấu nhắc bash, hiện tại cho thấy thông tin root@134fe9f385ba.

Docker commit

Lệnh commit của Docker cho phép người dùng lấy một container đang chạy và lưu trạng thái hiện tại của nó dưới dạng một image.

TungNT:docker-ubuntu-18.04 tungnt$ docker exec -it 134fe9f385ba /bin/bash
root@134fe9f385ba:/venv# 
root@134fe9f385ba:/venv# exit;
TungNT:docker-ubuntu-18.04 tungnt$ sudo docker commit docker-ubuntu-18.04-web1 ubuntu-18.04:python  
sha256:a73908e09d840f534fe36b203a1ade254569f187201fb5056beef7f8f799f5d4
TungNT:docker-ubuntu-18.04 tungnt$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
ubuntu-18.04               python              a73908e09d84        38 seconds ago      937MB

Build Image, Repository và chạy Container mới build

docker run -p 3306:3306 --name name_for_container mysql
TungNT:Docker tungnt$ docker build -t blue-ubuntu .
TungNT:~ tungnt$ docker run -p 1009:80 blue-ubuntu /bin/bash
TungNT:~ tungnt$ docker run -it -p 1009:80 blue-ubuntu /bin/bash
TungNT:~ tungnt$ docker run -v /Users/tungnt/Documents/Docker/webroot:/var/www/html -p 1009:80 -it blue-ubuntu /bin/bash
 * Starting nginx nginx 
TungNT:~ tungnt$ docker pull mysql:5.7.27
TungNT:~ tungnt$ docker tag a2a15febcdf3 blue/ubuntu
TungNT:~ tungnt$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
blue-ubuntu         latest              dc4a62eca784        2 hours ago         152MB
mysql               5.7.27              383867b75fd2        2 days ago          373MB
ubuntu              18.04               a2a15febcdf3        4 weeks ago         64.2MB
blue/ubuntu         latest              a2a15febcdf3        4 weeks ago         64.2MB
TungNT:~ tungnt$ docker rmi ubuntu:18.04
Untagged: ubuntu:18.04
Untagged: ubuntu@sha256:d1d454df0f579c6be4d8161d227462d69e163a8ff9d20a847533989cf0c94d90
TungNT:~ tungnt$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
blue-ubuntu         latest              dc4a62eca784        2 hours ago         152MB
mysql               5.7.27              383867b75fd2        2 days ago          373MB
blue/ubuntu         latest              a2a15febcdf3        4 weeks ago  

Build lại container từ container đã có:

TungNT:docker-ubuntu-18.04 tungnt$ docker container ls
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                                          NAMES
7d28428756b4        ubuntu:18.04               "/bin/bash"              7 hours ago         Up 42 seconds                                                      kafka
TungNT:docker-ubuntu-18.04 tungnt$ docker stop kafka
kafka
TungNT:docker-ubuntu-18.04 tungnt$ docker commit kafka kafka-01
sha256:b7d8a6d29f0b5995069e7cb6fd01c61e84defa18f50040ee273971dd0dabc3a5
TungNT:docker-ubuntu-18.04 tungnt$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED              SIZE
kafka-01                   latest              b7d8a6d29f0b        About a minute ago   1.03GB
TungNT:docker-ubuntu-18.04 tungnt$ docker run -it -p 9082:9092 kafka-01 /bin/bash
root@946c12bcefa2:/# 
TungNT:docker-ubuntu-18.04 tungnt$ docker container ls
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                                          NAMES
946c12bcefa2        kafka-01                   "/bin/bash"              22 seconds ago      Up 20 seconds       0.0.0.0:9082->9092/tcp                         clever_shockley
TungNT:docker-ubuntu-18.04 tungnt$ docker rename clever_shockley kafka
TungNT:docker-ubuntu-18.04 tungnt$ docker container ls
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS              PORTS                                          NAMES
946c12bcefa2        kafka-01                   "/bin/bash"              About a minute ago   Up About a minute   0.0.0.0:9082->9092/tcp                         kafka

Copy file giữa container và host

tungnt@TungNT Downloads % docker cp 3z0cfixu.php test:/tmp
tungnt@TungNT Downloads % docker container cp test:/tmp/docker.txt .

Dockerfile

Tạo Dockerfile

TungNT:~ tungnt$ mkdir docker-ubuntu-18.04
TungNT:~ tungnt$ cd docker-ubuntu-18.04

Trong thư mục docker-ubuntu-18.04 tạo các file vs nội dung như sau:

TungNT:docker-ubuntu-18.04 tungnt$ tree
.
├── Dockerfile
├── README.md
├── default.conf
├── start.sh
├── webroot
│   └── index.php
└── www.conf
TungNT:docker-ubuntu-18.04 tungnt$ vim Dockerfile
FROM ubuntu:18.04
  
MAINTAINER TungNT<tungnt.blue@gmail.com>

RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata
RUN apt-get install policycoreutils -y
RUN apt-get install nginx -y
RUN apt-get install iputils-ping vim git software-properties-common -y
RUN add-apt-repository -y ppa:ondrej/php
RUN apt-get update
RUN apt-get install php7.3-fpm -y
RUN apt-get install php7.3-common php7.3-mysql php7.3-xml php7.3-xmlrpc php7.3-curl php7.3-gd php7.3-imagick php7.3-cli php7.3-dev php7.3-imap php7.3-mbstring php7.3-opcache php7.3-soap php7.3-zip php7.3-intl -y

COPY www.conf /etc/php/7.3/fpm/pool.d/www.conf

WORKDIR /venv

COPY start.sh /venv

RUN chmod a+x /venv/*

ENTRYPOINT ["/venv/start.sh"]

EXPOSE 80
TungNT:docker-ubuntu-18.04 tungnt$ vim start.sh 
#!/bin/bash
  
setsebool -P httpd_can_network_connect 1

rm /etc/nginx/sites-enabled/default

service nginx start
service php7.3-fpm start

git config --global user.name "TungNT"
git config --global user.email "tungnt.blue@gmail.com"

exec $@
TungNT:docker-ubuntu-18.04 tungnt$ vim www.conf
...

Sử dụng Dockerfile

TungNT:docker-ubuntu-18.04 tungnt$ docker build -t ubuntu-18.04-nginx .
Sending build context to Docker daemon  86.02kB
Step 1/17 : FROM ubuntu:18.04
 ---> 2ca708c1c9cc
Step 2/17 : MAINTAINER TungNT<tungnt.blue@gmail.com>
 ---> Running in b4efe5e08196
Removing intermediate container b4efe5e08196
 ---> e058bd69f18f
Step 3/17 : RUN apt-get update
...
TungNT:docker-ubuntu-18.04 tungnt$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
ubuntu-18.04-nginx         latest              1bd2262376f5        11 minutes ago      666MB

Tạo container từ image

docker run -v <forder_in_computer>:<forder_in_container> -p <port_in_computer>:<port_in_container> -it <image_name> /bin/bash

Trong đó:

  • -v : Thể hiện việc mount volume, dữ liệu từ thư mục từ máy thật có thể được truy cập từ thư mục của máy ảo.
  • -p: Cổng mạng từ máy thật để dẫn tới cổng mạng của máy ảo đang chạy.
  • -t: Chạy container và mở terminal bằng /bin/bash
TungNT:docker-ubuntu-18.04 tungnt$ docker run -p 1618:80 -it ubuntu-18.04-nginx /bin/bash
TungNT:docker-ubuntu-18.04 tungnt$ docker run -v /Sources/Docker/docker-ubuntu-18.04/webroot:/srv/www/test -p 1618:80 -it ubuntu-18.04-nginx /bin/bash

Docker Hub

Đăng nhập docker hub:

tungnt@TungNT Downloads % docker login --username=tungnt2411
Password: 
Login Succeeded

Cách 1:

tungnt@TungNT Downloads % docker container ls --all
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS                                          NAMES
d00227ec59d7        docker-blue_web     "/venv/start.sh bash"   3 days ago          Up 2 hours          80/tcp                                         test
199e6cbccf77        docker-mysql_web    "/venv/start.sh bash"   8 days ago          Up 2 hours          0.0.0.0:3306->3306/tcp, 0.0.0.0:2400->80/tcp   blue
tungnt@TungNT Downloads % docker container commit test blue-test           
sha256:3096f2047b719eb813f01447db5ca8e2e386335a711c80fd00adbf8089e29a0c
tungnt@TungNT Downloads % docker images                         
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
blue-test           latest              3096f2047b71        11 seconds ago      662MB
docker-blue_web     latest              bc8cdd9616a2        3 days ago          313MB
docker-mysql_web    latest              7564e62b1222        8 days ago          969MB
ubuntu              18.04               d27b9ffc5667        13 days ago         64.2MB
mysql               latest              be0dbf01a0f3        5 weeks ago         541MB
tungnt@TungNT Downloads % docker tag 3096f2047b71 tungnt2411/test:1.0.0
tungnt@TungNT Downloads % docker images                                
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
blue-test           latest              3096f2047b71        2 minutes ago       662MB
tungnt2411/test     1.0.0               3096f2047b71        2 minutes ago       662MB
docker-blue_web     latest              bc8cdd9616a2        3 days ago          313MB
docker-mysql_web    latest              7564e62b1222        8 days ago          969MB
ubuntu              18.04               d27b9ffc5667        13 days ago         64.2MB
mysql               latest              be0dbf01a0f3        5 weeks ago         541MB
tungnt@TungNT Downloads % docker push tungnt2411/test

Cách 2:

Đẩy thư mục code bên trên lên GitHub hoặc Bitbucket và vào https://hub.docker.com để import lên DockerHub. Sau đó pull vể như bình thường.

docker pull tungnt2411/docker-ubuntu-18.04:tagname

Và push 1 tag version lên bằng câu lệnh sau:

docker push tungnt2411/docker-ubuntu-18.04:tagname

Docker compose

Dùng để kết nối các container riêng lẻ với nhau

Cài đặt Docker-compose

Cấu trúc thư mục

https://docs.docker.com/compose/rails/

Thêm file docker-compose.yml để khai báo và điều phối hoạt động của các container trong project. Ví dụ:

version: '3'

services:

  web1:
    container_name: web1
    build:
      context: ./web1
      dockerfile: Dockerfile
    command: 'bash'
    restart: always
    ports:
      - 1618:80
    volumes:
      - ./webroot:/srv/www/test
    tty: true
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

Build docker compose

TungNT:docker-ubuntu-18.04 tungnt$ docker-compose build
Building web1
Step 1/18 : FROM ubuntu:18.04
 ---> 2ca708c1c9cc
Step 2/18 : MAINTAINER TungNT<tungnt.blue@gmail.com>
...

Run docker compose

TungNT:docker-ubuntu-18.04 tungnt$ sudo docker-compose up
Creating docker-ubuntu-18.04-web1 ... done
Attaching to docker-ubuntu-18.04-web1
docker-ubuntu-18.04-web1 | setsebool:  SELinux is disabled.
docker-ubuntu-18.04-web1 |  * Starting nginx nginx                       [ OK ] 
TungNT:docker-ubuntu-18.04 tungnt$ docker stop 14e00ac2bfd9
14e00ac2bfd9
TungNT:docker-ubuntu-18.04 tungnt$ docker-compose up -d
Starting docker-ubuntu-18.04-web1 ... done

Makefile

Thay vào phải gõ từng câu lệnh docker compose như trên, ví dụ:

docker-compose up -d mysql 
docker-compose up -d redis
docker-compose up -d worker 

Hoặc:

docker-compose up -d mysql redis worker

Chúng ta có thể viết vào trong Makefile:

up:
   docker-compose up -d mysql redis worker

Sau đó, chỉ cần:

  • Chạy make up để bật các tiến trình nền.
  • Chạy make dev để start project.
  • Chạy make test để test code trước khi gửi pull request.
tungnt@TungNT % make up
tungnt@TungNT % docker-compose up -d
tungnt@TungNT % docker-compose up --build

https://github.com/1sitevn/docker-ubuntu-18.04 https://hub.docker.com/repository/docker/tungnt2411/docker-ubuntu-18.04

TungNT:docker-ubuntu-18.04 tungnt$ tree
.
├── Dockerfile
├── Makefile
├── README.md
├── docker-compose.yml
├── start.sh
├── vhost.conf
├── webroot
│   └── index.php
└── www.conf

Docker Network

Tham khảo

development/system/docker.txt · Last modified: 2024/10/17 07:46 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki