Author Topic: เริ่มต้นด้วยการลง docker ใน ubuntu , docker command , docker-compose  (Read 12871 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
dockerคืออะไร-ใช้ทำอะไร-มีประโยชน์อย่างไร

สำหรับคนที่เคยทำระบบพวก unix หรือ linux มา แล้วมีพวก application ลงอยู่ในระบบ หรือจะเป็น programmer ที่ต้องการใช้งาน โปรแกรมที่รันด้วยภาษา python , php , java เองก็ตาม เวลาที่เราทำการสร้างระบบ ขึ้นมา เช่น เว็บเซิฟเวอร์ที่รองรับการทำงาน web app ที่รันจาก php , แล้วลง webserver เป็น nginx หรือ apache ก็ตาม ปกติเราก็ต้อง ลง os กับเซิฟเวอร์ โหลด .iso มาลง แล้วทำการติดตั้ง nginx ไม่ก็ apache  แล้วก็ติดตั้งภาษา lang : php หรือ python หรือ java  เป็นต้น

ถ้ามีหลายระบบก็ต้องลงทีละระบบ อาจจะกินเวลาในการติดตั้งเป็นอาทิตย์ กว่าจะได้เริ่มเทสระบบจริงๆ  ซึ่งจะเป็นการเพิ่มขั้นตอน และความสับซ้อนมากขึ้น ครับ ซึ่งตรงจุดนี้เอง docker ได้เข้ามีบทบาทในการแก้ไขปัญหาในจุดนี้  ลดความซับซ้อนในการเตรียมระบบ ซึ่งจะมี images ที่เหล่านักพัฒนานั้นทำไว้ หรือเราสามารถเป็น images แล้วรวมเป็นชุด container ของบริษัทเราเอง แล้วเราสามารถ pull  images เหล่านั้นของเรามาใช้งานได้เลย ซึ่งถ้าทำเสร็จแล้ว สามารถ นำ image นี้ไป ลงต่อในเครื่อง Production หรือ เครื่องที่จะเป็น DR-site ได้เลย

ข้อดีของ docker อีกข้อคือ สามารถรองรับการทำงานได้ทั้ง Window OS , Mac OS หรือ Linux OS ซึ่งถ้าใช้คุ้นเคยการทำงานกับ text mode อยู่แล้วก็สามารถลง docker กับเครื่อง linux ได้ เช่นลงบน ubuntu
หรือถ้าท่านใดใช้ mac ก็สามารถติดตั้งโปรแกรม docker ใน mac ได้เลยครับผม
แค่นี้เราก็สามารถ connect เครื่อง client ของเรา กับ docker ได้ละครับ : )


เริ่มต้นด้วยการลง docker ใน ubuntu
https://docs.docker.com/engine/installation/linux/ubuntulinux/

เบื้องต้นสามารถรันใช้งาน docker โดยใช้ user ที่ไม่ใช่ root แต่ต้องเพิ่ม user ให้อยู่ใน group docker  ได้ตามนี้
Create the docker group.
$ sudo groupadd docker
Add your user to docker group.
$ sudo usermod -aG docker $USER

แค่นี้ก็จะใช้งาน docker โดยไม่ต้องใส่ sudo แล้วครับ เช่น

เป็นการเรียก image hello-world มาแสดง
$docker run hello-world

จะทำการโหลด image ubuntu ขึ้นมาให้
$docker run -it ubuntu /bin/bash

ทำการเชคดู process ที่เคยรันใน docker
$docker ps -a

จะทำการ exec เข้าไปยัง image ubuntu ที่ทำเอาไว้แล้ว
$docker exec -it ubuntu /bin/bash

หรือถ้าทราบเลข docker id จากการรัน docker ps ก็ทำการ exec เข้าไปยัง docker id = dfwee222ssis ที่ทราบได้ครับ
$docker exec -it dfwee222ssis /bin/bash
« Last Edit: พฤศจิกายน 04, 2019, 07:51:07 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: เริ่มต้นด้วยการลง docker ใน ubuntu
« Reply #1 on: กุมภาพันธ์ 07, 2017, 11:15:44 AM »
ปกติแล้วถ้าเว็บเซิฟเวอร์ ทำให้รองรับ jsp จะใช้งานเป็น Jboss ไม่ก็ TomCat ครับ
ก็ขึ้นอยู่กับว่า environment เป็นอย่างไรก็เลือกใช้ตามนั้นนะครับผม

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: เริ่มต้นด้วยการลง docker ใน ubuntu
« Reply #2 on: พฤศจิกายน 04, 2019, 07:48:57 PM »
The cp command can be used to copy files.

One specific file can be copied TO the container like:

docker cp foo.txt mycontainer:/foo.txt
One specific file can be copied FROM the container like:

docker cp mycontainer:/foo.txt foo.txt
For emphasis, mycontainer is a container ID, not an image ID.

Multiple files contained by the folder src can be copied into the target folder using:

docker cp src/. mycontainer:/target
docker cp mycontainer:/src/. target

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: เริ่มต้นด้วยการลง docker ใน ubuntu
« Reply #3 on: พฤศจิกายน 04, 2019, 07:50:49 PM »
###check docker ps process name and container id
docker ps -a

CONTAINER ID        IMAGE                                                             COMMAND                  CREATED             STATUS                   PORTS                                                                                      NAMES
acffaed1ba38        dhub-dev.deepinstinctweb.com/dimgmt/appliance-nginx:2.2.1.32      "/bin/sh -c 'envsubs…"   7 weeks ago         Up 7 weeks               0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4339->4339/tcp, 0.0.0.0:4431->4431/tcp   deepinstinct_nginx_1
0e389fa58458        dhub-dev.deepinstinctweb.com/dimgmt/application:2.2.1.32          "celery -P gevent --…"   7 weeks ago         Up 7 weeks                                                                                                          deepinstinct_dynamic_groups_service_1
##exec into docker name
docker exec deepinstinct_nginx_1 cat /etc/hosts

##delete container id
docker rm container_ID

##docker run attach and detach
docker run dhub-dev.deepinstinctweb.com/dimgmt/appliance-nginx:2.2.1.32
docker run -d dhub-dev.deepinstinctweb.com/dimgmt/appliance-nginx:2.2.1.32
docker attach id

###docker exec into container_name
docker exec -it deepinstinct_nginx_1 /bin/bash

##Second, you can easily start a stopped container running:
$ docker start container_name
##Once the container has been started, you can run your command by:
$ docker exec -it container_name bash -c "mycommand"
###docker run properly command in container_name
docker-compose run web /bin/bash

###docker check disk space
docker system df


###You can restart an existing container after it exited and your changes are still there.
docker start  `docker ps -q -l` # restart it in the background
docker attach `docker ps -q -l` # reattach the terminal & stdin

##Create a container to run it later on with required image.
docker create --name <container-name> <image-name>
###Run docker container
###Run the docker container with the required image and specified command / process. ‘-d’ flag is used for running the container in background.
docker run -it -d --name <container-name> <image-name> bash


=====The cp command can be used to copy files.
One specific file can be copied TO the container like:
docker cp foo.txt mycontainer:/foo.txt
One specific file can be copied FROM the container like:
docker cp mycontainer:/foo.txt foo.txt
For emphasis, mycontainer is a container ID, not an image ID.
Multiple files contained by the folder src can be copied into the target folder using:
docker cp src/. mycontainer:/target
docker cp mycontainer:/src/. target

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
docker-compose : การเขียนสคิปเพื่อทำการ automated application เช่นเราเขียนเป็นสคิปเพื่อติดตั้ง LAMP ในระบบก็เขียนใส่ docker-compose.yaml ได้ว่าติดตั้ง apache , php , php-extension , mariadb (database) ตั้งกำหนด path ของ document root หรือ ตั้งรหัส login mysql user อะไร เขียนคอนฟิคใส่ในไฟล์ได้เลย

#cd /home/ubuntu
#mkdir testpkl
#cd testpkl
#vi docker-compose.yaml

====docker-compose.yaml AMP : Apache + Mariadb + Phpmyadmin ====
 version: '3'
services:
    php-apache:
        image: php:7.2.1-apache
        ports:
            - 80:80
        volumes:
            - ./DocumentRoot:/var/www/html:z
        links:
            - 'mariadb'

    mariadb:
        image: mariadb:10.1
        volumes:
            - mariadb:/var/lib/mysql
        environment:
            TZ: "Asia/Bangkok"
            MYSQL_ALLOW_EMPTY_PASSWORD: "no"
            MYSQL_ROOT_PASSWORD: "rootpwd"
            MYSQL_USER: 'testuser'
            MYSQL_PASSWORD: 'testpassword'
            MYSQL_DATABASE: 'testdb'

volumes:
    mariadb:
====docker-compose.yaml AMP : Apache + Mariadb + Phpmyadmin ====

ทำการสร้างโดย
#docker-compose up -d

ถ้าจะให้ดีทำการเปิด tmux ไว้ด้วย หลังจากนั้นก็สามารถเชค docker ps ได้
#docker ps -a

แล้วเข้าสู่ container id นั้น
#docker exec -it container_id /bin/bash

หรือจะทำการ copy file จาก host เข้าไปที่ docker ก็
#cp index.php container_id:/root/.
« Last Edit: พฤศจิกายน 04, 2019, 08:00:00 PM by golfreeze »