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

你可能感兴趣的:(2020-06-06 docker基础命令-1)