当前位置 : 主页 > 编程语言 > java >

【Docker 】第 1 章

来源:互联网 收集:自由互联 发布时间:2022-06-23
​​https://www.bilibili.com/video/BV1og4y1q7M4?p=8spm_id_from=pageDriver​​ 文章目录 ​​1. 安装 Docker​​ ​​2. 底层原理​​ ​​2.1 Docker 如何工作?​​ ​​2.2 Docker 为什么比 VM 快?​​ ​​

​​https://www.bilibili.com/video/BV1og4y1q7M4?p=8&spm_id_from=pageDriver​​


文章目录

  • ​​1. 安装 Docker​​
  • ​​2. 底层原理​​
  • ​​2.1 Docker 如何工作?​​
  • ​​2.2 Docker 为什么比 VM 快?​​
  • ​​3. Docker 常用命令​​
  • ​​3.1 镜像命令​​
  • ​​3.2 容器命令​​

【Docker 】第 1 章_docker

  • 镜像:image,模板,容器通过其创建容器服务;
  • 容器:container,Docker 利用容器技术,独立运行一个或一组应用,通过镜像来创建;
  • 仓库:repository,存放镜像的地方;

1. 安装 Docker

ubuntu:​​curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun​​

第一次使用样例:

jiaming@ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally # 1. 本机寻找镜像未果
latest: Pulling from library/hello-world # 2. 下载
b8dfde127a29: Pull complete
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

第二次使用样例:

jiaming@ubuntu:~$ sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

未找到可用容器的结果:

jiaming@ubuntu:/etc/docker$ sudo docker run sajidjsaidjihfdshfsdhusisjidshusdhfisdjijiaosdjasdjkbfdjkbjkgghs
Unable to find image 'sajidjsaidjihfdshfsdhusisjidshusdhfisdjijiaosdjasdjkbfdjkbjkgghs:latest' locally
docker: Error response from daemon: pull access denied for sajidjsaidjihfdshfsdhusisjidshusdhfisdjijiaosdjasdjkbfdjkbjkgghs, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.jiaming@ubuntu:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 4 months ago 13.3kB

docker 默认资源路径:

jiaming@ubuntu:~$ sudo ls /var/lib/docker/
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes

卸载 docker:

sudo apt-get purge docker-ce
sudo rm -rf /var/lib/docker # 删除镜像、容器、配置文件等内容

镜像加速:

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

【Docker 】第 1 章_mysql_02

2. 底层原理

2.1 Docker 如何工作?

Docker 是一个 C/S 结构,Docker 的守护进程运行在主机上(服务端),通过 Socket 从客户端访问,DockerServer 接收到 Docker-Client 的指令,执行命令。

2.2 Docker 为什么比 VM 快?

  • Docker 比虚拟机有更少的抽象层;
  • Docker 共用一个内核,省去引导过程,响应速度极快;
  • 【Docker 】第 1 章_ubuntu_03

    3. Docker 常用命令

    jiaming@ubuntu:~$ sudo docker version
    jiaming@ubuntu:~$ sudo docker info
    jiaming@ubuntu:~$ sudo docker --help

    命令说明文档:https://docs.docker.com/engine/reference/commandline/docker/

    3.1 镜像命令

    • 查找;
    • 增加;
    • 删除;
    root@ubuntu:~# docker images
    REPOSITORY(仓库源) TAG(镜像标签) IMAGE ID(镜像的 ID) CREATED(镜像创建的时间) SIZE(镜像的大小)
    hello-world latest d1165f221234 4 months ago 13.3kB

    root@ubuntu:~# docker images --help

    Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]

    List images

    Options:
    -a, --all Show all images (default hides intermediate images) 列出所有镜像
    -q, --quiet Only show image IDs 只显示镜像的ID

    root@ubuntu:~# docker images -q
    d1165f221234root@ubuntu:~# docker search mysql
    NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    mysql MySQL is a widely used, open-source relation… 11113 [OK]
    mariadb MariaDB Server is a high performing open sou… 4213 [OK]
    mysql/mysql-server Optimized MySQL Server Docker images. Create… 825 [OK]
    mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88

    # 常用选项
    --filter=STARS=5000

    root@ubuntu:~# docker search mysql --filter=STARS=5000
    NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    mysql MySQL is a widely used, open-source relation… 11113 [OK]# 下载镜像 docker pull 镜像名[:tag]
    root@ubuntu:~# docker pull mysql
    Using default tag: latest # 如果不写tag,默认就是 latest
    latest: Pulling from library/mysql
    b4d181a07f80: Pull complete # 分层下载,docker image 的核心,联合文件系统
    a462b60610f5: Pull complete
    578fafb77ab8: Pull complete
    524046006037: Pull complete
    d0cbe54c8855: Pull complete
    aa18e05cc46d: Pull complete
    32ca814c833f: Pull complete
    9ecc8abdb7f5: Pull complete
    ad042b682e0f: Pull complete
    71d327c6bb78: Pull complete
    165d1d10a3fa: Pull complete
    2f40c47d0626: Pull complete
    Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b # 签名信息
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest # 真实地址

    # 等价
    docker pull mysql
    docker pull docker.io/library/mysql:latest

    # 指定版本下载
    root@ubuntu:~# docker pull mysql:5.7
    5.7: Pulling from library/mysql
    b4d181a07f80: Already exists # 无需重复下载
    a462b60610f5: Already exists
    578fafb77ab8: Already exists
    524046006037: Already exists
    d0cbe54c8855: Already exists
    aa18e05cc46d: Already exists
    32ca814c833f: Already exists
    52645b4af634: Pull complete
    bca6a5b14385: Pull complete
    309f36297c75: Pull complete
    7d75cacde0f8: Pull complete
    Digest: sha256:1a2f9cd257e75cc80e9118b303d1648366bc2049101449bf2c8d82b022ea86b7
    Status: Downloaded newer image for mysql:5.7
    docker.io/library/mysql:5.7

    root@ubuntu:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    mysql 5.7 09361feeb475 2 weeks ago 447MB
    mysql latest 5c62e459e087 2 weeks ago 556MB
    hello-world latest d1165f221234 4 months ago 13.3kBroot@ubuntu:~# docker rmi -f 镜像id # 删除指定的镜像
    root@ubuntu:~# docker rmi -f 镜像id 镜像id 镜像id 镜像id # 删除多个镜像
    root@ubuntu:~# docker rmi -f $(docker images -aq) # 删除全部镜像

    3.2 容器命令

    有了镜像才能创建容器;

    举例:​​docker pull centos​​

    # 创建容器并启动 docker run [options] image
    # 参数说明
    --name="Name" # 容器名字 tomcat01、tomcat02... 用来区分容器
    -d # 后台方式运行
    -i/-t # 使用交互方式运行,进入容器查看内容
    -p # 指定容器端口 -p 8080:8080
    -p 主机端口:容器端口
    -p 容器端口
    容器端口
    -P # 随机指定端口

    # 启动并进入容器
    root@ubuntu:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    centos latest 300e315adb2f 7 months ago 209MB
    root@ubuntu:~# docker run -it centos /bin/bash
    [root@f85583780089 /]# ls # 查看容器内的centos
    bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var# 退出命令
    [root@f85583780089 /]# exit
    exit

    # Ctrl+P+Q:容器不停止退出# 列出所有运行的容器
    docker ps 命令
    -a # 列出当前正在运行的容器+带出历史运行过的容器
    -n=? # 显示最近创建的容器
    -q # 只显示容器的编号
    root@ubuntu:~# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    root@ubuntu:~# docker ps -a # 历史运行过的容器
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    f85583780089 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago priceless_kirch
    80163dfdc558 d1165f221234 "/hello" 38 minutes ago Exited (0) 38 minutes ago quirky_banach
    cc8c663ebfe9 d1165f221234 "/hello" 6 days ago Exited (0) 6 days ago recursing_chandrasekhar
    319906d13cce d1165f221234 "/hello" 6 days ago Exited (0) 6 days ago mystifying_wilsondocker rm 容器id # 如果没有 -f 不能删除正在运行的容器
    docker rm -f $(docker ps -aq) # 删除所有容器# 启动和停止容器的操作
    docker start 容器id # 启动容器
    docker restart 容器id # 重启容器
    docker stop 容器id # 停止容器
    docker kill 容器id # 强制停止# 后台启动容器
    docker run -d centos # 无 -it 参数
    # 问题:docker ps, 发现 centos 停止了
    # 容器使用后台运行,必须要有一个前台进程,否则会自动停止# 查看日志
    root@ubuntu:~# docker run -d centos /bin/sh -c "while true; do echo "hello"; sleep 1;done"
    dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27
    root@ubuntu:~# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    dc3ea01151ae centos "/bin/sh -c 'while t…" 5 seconds ago Up 4 seconds youthful_driscoll
    root@ubuntu:~# docker logs -tf --tail 10 dc3ea01151ae # -tf 显示日志 --tail numbers 要显示的日志条数
    2021-07-12T14:18:30.602377140Z hello
    2021-07-12T14:18:31.605459945Z hello
    2021-07-12T14:18:32.609451241Z hello
    2021-07-12T14:18:33.612117664Z hello
    2021-07-12T14:18:34.615939548Z hello
    2021-07-12T14:18:35.619226781Z hello
    2021-07-12T14:18:36.622397637Z hello
    2021-07-12T14:18:37.625884377Z hello
    2021-07-12T14:18:38.627026141Z hello
    2021-07-12T14:18:39.632458006Z hello
    2021-07-12T14:18:40.634034443Z hello# 查看容器中进程信息 docker top 容器id
    root@ubuntu:~# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    dc3ea01151ae centos "/bin/sh -c 'while t…" 3 minutes ago Up 3 minutes youthful_driscoll
    root@ubuntu:~# docker top dc3ea01151ae
    UID PID PPID C STIME TTY TIME CMD
    root 78193 78171 0 22:18 ? 00:00:00 /bin/sh -c while true; do echo hello; sleep 1;done# 查看镜像的元数据 docker inspect 容器id
    root@ubuntu:~# docker inspect dc3ea01151ae
    [
    {
    "Id": "dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27",
    "Created": "2021-07-12T14:18:05.012922826Z",
    "Path": "/bin/sh",
    "Args": [
    "-c",
    "while true; do echo hello; sleep 1;done"
    ],
    "State": {
    "Status": "running",
    "Running": true,
    "Paused": false,
    "Restarting": false,
    "OOMKilled": false,
    "Dead": false,
    "Pid": 78193,
    "ExitCode": 0,
    "Error": "",
    "StartedAt": "2021-07-12T14:18:05.53219259Z",
    "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
    "ResolvConfPath": "/var/lib/docker/containers/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27/hostname",
    "HostsPath": "/var/lib/docker/containers/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27/hosts",
    "LogPath": "/var/lib/docker/containers/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27/dc3ea01151ae38347dcb38bf2ec88b64a57eb2ef2a8227bcee71d236a049fd27-json.log",
    "Name": "/youthful_driscoll",
    "RestartCount": 0,
    "Driver": "overlay2",
    "Platform": "linux",
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "docker-default",
    "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": {
    "LowerDir": "/var/lib/docker/overlay2/d3fa7836b22e75075302eba6f89b7654b51aa787d2504a5ed64801dd7de8e350-init/diff:/var/lib/docker/overlay2/e02bb848b82f04ac5607439ae38332836f1a9e0bce72eb3a52fd5068681303d5/diff",
    "MergedDir": "/var/lib/docker/overlay2/d3fa7836b22e75075302eba6f89b7654b51aa787d2504a5ed64801dd7de8e350/merged",
    "UpperDir": "/var/lib/docker/overlay2/d3fa7836b22e75075302eba6f89b7654b51aa787d2504a5ed64801dd7de8e350/diff",
    "WorkDir": "/var/lib/docker/overlay2/d3fa7836b22e75075302eba6f89b7654b51aa787d2504a5ed64801dd7de8e350/work"
    },
    "Name": "overlay2"
    },
    "Mounts": [],
    "Config": {
    "Hostname": "dc3ea01151ae",
    "Domainname": "",
    "User": "",
    "AttachStdin": false,
    "AttachStdout": false,
    "AttachStderr": false,
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    ],
    "Cmd": [
    "/bin/sh",
    "-c",
    "while true; do echo hello; sleep 1;done"
    ],
    "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": "ffa8d313c5198a5d2a4392aaa14a19a042978927947dd6d1e28da9e0e701635d",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {},
    "SandboxKey": "/var/run/docker/netns/ffa8d313c519",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "1c2b9d352d58c9ccfff44adbe40ac25df724c2e08582c18324bb25729811512d",
    "Gateway": "172.17.0.1",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "172.17.0.2",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "MacAddress": "02:42:ac:11:00:02",
    "Networks": {
    "bridge": {
    "IPAMConfig": null,
    "Links": null,
    "Aliases": null,
    "NetworkID": "71a935afb831cdd5873d6ed772edaed79c2a991227cccd7ae38496fc13488738",
    "EndpointID": "1c2b9d352d58c9ccfff44adbe40ac25df724c2e08582c18324bb25729811512d",
    "Gateway": "172.17.0.1",
    "IPAddress": "172.17.0.2",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "02:42:ac:11:00:02",
    "DriverOpts": null
    }
    }
    }
    }
    ]# 进入当前正在运行的容器 docker exec -it 容器id bashshell,开启一个新的终端
    root@ubuntu:~# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    774bb5d55750 centos "/bin/sh -c 'while t…" 5 seconds ago Up 4 seconds eager_haibt
    root@ubuntu:~# docker exec -it 774bb5d55750 /bin/bash
    [root@774bb5d55750 /]#

    # docker attach 容器id
    进入容器正在执行的终端# 容器拷贝文件到主机 docker cp 容器id:容器内路径 目的路径
    root@ubuntu:~# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    root@ubuntu:~# docker run -it centos /bin/bash
    [root@6da63bb2efb5 /]# root@ubuntu:~#
    root@ubuntu:~# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6da63bb2efb5 centos "/bin/bash" 16 seconds ago Up 15 seconds mystifying_joliot
    # 进入容器内部
    root@ubuntu:~# docker exec -it 6da63bb2efb5 /bin/bash
    [root@6da63bb2efb5 /]# cd home/
    # 在容器内部新建文件
    [root@6da63bb2efb5 home]# touch hello.txt
    [root@6da63bb2efb5 home]# ls
    hello.txt
    [root@6da63bb2efb5 home]# exit
    exit
    root@ubuntu:~# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6da63bb2efb5 centos "/bin/bash" About a minute ago Up About a minute mystifying_joliot
    # 将文件拷贝到主机
    root@ubuntu:~# docker cp 6da63bb2efb5:/home/hello.txt /home
    root@ubuntu:~# cd /home/
    root@ubuntu:/home# ls
    hello.txt

    # 拷贝是一个手动过程,可以使用 -v 卷技术实现同步

    【Docker 】第 1 章_ubuntu_04


    上一篇:【LiteOS】第 3 章
    下一篇:没有了
    网友评论