Docker學習筆記01

Docker學習筆記01

Docker學習html

  1. Docker的概述java

  2. Docker的安裝node

  3. Docker的命令mysql

    1. 鏡像命令linux

    2. 容器命令nginx

    3. 操做命令web

    4. 。。。sql

  4. Docker鏡像docker

  5. 容器數據卷json

  6. DockerFile

  7. Docker網絡原理

  8. IDEA整合Docker

  9. Docker Compose'

  10. Docker Swarm

  11. CI\CD Jenkins

Docker概述

Docker爲何會出現?

開發--線上,應用環境和業務環境--》本身的環境

須要各類環境配置

發佈項目(包括各類環境),項目和環境一塊兒打包遷移。

有些開發環境與其餘環境不能跨平臺。

傳統:jar包--》運維去部署

如今:開發--打包部署上線,一套流程作完。

Docker解決上面帶來的問題

java-- apk -- 發佈(應用商店) --使用APK --安裝便可

java --jar (環境)-- 打包項目帶上環境(鏡像)--(Docker倉庫:商店)--下載咱們發佈的鏡像--直接容許便可(鏡像機制)

Dockerd的思想就來自於集裝箱!

JRE-- 多個應用(端口衝突)--原來都是交叉的

隔離:Docker的核心思想!打包裝箱!每一個箱子都是相互隔離的

Docker經過隔離機制,能夠將服務器利用到極致.

本質:全部的技術都是由於出現了問題,咱們須要去解決,漕渠學習

Docker的歷史

2010,幾個IT年輕人,成立一家dotCloud

作一些pass的雲計算服務!LXC有關的容器技術!

Vmware的應用--笨重--

他們將本身的技術(容器化技術)命名, 就是Docker!

Docker剛剛誕生的時候並無引發行業的注意,dotCloud!--活不下去

開源

開發源代碼

2013年,Docker開源

Docker愈來愈多的人發現的Docker的優勢! 火力,Docker每月都會更新一個版本.

2014年4月9號,Docker 1.0發佈!

Docker爲啥這麼火, 十分輕巧

在容器技術出來以前,咱們使用的都是虛擬機技術.

虛擬機:在windows中裝一個Vmware,經過在這個軟件上虛擬出來一個單獨的電腦!至關與一個真實的電腦!笨重!

虛擬機也是一種虛擬化技術,Docker容器技術,也是虛擬化技術

 VM:linux centos原生鏡像(一個電腦)  隔離,須要開啓多個虛擬機 幾個G 幾分鐘
 docker:隔離,鏡像(最核心的環境 4m+jdk + mysql )十分的小巧,運行鏡像便可 小巧 各個M KB 秒級啓動

聊聊Docker

Docker 是基於GO語言開發的,開源項目

官網:https://www.docker.com/

文檔:https://docs.docker.com/

倉庫地址:https://hub.docker.com/

Docker能夠作什麼

以前的虛擬機:

缺點:

  1. 資源佔用很是多

  2. 冗餘步驟多

  3. 啓動很慢

容器化技術

比較Docker和虛擬機技術的不一樣

  1. 傳統的虛擬機,虛擬出來一套硬件,運行一個完整的操做系統,軟後在系統上安裝和運行軟件

  2. 容器內的應用直接運行在 宿主機上,容器是沒有本身的內核,也沒有虛擬的咱們的硬件,因此就輕便了

  3. 每一個容器間,是相互隔離的,每個容器都有一個屬於本身的文件系統,互不影響

DevOps(開發 運維)

應用跟快速的交付和部署

傳統:一堆幫助文檔,安裝程序

Docker:打包鏡像發佈測試,一鍵運行

跟邊界的升級和縮容

使用Docker以後,咱們部署應用就像搭積木同樣

項目打包一個鏡像,擴展 服務器A 服務器B

更簡單的系統運維

在容器化以後,咱們的開發,測試環境都是高度是一致的

跟高效的計算資源利用

Docker是 內核級別的虛擬化,能夠在一個物理機上能夠運行不少個容器實例,服務器的性能能夠被壓榨到極致

Docker的安裝

Docker的基本組成

鏡像(image):

docker鏡像就比如一個模板,能夠經過這個模板來建立容器服務,tomcat鏡像>run>tomcat01容器(提供服務的),經過這個鏡像能夠建立多個容器(最終服務運行或者項目就在容器中運行)

容器(container)

Docker利用容器技術,獨立運行一個或者多個組應用,經過鏡像來建立

啓動,中止,刪除,基本命令

目前就能夠把這個容器理解爲一個簡單的Linux系統

倉庫(repositor)

倉庫就是存放鏡像的地方!

倉庫分爲共有倉庫和私有倉庫

Docker Hub(默認是國外的)

阿里雲...都有容器服務器(配置鏡像加速 ! )

安裝Docker

環境的準備

  1. 須要會一點點的Linux基礎

  2. Centos的基礎

  3. 使用MobaXterm鏈接服務器進行操做!

環境的查看

 # 系統的內核是3.10以上的
 [root@localhost ~]# uname -r
 3.10.0-327.el7.x86_64 
 #系統的版本
 [root@localhost ~]# cat /etc/os-release
 NAME="CentOS Linux"
 VERSION="7 (Core)"
 ID="centos"
 ID_LIKE="rhel fedora"
 VERSION_ID="7"
 PRETTY_NAME="CentOS Linux 7 (Core)"
 ANSI_COLOR="0;31"
 CPE_NAME="cpe:/o:centos:centos:7"
 HOME_URL="https://www.centos.org/"
 BUG_REPORT_URL="https://bugs.centos.org/"
 ​
 CENTOS_MANTISBT_PROJECT="CentOS-7"
 CENTOS_MANTISBT_PROJECT_VERSION="7"
 REDHAT_SUPPORT_PRODUCT="centos"
 REDHAT_SUPPORT_PRODUCT_VERSION="7"

安裝

幫助文檔:https://docs.docker.com/engine/install/centos/

安裝文檔:http://www.javashuo.com/article/p-hkydhroo-ho.html

#Step1.檢查是否安裝過 docker :yum list installed|grep docker 
 #使用命令 yum -y remove docker-ce.x86_64  卸載
 #刪除存儲目錄
 # rm -rf /etc/docker
 # rm -rf /run/docker
 # rm -rf /var/lib/dockershim
 # rm -rf /var/lib/docker
 #Step2. 使用命令 uname-r 檢查centOs的內核版本 docker要求centOs的內核版本在3.10 以上
 #Step3. 移除舊版本:sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine
 #Step4. 安裝必要的系統工具:sudo yum install -y yum-utils device-mapper-persistent-data lvm2
#1.卸載舊的版本
  sudo yum remove docker \
                   docker-client \
                   docker-client-latest \
                   docker-common \
                   docker-latest \
                   docker-latest-logrotate \
                   docker-logrotate \
                   docker-engine
 #2.須要安裝的安裝包
  sudo yum install -y yum-utils
  
 #3.設置鏡像的倉庫(國外)
  sudo yum-config-manager \
     --add-repo \
     https://download.docker.com/linux/centos/docker-ce.repo  #默認是國外的
   
 sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  #推薦使用國內的鏡像資源.比較快
#建議事先 更新yum 的軟件包的索引
 [root@localhost ~]# yum makecache fast
#4.安裝docker相關的引擎 docker-ce 社區 ee企業版
 sudo yum install docker-ce docker-ce-cli containerd.io
 #安裝docker-ce : sudo yum -y install docker-ce 
#5.啓動Docker
 sudo systemctl start docker
 ​
 #6.使用Docker version來測試是否啓動成功
 docker version

  #7.hello-world
 docker run hello-world

 #8.查看一下 下載的這個hello-world鏡像

瞭解:卸載docker

 #1.卸載依賴
 yum remove docker-ce docker-ce-cli containerd.io
 ​
 #2.刪除資源
 rm -rf /var/lib/docker 
 ​
 # /var/lib/docker docker的默認工做路徑

阿里雲鏡像加速

  1. 登錄阿里雲地址;https://homenew.console.aliyun.com/home/dashboard/ProductAndService

    1. 找到容器鏡像服務

2.找到鏡像加速器

3.配置使用

 sudo mkdir -p /etc/docker
 ​
 sudo tee /etc/docker/daemon.json <<-'EOF'
 {
   "registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
 }
 EOF
 ​
 sudo systemctl daemon-reload
 ​
 sudo systemctl restart docker

回顧HelloWorld的流程

底層原理

Docker是怎麼工做的?

Docker是一個Client - Server 結構的系統,Docker的守護進程運行在主機上,經過Socket從客戶端訪問的.

DockerServer接收到Docker-Client的指令 , 就會執行這個命令!

Docker爲何比虛擬機Vmware 快?

  1. Docker有着比虛擬機更少的抽象層.

  2. docker利用的是宿主機的內核,vm須要是Guest OS

因此說,新建一個容器的時候,docker不須要像虛擬機同樣從新加載一個操做系統的內核.避免引導.虛擬機是加載Guest OS,分鐘級別的,而Docker是利用宿主機的操做須要,省略了這個複雜的過程.秒級!

Docker的經常使用的命令

幫助命令

docker version #顯示docker的詳細信息
docker info       #顯示docker的系統信息,包括鏡像和容器的數量
docker 命令 --help  #萬能命令,幫助文檔.

幫助文檔的地址:https://docs.docker.com/reference/

鏡像命令

docker images 查看全部本地的主機上的鏡像

[root@localhost yum.repos.d]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED      SIZE
hello-world   latest    d1165f221234   3 days ago   13.3kB

#解釋
REPOSITORY  鏡像的倉庫源
TAG            鏡像的標籤
IMAGE ID    鏡像的ID
CREATED        鏡像的建立時間
SIZE        鏡像的大小

#可選項
[root@localhost yum.repos.d]# docker images --help
Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
  -a, --all             Show all images (default hides intermediate images)#列出全部鏡像
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs        #只顯示鏡像的ID

docker search 搜索鏡像

[root@localhost]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10589     [OK]
mariadb                           MariaDB Server is a high performing open sou…   3963      [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   778 

#可選項,經過搜索來過濾
[root@localhost]# docker search mysql --help

Usage:  docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
      
--fileter=STARS=3000    #搜索出來的鏡像就是STARS大於30000的

docker pull 下載鏡像

#下載鏡像 doker pull 鏡像名字[:tag]
[root@localhost ~]# docker pull --help

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet                   Suppress verbose output
 
[root@localhost ~]# docker pull mysql
Using default tag: latest        #若是不寫 tag ,默認就是latest最高版本
latest: Pulling from library/mysql
a076a628af6f: Pull complete        #分層下載,docker images的核心 聯合文件系統
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Extracting  56.82MB/113.1MB
80539cea118d: Download complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c    #簽名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest    #真實地址

#等價
docker pull MySQL
dockers pull docker.io/library/mysql:latest

#指定版本下載
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi -f 刪除鏡像命令

[root@localhost ~]# docker rmi --help

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

[root@localhost ~]# docker rmi -f 鏡像的id  #刪除指定的容器
[root@localhost ~]# docker rmi -f i鏡像的id 容器的id 容器的id #刪除多個容器
[root@localhost ~]# docker rmi -f $(docker images -aq)  #刪除所有的容器

容器命令

說明:咱們有了鏡像才能夠建立容器,linux,下載一個centos 鏡像來測試學習

docker pull centos

新建容器並啓動

docker run [可選參數] image

#參數說明

--name="name"    容器名字 tomcat01 tomcat02,用來區分容器
-d                後臺方式運行
-it                使用交互方式運行,進入容器查看內容
-p                指定容器的端口 -p 8080:8080
    -p ip:主機端口:容器端口
    -p 主機端口:容器端口(經常使用)
    -p 容器端口
    容器端口
-P                隨機指定端口

#測試,啓動並進入容器
docker run -it centos /bin/bash
[root@localhost ~]# docker run -it centos /bin/bash
[root@69b33819a9fe /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
#從容器中退出主機
[root@69b33819a9fe /]# exit

列出全部的運行的容器

#docker ps 命令
    #列出當前正在運行的容器
-a    #列出當前正在運行的容器,和歷史運行過的容器
-n=?    #顯示最近建立的容器
-q        #顯示容器的編號
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                            PORTS     NAMES
69b33819a9fe   centos        "/bin/bash"   2 minutes ago   Exited (130) About a minute ago             gracious_lamarr
768d934f05a3   hello-world   "/hello"      38 hours ago    Exited (0) 38 hours ago                     great_davinci

退出容器

exit #直接容器退出並中止
Ctrl + P + Q    #退出不中止容器

刪除容器

docker rm 容器 id                    #刪除指定的容器,不能刪除正在運行的容器,若是要強制刪除 rm -f
docker rm -f $(docker ps -aq)     #刪除全部的容器
dockr ps -a -q|xargs docker rm     #刪除全部的容器

啓動和中止容器的操做

docker start 容器id     #啓動容器
docker stop 容器id     #中止當前正在運行的容器
docker restart 容器id #重啓容器
docker kill 容器id     #強制中止當前容器

經常使用的其餘命令

後臺啓動容器

#命令 docker run -d 鏡像名

[root@localhost ~]# docker run -d centos
# 問題docker ps ,發現 centos 中止了

#常見的坑:docker 容器使用後臺運行,就必需要有一個前臺進行,docker發現沒有應用,就會自動中止
#nginx,容器啓動後,發現本身沒有提供服務,就會馬上中止,就是沒有程序

查看日誌

docker log -f -t --tail 條數 鏡像id #容器,沒有日誌
[root@localhost ~]# docker logs --help
Usage:  docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m
                       for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g.
                       42m for 42 minutes)


#編寫一段腳本
while true;do echo cai007;sleep 1;done
[root@localhost ~]#docker run -d centos /bin/sh -c "while true;do echo cai007;sleep 1;done"

[root@localhost ~]# docker ps

#顯示日誌
--tf                 #顯示所有日誌
--tail number         #顯示日誌條數
[root@localhost ~]# docker logs -t -f --tail 10 300e315adb2f

查看容器中的進程信息ps

#命令 docker top 容器id
[root@localhost ~]# docker top 1b51c68d06bb
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7352                7332                0                   10:09               pts/0               00:00:00   

查看鏡像的元數據

#命令
docker inspect 容器id

#測試
[root@localhost ~]# docker inspect 1b51c68d06bb
[
    {
        "Id": "1b51c68d06bbf5a43c7a9b524ae7199b2ba15df66eb5038947962be55065084f",
        "Created": "2021-03-11T02:09:52.999448823Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 7352,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-03-11T02:09:55.056606873Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/1b51c68d06bbf5a43c7a9b524ae7199b2ba15df66eb5038947962be55065084f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/1b51c68d06bbf5a43c7a9b524ae7199b2ba15df66eb5038947962be55065084f/hostname",
        "HostsPath": "/var/lib/docker/containers/1b51c68d06bbf5a43c7a9b524ae7199b2ba15df66eb5038947962be55065084f/hosts",
        "LogPath": "/var/lib/docker/containers/1b51c68d06bbf5a43c7a9b524ae7199b2ba15df66eb5038947962be55065084f/1b51c68d06bbf5a43c7a9b524ae7199b2ba15df66eb5038947962be55065084f-json.log",
        "Name": "/intelligent_visvesvaraya",
        "RestartCount": 0,
        "Driver": "devicemapper",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "DeviceId": "29",
                "DeviceName": "docker-253:0-6807780-e119e0df693b087335ead2c23457a2a857d627c4281a25a24d09d468d0b08a44",
                "DeviceSize": "10737418240"
            },
            "Name": "devicemapper"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "1b51c68d06bb",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201204",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "2cafed0eecee28eaca661731315b18d65cc9bee2494f4ea6fb0ec676a6e2e8fe",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/2cafed0eecee",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "f6a077c2e09ea018b0d5957974756564e415f5ea23ee96a5d60ff7e3e1b3f76d",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "29ac9086e0a2908eb23107631795ffac3b3b402aa631aa4e7d157c21ea0e3b84",
                    "EndpointID": "f6a077c2e09ea018b0d5957974756564e415f5ea23ee96a5d60ff7e3e1b3f76d",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]

進入當前正在運行的容器

#咱們一般容器都是使用後臺方式運行的,須要進入容器,修改一些配置

#命令
docker exec -it 容器id /bin/bash

#測試
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
1b51c68d06bb   centos    "/bin/bash"   12 minutes ago   Up 12 minutes             intelligent_visvesvaraya
9b5fc811d7e5   centos    "/bin/bash"   12 minutes ago   Up 12 minutes             strange_dijkstra
[root@localhost ~]# docker exec -it 9b5fc811d7e5 /bin/bash
[root@9b5fc811d7e5 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@9b5fc811d7e5 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 02:09 pts/0    00:00:00 /bin/bash
root         16      0  0 02:22 pts/1    00:00:00 /bin/bash
root         32     16  0 02:23 pts/1    00:00:00 ps -ef

#方式2
docker attach 容器id
[root@9b5fc811d7e5 /]#docker attach 1b51c68d06bb
正在執行的當前代碼...

#docker exec        #進入容器後開啓一個新的終端,能夠在裏面操做(經常使用)
#docker attach         #進入容器正在執行的終端,不會啓動新的進程

從容器內拷貝到主機上

docker cp 容器id:容器內路徑 目的的主機路徑

#查看當前主機目錄下
[root@localhost ~]# ls
123.java  123.txt  anaconda-ks.cfg  Desktop  workspace  公共  模板  視頻  圖片  文檔  下載  音樂
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
9b5fc811d7e5   centos    "/bin/bash"   26 minutes ago   Up 26 minutes             strange_dijkstra
#進入docker容器內部
[root@localhost ~]# docker exec -it 9b5fc811d7e5  /bin/bash
[root@9b5fc811d7e5 /]# cd /home/
#在容器內新建一個文件
[root@9b5fc811d7e5 home]# touch test.java
[root@9b5fc811d7e5 home]# ls
test.java
[root@9b5fc811d7e5 home]# read escape sequence
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
9b5fc811d7e5   centos    "/bin/bash"   27 minutes ago   Up 27 minutes             strange_dijkstra
#將這個文件拷貝出來到主機上
[root@localhost ~]# docker cp 9b5fc811d7e5:/home/test.java /home
[root@localhost ~]# cd /home/
[root@localhost home]# ls
cai  test.java  user1

#拷貝是一個手動的過程,後面可使用 -v 卷的技術,能夠實現主機與容器打通

小結

練習

Docker 安裝 Nginx

#1.搜索鏡像    search  建議去docker官方文檔查看
[root@localhost home]# docker search nginx
#2.下載鏡像 pull
[root@localhost home]# docker pull nginx

#3.運行測試
[root@localhost home]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 days ago     13.3kB
mysql         latest    c8562eaf9d81   7 weeks ago    546MB
nginx         latest    f6d0b4767a6c   8 weeks ago    133MB
centos        latest    300e315adb2f   3 months ago   209MB
# -d 後臺運行
# --name 給容器命名
# -p 宿主機端口:容器內端口
[root@localhost home]# docker run -d --name nginx01 -p 3344:80 nginx
f293c3bb0fe40120c65988da96691662a419d14e18e7560cc4177482df7f9cb5
[root@localhost home]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
f293c3bb0fe4   nginx     "/docker-entrypoint.…"   12 seconds ago   Up 8 seconds    0.0.0.0:3344->80/tcp   nginx01
9b5fc811d7e5   centos    "/bin/bash"              46 minutes ago   Up 46 minutes                          strange_dijkstra
[root@localhost home]# curl localhost:3344

#進入容器
[root@localhost home]# docker exec -it f293c3bb0fe4 /bin/bash
root@f293c3bb0fe4:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@f293c3bb0fe4:/# cd /etc/nginx/
root@f293c3bb0fe4:/etc/nginx# ls
conf.d          koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params  koi-win  modules     scgi_params  win-utf

端口暴露的概念

思考問題:咱們每次改動nginx配置文件,都須要進入容器內部,十分麻煩,咱們要是能夠在容器外部提供一個端口映射路徑,到達在容器修改文件名,容器內部就能夠自動修改? -v 數據卷!

Docker 安裝Tomcat

#官方的使用
docker run -it --rm tomcat:9.0

#咱們以前的啓動都是後臺,中止容器以後,容器仍是能夠查看的到的 docker run -it --rm ,通常用來測試,用完即刪

#正常方法(下載在啓動)
docker pull tomcat

#啓動運行
docker run -d -p 3377:8080 --name tomcat01 tomcat

#測試訪問沒有問題
進入容器
[root@localhost ~]# docker exec -it tomcat01 /bin/bash

#發現問題:一、linux命令少了。二、沒有webapps  阿里雲鏡像的緣由。默認是最小鏡像,全部沒必要要的都被剔除

#保證最小可運行的環境
[root@localhost ~]# docker run -d -p 3377:8080 --name tomcat01 tomcat
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-port=3377/tcp

思考問題:咱們之後要部署項目,若是每次都要進入容器是否是很是的麻煩?咱們要是在容器外提供一個映射路徑,webapps,咱們在外部放置項目,就自動同步到內部就行了。

部署es+kibana

#es 暴露的端口不少
#es 十分耗內存
#es 的數據通常須要放置到安全目錄!掛載
# --net somenetwork ? 網絡配置

#啓動 elasticsearch
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2

#啓動 Linux就卡住了
docker stats 
#查看docker cpu 的狀態

#es 是十分號內存的 1.xG 

#查看 docker stats 

#測試一下es 是否成功

#關閉,增長內存的限制,修改配置文件, -e 環境配置修改
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2

使用:使用kibana鏈接es

相關文章
相關標籤/搜索