Centos安装Docker

一.Docker安装步骤

1.配置本地yum

#配置阿里云镜像源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all 

2.卸载本地已安装Docker

yum remove docker

3.安装所需依赖

#安装所需依赖
yum install -y yum-utils

#配置阿里Docker镜像
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4.安装Docker

#更新软件包
yum makecache fast
#安装最新Docker
yum install docker-ce docker-ce-cli containerd.io

#可以选择安装Docker版本
list docker-ce --showduplicates | sort -r
#VERSION_STRING中选择版本
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

5.启动Docker服务

#启动对应服务
systemctl start docker
#查看Docker版本
[root@localhost lib]# docker version
Client: Docker Engine - Community
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        370c289
 Built:             Fri Apr  9 22:45:33 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.6
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8728dd2
  Built:            Fri Apr  9 22:43:57 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

6.安装一个测试程序

#Pull一个HelloWorld测试程序
[root@localhost lib]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
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/

7.查看Docker镜像

[root@localhost /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    d1165f221234   7 weeks ago   13.3kB

8.卸载Docker

#卸载软件
yum remove docker-ce docker-ce-cli containerd.io
#删除资源
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

二.Docker镜像命令

1.查看镜像

#查看镜像
docker images --help
#相关参数
-a 查看所有信息
-f 过滤
-q 只查看id

2.搜索命令

#搜索命令
docker search --help
#查询mysql
docker search mysql
#根据过滤条件查询
[root@localhost /]# docker search mysql --filter=STARS=5000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10777     [O

3.下载镜像

#从仓储拉取,不带版本 docker pull 镜像名:[tag]
[root@localhost /]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
f7ec5a41d630: Pull complete    #docker核心,文件分层系统,如有较低版本则不用下相同部分,可共用
9444bb562699: Pull complete 
6a4207b96940: Pull complete 
181cefd361ce: Pull complete 
8a2090759d8a: Pull complete 
15f235e0d7ee: Pull complete 
d870539cd9db: Pull complete 
493aaa84617a: Pull complete 
bfc0e534fc78: Pull complete 
fae20d253f9d: Pull complete 
9350664305b3: Pull complete 
e47da95a5aab: Pull complete 
Digest: sha256:04ee7141256e83797ea4a84a4d31b1f1bc10111c8d1bc1879d52729ccd19e20a
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

#按版本下载,只下载了id不相同的部分,共用上层相同文件
[root@localhost /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
f7ec5a41d630: Already exists 
9444bb562699: Already exists 
6a4207b96940: Already exists 
181cefd361ce: Already exists 
8a2090759d8a: Already exists 
15f235e0d7ee: Already exists 
d870539cd9db: Already exists 
cb7af63cbefa: Pull complete 
151f1721bdbf: Pull complete 
fcd19c3dd488: Pull complete 
415af2aa5ddc: Pull complete 
Digest: sha256:a655529fdfcbaf0ef28984d68a3e21778e061c886ff458b677391924f62fb457
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

3.镜像删除

[root@localhost /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
mysql         5.7       87eca374c0ed   5 days ago    447MB
mysql         latest    0627ec6901db   5 days ago    556MB
hello-world   latest    d1165f221234   7 weeks ago   13.3kB
#根据id进行删除
[root@localhost /]# docker rmi -f 87eca374c0ed
Untagged: mysql:5.7
Untagged: mysql@sha256:a655529fdfcbaf0ef28984d68a3e21778e061c886ff458b677391924f62fb457
Deleted: sha256:87eca374c0ed97f0f0b504174b0d22b0a0add454414c0dbf5ae43870369f6854
Deleted: sha256:3b035442a2f8d52d6c5c2d83a18c6c21a89b4dc6c89b481bcf40df89087655ce
Deleted: sha256:a223f1762b2c619a59b81fc2304bf4c9b791c777c8bdb19760c09cbd1f061efc
Deleted: sha256:92402939b3fd03bee3745eb90df9799bcb7d0ef92ca8ecf7ef37ad9c8a550084
Deleted: sha256:028b21e33aa4cd9c88acdd194d5cbef25638ffbca8669bfc0da72ad1eb148997
[root@localhost /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
mysql         latest    0627ec6901db   5 days ago    556MB
hello-world   latest    d1165f221234   7 weeks ago   13.3kB
#根据查询id全部删除
[root@localhost /]# docker rmi -f $(docker images -qa)

三.Docker容器命令

1.新建一个容器(新下载Centos)并启动

#下载镜像
docker pull centos
-d  后台运行方式
-it 与镜像容器进行交互
-P  映射容器端口,或者映射主机与容器端口8080:8080
-p  映射随机端口
--name="NAME" 设定容器名称进行区分 例如TOMCAT01,TOMCAT02

#启动并进入容器
[root@localhost /]# docker run -it centos /bin/bash 
[root@3c2142c20fb5 /]# 
#挂起容器
ctrl+p+q
#退出容器
exit
#列出已运行的容器
[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
48cb0ba9af6b   centos        "/bin/bash"   2 minutes ago    Exited (127) 7 seconds ago             relaxed_edison
3c2142c20fb5   centos        "/bin/bash"   5 minutes ago    Exited (0) 3 minutes ago               distracted_mayer
cc287d1937b4   hello-world   "/hello"      16 minutes ago   Exited (0) 16 minutes ago              xenodochial_galileo
21a27357f54d   hello-world   "/hello"      2 hours ago      Exited (0) 2 hours ago                 zen_einstein

2.删除容器

[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
f97d54a7b64c   centos    "/bin/bash"   18 minutes ago   Up 18 minutes             elegant_turing
[root@localhost /]# docker rm -f f97d54a7b64c
f97d54a7b64c
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#可以递归删除所有容器
docker rm -f $(docker ps -qs)

3.启动和停止容器

#启动docker容器
[root@localhost /]# docker run -it centos /bin/bash
[root@46d4645d0d11 /]# exit
exit
#查看历史运行的容器
[root@localhost /]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                        PORTS     NAMES
46d4645d0d11   centos        "/bin/bash"   8 seconds ago    Exited (0) 5 seconds ago                clever_cohen
819747a9ab7b   centos        "/bin/bash"   23 minutes ago   Exited (0) 23 minutes ago               flamboyant_blackburn
[root@localhost /]# docker start 46d4645d0d11
46d4645d0d11
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
46d4645d0d11   centos    "/bin/bash"   30 seconds ago   Up 5 seconds             clever_cohen
[root@localhost /]# docker stop 46d4645d0d11
46d4645d0d11
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#如果无法结束docker进程可以kill命令
docker kill [id]

四.常用其他命令

1.后台启动镜像

[root@localhost /]# docker run -d centos
53dca7cf27796afa29dcbe446fdb00b2928c61f01812e41540511e77d521c627
#后台运行需要一个前台进程,docker发现容器中没有应用,就会自动停止!
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

2.查看镜像日志

#挂起Docker进程
[root@e905d8f545e4 tmp]# [root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS             PORTS     NAMES
e905d8f545e4   centos    "/bin/bash"   About an hour ago   Up About an hour             affectionate_dewdney
#Docker日志帮助
[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)
#显示容器的最近的10条日志
[root@localhost /]# docker logs --tail 10 e905d8f545e4

3.查看容器中的进程

#查看容器
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS             PORTS     NAMES
e905d8f545e4   centos    "/bin/bash"   About an hour ago   Up About an hour             affectionate_dewdney
#根据id查看容器中的进程
[root@localhost /]# docker top e905d8f545e4
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                42215               42195               0                   04:42               pts/0               00:00:00            /bin/bash

4.查看容器信息

#查看容器进程
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS             PORTS     NAMES
e905d8f545e4   centos    "/bin/bash"   About an hour ago   Up About an hour             affectionate_dewdney
#查看容器信息
[root@localhost /]# docker inspect e905d8f545e4 
[
    {
        "Id": "e905d8f545e4faf25f1140b07c5fa9f2d525fbd527b8a01d1c5428612ad2a109",
        "Created": "2021-04-25T11:42:32.954697372Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 42215,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-04-25T11:42:33.495756417Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/e905d8f545e4faf25f1140b07c5fa9f2d525fbd527b8a01d1c5428612ad2a109/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/e905d8f545e4faf25f1140b07c5fa9f2d525fbd527b8a01d1c5428612ad2a109/hostname",
        "HostsPath": "/var/lib/docker/containers/e905d8f545e4faf25f1140b07c5fa9f2d525fbd527b8a01d1c5428612ad2a109/hosts",
        "LogPath": "/var/lib/docker/containers/e905d8f545e4faf25f1140b07c5fa9f2d525fbd527b8a01d1c5428612ad2a109/e905d8f545e4faf25f1140b07c5fa9f2d525fbd527b8a01d1c5428612ad2a109-json.log",
        "Name": "/affectionate_dewdney",
        "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/c7387e41e371cc3cb9a9eb099895523d0ea81fc767f4e13722d3482c056fe1c0-init/diff:/var/lib/docker/overlay2/f88e6c52d5e3c821a53d376ad1b9a20b231826dc7dbaf89b8b88b8defcd97f16/diff",
                "MergedDir": "/var/lib/docker/overlay2/c7387e41e371cc3cb9a9eb099895523d0ea81fc767f4e13722d3482c056fe1c0/merged",
                "UpperDir": "/var/lib/docker/overlay2/c7387e41e371cc3cb9a9eb099895523d0ea81fc767f4e13722d3482c056fe1c0/diff",
                "WorkDir": "/var/lib/docker/overlay2/c7387e41e371cc3cb9a9eb099895523d0ea81fc767f4e13722d3482c056fe1c0/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "e905d8f545e4",
            "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": "f1b129209ce43fad97964f22544eda8636a7f7b4c9e11520684efd0259563211",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/f1b129209ce4",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "5765cacba5e60d164ef75958f4d8ac793282342de413bd29e38209f6ee19e3a4",
            "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": "eb7411eaea4146ffa971d8466ea3aad67da8512949074ae796fdb3db3745dcb8",
                    "EndpointID": "5765cacba5e60d164ef75958f4d8ac793282342de413bd29e38209f6ee19e3a4",
                    "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
                }
            }
        }
    }
]

5.进入正在运行的容器

[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS             PORTS     NAMES
e905d8f545e4   centos    "/bin/bash"   About an hour ago   Up About an hour             affectionate_dewdney
#交互模式进入容器docker exec -it 容器id 命令方式
[root@localhost /]# docker exec -it e905d8f545e4 /bin/bash
#方式2:进入正在执行的进程
[root@localhost /]# docker attach e905d8f545e4

6.从容器中拷贝文件至本地

[root@localhost /]# docker attach e905d8f545e4
[root@e905d8f545e4 tmp]# cd /tmp
[root@e905d8f545e4 tmp]# ls
1.sh  ks-script-esd4my7v  ks-script-eusq_sc5
[root@e905d8f545e4 tmp]# exit
exit
[root@localhost /]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED             STATUS                      PORTS     NAMES
e905d8f545e4   centos        "/bin/bash"   About an hour ago   Exited (0) 17 seconds ago             affectionate_dewdney
#拷贝容器中的1.sh文件到本地home文件夹中
[root@localhost /]# docker cp e905d8f545e4:/tmp/1.sh /home
[root@localhost /]# cd /home
[root@localhost home]# ls
1.sh  tenno
[root@localhost home]# 

命令小结图片:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值