IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Docker基础 -> 正文阅读

[系统运维]Docker基础

Docker概述

hub.docker.io

docs.docker.io

Docker安装

Docker

aldsfjasfkjsd

#/bin/bash

Docker的常用命令

帮助命令

镜像命令

容器命令

docker pull centos

新建容器并启动

docker run [args] image

# 参数说明
--name='Name'     容器名字 tomcat01, tomcat02...
-d                后台方式运行
-it               使用交互方式运行,并进入容器查看内容
-p                指定容器的端口 -p 8080:8080
     -p 主机端口:容器端口(常用)
     -p 容器端口
     -p ip:主机端口:容器端口
-P                随机指定端口



# 测试,启动并进入容器
[will@will ~]# docker run -it centos /bin/bash

列出所有的运行的容器

# docker ps
# docker ps -a
# docker ps -a -n=1
# docker ps -aq

停止容器

exit  # 停止容器并退出
ctrl + p + q # 不停止容器

删除容器

docker rm [container_id]  # 删除指定的容器,不能删除正在运行的容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -aq | xargs docker rm # 删除所有容器

启动和停止容器

docker start container_id
docker restart container_id
docker stop container_id
docker kill container_id

常用其他命令

后台启动容器

# 命令  docker run -d image_id
docker run -d centos
# 问题:docker ps 时发现centos停止了,因为docker容器使用后台

查看日志

docker logs
--tail

# 测试打印日志
docker run -d centos -c "while true;do echo 'hello world'; sleep 2; done"
[will@will ~]$ docker run -d centos /bin/sh -c "while true;do echo "hello world" ;sleep 1;done"
ab5e232e8ac9e05ee46945da5a4a2d2055b306953653a3fa44558c90f6d39450
[will@will ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
a7d1d775a89f   centos    "/bin/sh -c 'while t…"   6 seconds ago   Up 5 seconds             pensive_agnesi
[will@will ~]$ docker logs -f -t --tail 10 a7d1d775a89f
2021-09-12T14:06:53.978067719Z hello world
2021-09-12T14:06:54.979931412Z hello world
2021-09-12T14:06:55.981915303Z hello world
2021-09-12T14:06:56.984199095Z hello world
2021-09-12T14:06:57.986561111Z hello world
2021-09-12T14:06:58.987869560Z hello world
2021-09-12T14:06:59.989511714Z hello world
2021-09-12T14:07:00.990942246Z hello world
2021-09-12T14:07:01.992878156Z hello world
2021-09-12T14:07:02.994963108Z hello world
2021-09-12T14:07:03.997359975Z hello world
2021-09-12T14:07:04.999525036Z hello world
2021-09-12T14:07:06.001806035Z hello world

查看容器中进程信息ps

docker top container_id
[will@will ~]$ docker start a7d1d775a89f
a7d1d775a89f
[will@will ~]$ 
[will@will ~]$ 
[will@will ~]$ docker top a7d1d775a89f
UID                 PID                 PPID                C                   STIME               TTY                 TIME     
root                44527               44505               1                   22:10               ?                   00:00:00
root                44591               44527               0                   22:10               ?                   00:00:00
[will@will ~]$ 

查看镜像元数据

[will@will ~]$ docker inspect a7d1d775a89f
[
    {
        "Id": "a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33",
        "Created": "2021-09-12T14:06:39.641849451Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo 'hello world' ;sleep 1;done"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 137,
            "Error": "",
            "StartedAt": "2021-09-12T14:10:39.613653276Z",
            "FinishedAt": "2021-09-12T14:11:54.47701037Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33/hostname",
        "HostsPath": "/var/lib/docker/containers/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33/hosts",
        "LogPath": "/var/lib/docker/containers/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33/a7d1d775a89f9a6a620e66ed3236c7a8531376fbaa4aefa17b3381977406ae33-json.log",
        "Name": "/pensive_agnesi",
        "RestartCount": 0,
        "Driver": "overlay2",
        "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": {
                "LowerDir": "/var/lib/docker/overlay2/69e963790c3628fc36bfca9ce352266757ec7b9d4bcfac2eebd411fd92297bc0-init/diff:/var/lib/docker/overlay2/e88c6a2fc23c05ec9c082eaee933e458bcc746d1db9820739e790862372c71b9/diff",
                "MergedDir": "/var/lib/docker/overlay2/69e963790c3628fc36bfca9ce352266757ec7b9d4bcfac2eebd411fd92297bc0/merged",
                "UpperDir": "/var/lib/docker/overlay2/69e963790c3628fc36bfca9ce352266757ec7b9d4bcfac2eebd411fd92297bc0/diff",
                "WorkDir": "/var/lib/docker/overlay2/69e963790c3628fc36bfca9ce352266757ec7b9d4bcfac2eebd411fd92297bc0/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "a7d1d775a89f",
            "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 world' ;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": "3ad38981c09954a7773da16ef2c3c4c1aa08a65dbb25a44fc0e4ca239982ceab",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/3ad38981c099",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "71a309b12f1d882f7a0077b54a2893de036d1bc97519bdb707ad1505585d0ce1",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

进入当前正在运行的命令

# 我们通常都是使用后台方式进入容器的,需要进入容器,修改一些配置:
docker exec container_id

[will@will ~]$ docker exec -it a7d1d775a89f /bin/bash
[root@a7d1d775a89f /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@a7d1d775a89f /]#

docker attach container_id # 进入容器正在执行的终端,不会启动新的进程
docker exec container_id   # 进入容器后开启一个新的终端,可以在里面操作(常用)

从容器内拷贝文件到主机上

docker cp 容器id:容器内路径  目的主机路径  # 与scp命令非常相似,同时也支持从主机到容器的拷贝

小结

[will@will ~]$ docker

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/home/will/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/home/will/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/will/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/will/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Build with BuildKit (Docker Inc., v0.6.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.8.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container # 当前shell下连接指定运行镜像
  build       Build an image from a Dockerfile                                              # 通过dockerfile定制镜像
  commit      Create a new image from a container's changes                                 # 提交当前容器为新的镜像
  cp          Copy files/folders between a container and the local filesystem               # 从容器和宿主机相互拷贝指定文件或者目录
  create      Create a new container                                                        # 创建一个新的容器,同run,但不启动容器
  diff        Inspect changes to files or directories on a container's filesystem           # 查看docker容器变化
  events      Get real time events from the server                                          # 从docer服务获取容器实时事件
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers                                  # 停止容器
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE                # 给源中的镜像打标签
  top         Display the running processes of a container                         # 查看容器中
  unpause     Unpause all processes within one or more containers                  # 取消暂停容器
  update      Update configuration of one or more containers
  version     Show the Docker version information                                  # 查看docker版本号
  wait        Block until one or more containers stop, then print their exit codes # 截取容器停止时的退出状态值

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/

作业练习

docker安装nginx

# 1、搜索镜像 search 建议去hub.docker.com搜索,可以看到帮忙文档
# 2、下载镜像 pull
docker pull nginx
# 3、运行测试
docker run -d --name nginx01 -p 3344:80 nginx

docker安装tomcat

docker pull tomcat:9.0.52-jdk8-corretto
# 官方的使用
docker run -it -rm tomcat:9.0.52-jdk16-corretto  # 启动时新建一个容器,停止时自动删除这个容器docker run -it -p 8080:8080 -rm tomcat:9.0.52-jdk16-corretto


# 正常使用
docker run -d -p 8080:8080 --name tomat01 tomcat

# 测试访问没有问题


# 进入容器
docker exec -it tomcat01 /bin/bash

# 发现问题:linux命令很少,没有webapps。 原因:阿里云的镜像是最小镜像,所有不必要的都剔除掉了。
# 最小镜像:保证能运行的最小镜像!

部署es+kibana

# 问题:
# es暴露的端口非常多! 十分消耗内存 数据一般放到安全目录
# --net somenetwork ?网络配置

docker run -d --name elasticsearch -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2
  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-09-14 13:47:18  更:2021-09-14 13:48:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 17:02:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码