2020-06-06 docker基础命令-1

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 http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新软件包索引

yum makecache fast

4安装doceker-ce的版本

sudo yum install docker-ce docker-ce-cli containerd.io

5. 启动docker

sudo systemctl start docker

6. 查看docker版本

docker version

7. docker run hello-world

docker run hello-world
截屏2020-06-06 下午1.44.11.png

8. 查看镜像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB

了解: 卸载docker

  • 卸载依赖
    Uninstall the Docker Engine, CLI, and Containerd packages:
sudo yum remove docker-ce docker-ce-cli containerd.io
  • 删除资源
    Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
sudo rm -rf /var/lib/docker

7 阿里云镜像加速

8. Run的流程和Docker原理

Run的流程.

  1. 开始-->Docker会在本机寻找镜像---->判断本机是否有这个镜像---->有就运行/没有就去Docker Hub上下载.
  2. 找不到返回错误/找到下载.

9 镜像的基本命令

  • docker images 查看所有本地的主机上的镜像
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              30f937e841c8        2 weeks ago         541MB
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB
# 解释
REPOSITORY 镜像的仓库
TAG                 镜像的标签
IMAGE ID        镜像的创建时间
CREATED       镜像的创建时间
SIZE                镜像的大小
docker images --help
Options:    可选项
  -a, --all             Show all images (default hides intermediate images)     列出所有镜像
  -f, --filter filter   Filter output based on conditions provided
  -q, --quiet           Only show numeric IDs                                                 只显示镜像的ID
  • docker search 搜索镜像
[root@localhost ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9587                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3486                [OK]                
# 可选项,通过搜索来过滤
--filter=STARS=3000   #搜索出来镜像大于3000STARS

[root@localhost ~]# docker search mysql --filter=STARS=3000 
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   9587                [OK]                
mariadb             MariaDB is a community-developed fork of MyS…   3486                [OK]  
  • docker pull 下载镜像
# 下载镜像docker pull 镜像名[:tag]
[root@localhost ~]# docker pull mysql
Using default tag: latest             如果不写tag, 默认是最新的lastest
latest: Pulling from library/mysql
afb6ec6fdc1c: Pull complete    # 分层下载, docker image 的核心 联合文件系统
0bdc5971ba40: Pull complete 
97ae94a2c729: Pull complete 
f777521d340e: Pull complete 
1393ff7fc871: Pull complete 
a499b89994d9: Pull complete 
7ebe8eefbafe: Pull complete 
597069368ef1: Pull complete 
ce39a5501878: Downloading  61.31MB/111.5MB
7d545bca14bf: Download complete 
ce39a5501878: Pull complete 
7d545bca14bf: Pull complete 
211e5bb2ae7b: Pull complete 
5914e537c077: Pull complete 
Digest: sha256:a31a277d8d39450220c722c1302a345c84206e7fd4cdb619e7face046e89031d
Status: Downloaded newer image for mysql:latest   
docker.io/library/mysql:latest   # 真实地址
  • 镜像删除
docker rmi -f  镜像id
docker rmi -f $(docker images -aq)

10 容器命令

说明: 我们有镜像才可以创建容器,Linux, 下载一个centos镜像来测试学习.

docker pull centos
  • 新建容器并启动
[root@localhost ~]# docker run --help
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
# 参数说明
--name="Name"      容器名字     tomcat01    tomcat02 ,用来区分容器
-d                            后台方式运行
-it                            使用交互方式运行,进入容器查看内容
-p                            指定容器的端口      -p     8080:8080
             -p   ip:主机端口:容器端口
             -p   主机端口:容器端口
             -p   容器端口
            容器端口
-p                           随机指定端口        
  • 交互模式启动容器
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              30f937e841c8        2 weeks ago         541MB
centos              latest              470671670cac        4 months ago        237MB
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB
[root@localhost ~]# docker run -it centos /bin/bash
[root@5785165be7dc /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@5785165be7dc /]# exit      # 退出
exit
  • 列出所有运行的docker
# docker ps 命令
-a    # 正在和历史运行的容器
-n=?     # 显示最近创建的容器
-q         # 只显示最近创建容器id

[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
5785165be7dc        centos              "/bin/bash"         2 minutes ago       Exited (0) About a minute ago                       interesting_liskov
55f7f3143e21        hello-world         "/hello"            4 hours ago         Exited (0) 4 hours ago                              frosty_carson
[root@localhost ~]# 
  • 退出容器
    ctrl+p+q #容器不停止退出.
    exit # 直接容器停止退出.
[root@localhost ~]# docker run -it centos /bin/bash
[root@232c92712de3 /]# 
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
232c92712de3        centos              "/bin/bash"         12 seconds ago      Up 11 seconds                           nervous_nightingale
  • 删除容器
docker rm   容器id       # 不能删除正在运行的容器,  如果要删除需要rm -f
docker rm   -f $(docker ps -aq)
docker ps -a -q |xargs docker rm    # 删除所有的容器
  • 启动和停止容器的操作
docker start 容器id           
docker restart 容器id
docker stop 容器id
docker kill 容器id

11 日志,元数据,进程的查看

  • 后台启动容器
# 命令 docker run -d 镜像名!
[root@localhost ~]# docker run -d centos
e444a0538f2604e973b527e2eb52d8b4e768fb322fc753584160810dcff1ee92
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES 
# 问题docker ps, 发现centos停止了.
# 常见的坑, docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动执行.
# nginx, 容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了.
  • 查看日志
docker logs -ft  容器, 没有日志
# 自己编写一段shell脚本
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo lvyang;sleep 1;done"
f5c7df36b27d9b703b0a2f5acb2ceeedcc717f56173b03ab54dc30ef7161a00c
# docker logs -ft f5c7df36b27d
# docker logs -ft --tail 10 f5c7df36b27d
  • 查看容器中进程信息.
# 命令 docker top 容器id
[root@localhost ~]# docker top f5c7df36b27d
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                46588               46570               0                   13:25               ?                   00:00:00            /bin/sh -c while true;do echo lvyang;sleep 1;done
root                46912               46588               0                   13:30               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
  • 查看镜像的元数据.
# docker inspect 
[root@localhost ~]#  docker inspect f5c7df36b27d
[
    {
        "Id": "f5c7df36b27d9b703b0a2f5acb2ceeedcc717f56173b03ab54dc30ef7161a00c",
        "Created": "2020-06-07T05:25:41.786500441Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo lvyang;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 46588,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-06-07T05:25:42.942468836Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee",
        "ResolvConfPath": "/var/lib/docker/containers/f5c7df36b27d9b703b0a2f5acb2ceeedcc717f56173b03ab54dc30ef7161a00c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/f5c7df36b27d9b703b0a2f5acb2ceeedcc717f56173b03ab54dc30ef7161a00c/hostname",
        "HostsPath": "/var/lib/docker/containers/f5c7df36b27d9b703b0a2f5acb2ceeedcc717f56173b03ab54dc30ef7161a00c/hosts",
        "LogPath": "/var/lib/docker/containers/f5c7df36b27d9b703b0a2f5acb2ceeedcc717f56173b03ab54dc30ef7161a00c/f5c7df36b27d9b703b0a2f5acb2ceeedcc717f56173b03ab54dc30ef7161a00c-json.log",
        "Name": "/admiring_tesla",
        "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,
            "Capabilities": null,
            "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/9fc9019d0b1513d6430754c22a48e7891d9d6fc811fc3cbad2794502b8b37fc2-init/diff:/var/lib/docker/overlay2/ae47af1c1d7a9b6416705aa33190c71631d0fd4be9e38cbf40980815f75b9a97/diff",
                "MergedDir": "/var/lib/docker/overlay2/9fc9019d0b1513d6430754c22a48e7891d9d6fc811fc3cbad2794502b8b37fc2/merged",
                "UpperDir": "/var/lib/docker/overlay2/9fc9019d0b1513d6430754c22a48e7891d9d6fc811fc3cbad2794502b8b37fc2/diff",
                "WorkDir": "/var/lib/docker/overlay2/9fc9019d0b1513d6430754c22a48e7891d9d6fc811fc3cbad2794502b8b37fc2/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "f5c7df36b27d",
            "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 lvyang;sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200114",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS",
                "org.opencontainers.image.created": "2020-01-14 00:00:00-08:00",
                "org.opencontainers.image.licenses": "GPL-2.0-only",
                "org.opencontainers.image.title": "CentOS Base Image",
                "org.opencontainers.image.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "6ee8611baaef01d80f4a51acbc1e34d9c36981cafccb9f6221b8514de44d9ef5",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/6ee8611baaef",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "b6c7453ea756f9162e0ba49bc4366228c9eb940800ad60d57f2c3420dd059b3d",
            "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": "65cf1533912978a53898d8c9cacd9571f788e81a46e4a92e677e44bd700d7b6d",
                    "EndpointID": "b6c7453ea756f9162e0ba49bc4366228c9eb940800ad60d57f2c3420dd059b3d",
                    "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
                }
            }
        }
    }
]

12. 进入容器的命令和拷贝命令

# 我们通常容器都是使用后台方式运行的, 需要进入容器,修改一些配置.
# 1.   进入容器后开启一个新的终端, 可以在里面操作.
docker exce -it contos /bin/bash/
# 2. 进入容器正在执行的终端,不会启动新的进程(不停循环的日子输出....)
[root@localhost ~]# docker attach f5c7df36b27d

  • 从容器内拷贝文件到主机上
docker cp 容器id 

docker cp f5c7df36b27d:/home/test.java /home

14. 部署Niginx

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,948评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,371评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,490评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,521评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,627评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,842评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,997评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,741评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,203评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,534评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,673评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,339评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,955评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,770评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,000评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,394评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,562评论 2 349