码迷,mamicode.com
首页 > 其他好文 > 详细

docker基础

时间:2020-07-02 18:16:36      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:bad   star   pause   shm   信息   max   pup   tfs   dead   

三、docker安装

官方地址:https://docs.docker.com/engine/install/centos/

3.1.安装步骤

1.卸载旧版本

[root@docker ~]# yum remove docker                    docker-client                    docker-client-latest                    docker-common                    docker-latest                    docker-latest-logrotate                    docker-logrotate                    docker-engine -y

2.安装工具包

[root@docker ~]# yum install -y yum-utils

3.安装yum源

[root@docker ~]# yum-config-manager      --add-repo      http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@docker ~]# yum clean all 
[root@docker ~]# yum makecache 

4.安装社区版

[root@docker ~]# yum install docker-ce docker-ce-cli containerd.io -y

5.启动docker

[root@docker ~]# systemctl start docker

6.检查版本

[root@docker ~]# docker version
[root@docker ~]# docker info  

7.测试输出helloworld

[root@docker ~]# docker run hello-world

技术图片

8.网易163加速(未使用)

[root@docker ~]# cat /etc/docker/daemon.json    
{
  "registry-mirrors": ["http://xxx.c.163.com"]
}
[root@docker ~]# 
[root@docker ~]# systemctl daemon-reload 
[root@docker ~]# systemctl restart docker

9.阿里云加速(实验环境使用)

[root@docker ~]# cat /etc/docker/daemon.json

{
  "registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}

[root@docker ~]# 
[root@docker ~]# systemctl daemon-reload 
[root@docker ~]# systemctl restart docker

四、docker的常用命令

  • 拓扑

技术图片

4.1.帮助命令

docker version      #显示docker的版本信息
docker info         #显示docker的系统信息
docker --help       

任何的博客写的都没有官方文档详细

帮助文档的官方地址:https://docs.docker.com/reference/

技术图片

4.2.镜像命令

docker images

[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
badwolf/centos      httpd               00398159773f        8 days ago          782MB

#解释
REPOSITORY      #镜像仓库源
TAG             #镜像的标签
IMAGE ID        #镜像的ID
CREATED         #镜像的创建时间
SIZE            #镜像的大小

#可选项:
Options:
  -a, --all             #列出所有镜像
  -q, --quiet           #只显示镜像id

docker search

[root@docker ~]# docker search mysql
#可选项
Options:
  -f, --filter filter   #--filter=STARS=100 搜索出来的镜像STARS大于100

docker pull

[root@docker ~]# docker pull mysql
Using default tag: latest           #如果不选择版本,默认下载最新版,docker pull mysql:5.7,版本官方存在即可
latest: Pulling from library/mysql      
d51ce1c2e575: Pull complete         #分层下载
c2344adc4858: Pull complete 
......
Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6 #签名
Status: Downloaded newer image for mysql:latest 
docker.io/library/mysql:latest #真实下载地址
[root@docker ~]# 

docker rmi

#指定镜像ID删除
[root@docker ~]# docker rmi -f 镜像id
#删除多个镜像
[root@docker ~]# docker rmi -f 镜像id1 镜像id2 镜像id3 ......
#删除全部镜像
[root@docker ~]# docker rmi -f $(docker images -aq)

docker remove

#卸载docker
[root@docker ~]# yum remove docker-ce docker-ce-cli containerd.io

4.3.容器命令

说明:下载一个centos来进行测试

[root@docker ~]# docker pull centos:7   #一定要加版本号,最新版的下载可能会有问题,不兼容你的系统

docker run

[root@docker ~]# docker run [可选参数] image

#参数列表
--name  #容器名字 httpd mysqld tomcat .....
-d      #后台运行
-it     #使用交互模式运行,进入容器查看内容
-p      #指定容器的端口
    -p  #主机端口:容器端口
    -p  #容器端口
    -p  #ip:主机端口:容器端口
    -p  #容器端口

#案例1:
#启动并进入容器
[root@docker ~]# docker run -it centos:7 /bin/bash
[root@440e3ddc5000 /]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
[root@440e3ddc5000 /]# hostname
440e3ddc5000 
[root@440e3ddc5000 /]# exit 

#案例2
#后台启动
docker run -d centos:7  
#问题
    #使用-d启动,使用ps检查发现容器停止了,出现这样的问题的原因是容器的启动必须有一个前台进程,否则docker会认为程序已经关闭,所以docker会自动停止

docker ps

#检查正在运行的容器
[root@docker ~]# docker ps -a

#参数说明
-a      #检查所有容器状态
-n=?    #显示最近创建的容器
-q      #只显示容器编号

退出容器

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

docker rm

说明:镜像在运行的时候变成容器,所有容器在运行的时候是无法删除镜像的,但是容器在运行状态可以删除容器。

#删除一个容器
[root@docker ~]# docker rm -f 容器id
#删除所有容器
[root@docker ~]# docker rm -f $(docker ps -aq)
[root@docker ~]# docker ps -a -q |xargs rm -f

docker start

[root@docker ~]# docker start id

docker stop

[root@docker ~]# docker stop id 

docker kill

[root@docker ~]# docker kill id

docker logs

[root@docker ~]# docker logs name/id
#参数说明
-ft             #显示日志
--tail +number  #要显示的日志条数 

#案例1
#持续运行一个容器,并输出指定信息
[root@docker ~]# docker run -d centos:7 /bin/bash -c "while true;do echo hello world;sleep 1; done"
[root@docker ~]# docker logs f723b0ee1be7

#检查尾部10条日志
[root@docker ~]# docker logs -tf --tail 10 f723b0ee1be7

技术图片

docker top

#显示容器里面的进程
[root@docker ~]# docker top name/id

docker commit

#相当于创建一个新的镜像模板,这个模板里面包含下载的httpd
[root@ed1b2d1b2a8a /]# ping www.baidu.com
[root@ed1b2d1b2a8a /]# yum install -y httpd
[root@ed1b2d1b2a8a /]# exit
[root@docker ~]# docker commit 运行的容器id  新的镜像id

docker stats

#检查CPU状态
[root@docker ~]# docker stats 

docker inspect(重要)

#查看容器的元数据
[root@docker ~]# docker run  -d centos:7 /bin/bash -c "while true;do echo hello world;sleep 1; done"
[root@docker ~]# docker inspect 5d225d045b80 
[
    {
        "Id": "5d225d045b8012ffcb79eefcc57c3fab474fc451b396dd73e59fc0803b3bdb9c",
        "Created": "2020-07-01T03:29:51.099721427Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo hello world;sleep 1; done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 19692,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-07-01T03:29:51.295193435Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:b5b4d78bc90ccd15806443fb881e35b5ddba924e2f475c1071a38a3094c3081d",
        "ResolvConfPath": "/var/lib/docker/containers/5d225d045b8012ffcb79eefcc57c3fab474fc451b396dd73e59fc0803b3bdb9c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/5d225d045b8012ffcb79eefcc57c3fab474fc451b396dd73e59fc0803b3bdb9c/hostname",
        "HostsPath": "/var/lib/docker/containers/5d225d045b8012ffcb79eefcc57c3fab474fc451b396dd73e59fc0803b3bdb9c/hosts",
        "LogPath": "/var/lib/docker/containers/5d225d045b8012ffcb79eefcc57c3fab474fc451b396dd73e59fc0803b3bdb9c/5d225d045b8012ffcb79eefcc57c3fab474fc451b396dd73e59fc0803b3bdb9c-json.log",
        "Name": "/cool_driscoll",
        "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/36d2790a2b810dc85ff542cf18b69a9fba643336184c612c82abbf28e9cac771-init/diff:/var/lib/docker/overlay2/abca5eb688f401e6a61dea88c8db1405b0e3d3a4368bc364b3f02972dbcd4764/diff",
                "MergedDir": "/var/lib/docker/overlay2/36d2790a2b810dc85ff542cf18b69a9fba643336184c612c82abbf28e9cac771/merged",
                "UpperDir": "/var/lib/docker/overlay2/36d2790a2b810dc85ff542cf18b69a9fba643336184c612c82abbf28e9cac771/diff",
                "WorkDir": "/var/lib/docker/overlay2/36d2790a2b810dc85ff542cf18b69a9fba643336184c612c82abbf28e9cac771/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "5d225d045b80",
            "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/bash",
                "-c",
                "while true;do echo hello world;sleep 1; done"
            ],
            "Image": "centos:7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200504",
                "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-05-04 00:00:00+01:00",
                "org.opencontainers.image.licenses": "GPL-2.0-only",
                "org.opencontainers.image.title": "CentOS Base Image",
                "org.opencontainers.image.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "ecc4e8e85fb1417884843733bd8fb514a51e61d6c45f8dec852212c6ef67baa1",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/ecc4e8e85fb1",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "378e0fe1d3c21049be0d2b781f23c55daa70c33ebc220cd81b5d60b4e59af45c",
            "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": "1393cca089f9866db692381f1f57d9ef4b752522798831ca936a14b96f63afa9",
                    "EndpointID": "378e0fe1d3c21049be0d2b781f23c55daa70c33ebc220cd81b5d60b4e59af45c",
                    "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
                }
            }
        }
    }
]
[root@docker ~]# 

docker exec(重要)

#进入容器,打开一个新的终端
[root@docker ~]# docker ps -a
[root@docker ~]# docker exec -it 容器id /bin/bash

docker attach(重要)

#进入容器,进入正在执行的进程,不会打开新的进程
[root@docker ~]# docker attach 容器id

docker cp(重要)

[root@docker ~]# docker cp 容器id:容器内路径  目的主机路径

#案例1
#将容器内的文件拷贝出来
[root@docker ~]# docker run -it centos:7 /bin/bash
[root@132e687f8968 ~]# touch /opt/test.txt
[root@132e687f8968 ~]# exit
[root@docker ~]# docker cp 132e687f8968:/opt/test.txt /mnt
[root@docker ~]# ls /mnt/
test.txt
[root@docker ~]# 

docker命令小结

待补充......

4.3.练习

练习1:部署nginx

要求:利用docker部署两个nginx,映射端口分别为:8888和9999

拓扑

技术图片

1.搜索镜像

官方地址:https://hub.docker.com/

技术图片

[root@docker ~]# docker search nginx:1.14
2.拉取镜像
[root@docker ~]# docker pull nginx:1.14
3.运行测试
[root@docker ~]# docker run -d --name nginx01 -p 8888:80 nginx:1.14
[root@docker ~]# docker run -d --name nginx02 -p 9999:80 nginx:1.14 
[root@docker ~]# docker ps
4.测试验证
[root@docker ~]# curl localhost:8888
[root@docker ~]# curl localhost:9999

技术图片

练习2:部署tomcat

要求:利用docker部署两个tomcat,映射端口分别为:8081和8082

1.搜索镜像
[root@docker ~]# docker search tomcat:9.0
2.下载(拉取)镜像
[root@docker ~]# docker pull tomcat:9.0
3.运行测试
[root@docker ~]# docker run -d --name tomcat01 -p 8082:8080 tomcat:9.0 
[root@docker ~]# docker run -d --name tomcat02 -p 8082:8080 tomcat:9.0

#docker已经精简了tomcat镜像,默认情况下webapp下面没有项目文件,访问会报错404,但是webabb.dist下面有文件,可以copy过去
[root@docker ~]# docker exec -it tomcat01 /bin/bash
root@d4dfffbd6f22:/usr/local/tomcat# cp -r webapps.dist/* webapps/
4.测试验证
[root@docker ~]# curl localhost:8081
[root@docker ~]# curl localhost:8082

练习3:部署es集群

1.搜索镜像
[root@docker ~]# docker search elasticsearch:7.6.1
2.拉取镜像
[root@docker ~]# docker pull elasticsearch:7.6.1
3.运行测试
#es的运行需要注意一个问题,es本身十分的消耗内存,所以在运行的时候需要指定内存限制,否则可能会导致内存不够用而死机
[root@docker ~]# docker run -d --name elasticsearch01  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms1024m -Xmx2048m" elasticsearch:7.6.1
[root@docker ~]# docker stats 
4.测试验证
[root@docker ~]# curl localhost:9200
{
  "name" : "53533d2d3f24",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "WSmV0BPYTv--ig2PftBtag",
  "version" : {
    "number" : "7.6.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
    "build_date" : "2020-02-29T00:15:25.529771Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
[root@docker ~]# 

docker基础

标签:bad   star   pause   shm   信息   max   pup   tfs   dead   

原文地址:https://blog.51cto.com/14539398/2508024

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!