Docker基础知识介绍

Docker概述

查看百度百科介绍 docker

查看官网介绍 官网docker介绍

docker社区版本文档:https://docs.docker.com/engine/installation/

 

Docker部署

系统环境说明

[root@linux-node1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@linux-node1 ~]# uname -r
3.10.0-327.el7.x86_64
[root@linux-node1 ~]# uname -m
x86_64

 

Docker安装

[root@linux-node1 ~]# yum install -y docker
[root@linux-node1 ~]# systemctl enable docker
[root@linux-node1 ~]# systemctl start docker
[root@linux-node1 ~]# ps -ef|grep docker
root     33377     1  0 20:54 ?        00:00:00 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --selinux-enabled --log-driver=journald --signature-verification=false
root     33381 33377  0 20:54 ?        00:00:00 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc --runtime-args --systemd-cgroup=true
root     33508 31744  0 20:54 pts/0    00:00:00 grep --color=auto docker

 

修改Docker默认仓库地址 

由于docker默认仓库地址在国外需要修改库地址,这里是有阿里云给用户提供的docker加速地址。修改方法如下:

获取阿里云提供的docker镜像加速地址(容器服务-->控制台-->Docker Hub 镜像站点-->您的专属加速器地址): https://xxxxx.mirror.aliyuncs.com

[root@linux-node1 ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd-current \
--registry-mirror=https://k9lgui7f.mirror.aliyuncs.com \  # 新增这行配置
[root@linux-node1 ~]# systemctl daemon-reload
[root@linux-node1 ~]# systemctl restart docker

Docker基本应用

docker指令列表:https://docs.docker.com/edge/engine/reference/commandline/docker/#description

启动并管理第一个docker容器

[root@linux-node1 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
b04784fba78d: Pull complete 
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f

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.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
[root@linux-node1 ~]# docker ps               # 列出运行中的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

[root@linux-node1 ~]# docker ps -a                      # 列出本地所有的容器(包括运行、停止状态)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9fe352841955 hello-world "/hello" 54 seconds ago Exited (0) 52 seconds ago jovial_dijkstra

[root@linux-node1 ~]# docker images            # 查看本地镜像列表
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest 1815c82652c0 4 days ago 1.84 kB

[root@linux-node1 ~]# docker save hello-world >hello-world.tar    # 导出本地容器镜像
[root@linux-node1 ~]# ll
total 24
-rw-------. 1 root root 3526 Feb 27 17:57 anaconda-ks.cfg
-rw-r--r-- 1 root root 12800 Jun 19 21:29 hello-world.tar
-rw-r--r--. 1 root root 2779 Feb 27 17:45 ks-pre.log
[root@linux
-node1 ~]# docker rmi hello-world # 删除镜像,但是由于有容器正在使用该镜像导致删除失败 Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 9fe352841955 is using its referenced image 1815c82652c0
[root@linux
-node1 ~]# docker rmi -f hello-world           # 强制删除镜像 Untagged: hello-world:latest Untagged: docker.io/hello-world@sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f Deleted: sha256:1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57
[root@linux
-node1 ~]# docker images                  # 查看删除结果 REPOSITORY TAG IMAGE ID CREATED SIZE
[root@linux
-node1 ~]# docker load < hello-world.tar         # 导入保存的镜像 Loaded image: docker.io/hello-world:latest
[root@linux
-node1 ~]# tar -xf hello-world.tar # 镜像本质是固定格式的各种文件的tar包 [root@linux-node1 ~]# ll total 36 -rw-r--r-- 1 root root 1520 Jun 15 03:29 1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57.json drwxr-xr-x 2 root root 47 Jun 15 03:29 3e0554cb0efadb678332292bb7835495fbb0c71af7e01bd4f7d11e64fe3d54df -rw-r--r-- 1 root root 217 Jan 1 1970 manifest.json -rw-r--r-- 1 root root 104 Jan 1 1970 repositories

 其他相关指令介绍

[root@linux-node1 ~]# docker search centos      # 在官网容器镜像库里面搜索centos镜像
INDEX       NAME                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                                 The official build of CentOS.                   3409      [OK]                               # 官方提供的镜像
docker.io   docker.io/jdeathe/centos-ssh                     CentOS-6 6.9 x86_64 / CentOS-7 7.3.1611 x8...   72                   [OK]
docker.io   docker.io/nimmis/java-centos                     This is docker images of CentOS 7 with dif...   26                   [OK]
docker.io   docker.io/gluster/gluster-centos                 Official GlusterFS Image [ CentOS-7 +  Glu...   19                   [OK]
docker.io   docker.io/kinogmt/centos-ssh                     CentOS with SSH                                 14                   [OK]
docker.io   docker.io/egyptianbman/docker-centos-nginx-php   A simple and highly configurable docker co...   9                    [OK]
docker.io   docker.io/torusware/speedus-centos               Always updated official CentOS docker imag...   8                    [OK]
docker.io   docker.io/nathonfowlie/centos-jre                Latest CentOS image with the JRE pre-insta...   6                    [OK]
docker.io   docker.io/centos/mariadb55-centos7                                                               5                    [OK]
docker.io   docker.io/darksheer/centos                       Base Centos Image -- Updated hourly             3                    [OK]
docker.io   docker.io/centos/redis                           Redis built for CentOS                          2                    [OK]
docker.io   docker.io/harisekhon/centos-java                 Java on CentOS (OpenJDK, tags jre/jdk7-8)       2                    [OK]
docker.io   docker.io/harisekhon/centos-scala                Scala + CentOS (OpenJDK tags 2.10-jre7 - 2...   2                    [OK]
docker.io   docker.io/blacklabelops/centos                   CentOS Base Image! Built and Updates Daily!     1                    [OK]
docker.io   docker.io/freenas/centos                         Simple CentOS Linux interactive container       1                    [OK]
docker.io   docker.io/sgfinans/docker-centos                 CentOS with a running sshd and Docker           1                    [OK]
docker.io   docker.io/timhughes/centos                       Centos with systemd installed and running       1                    [OK]
docker.io   docker.io/vorakl/centos                          CentOS7, EPEL, tools. Updated/Tested daily!     1                    [OK]
docker.io   docker.io/grayzone/centos                        auto build for centos.                          0                    [OK]
docker.io   docker.io/grossws/centos                         CentOS 6 and 7 base images with gosu and l...   0                    [OK]
docker.io   docker.io/januswel/centos                        yum update-ed CentOS image                      0                    [OK]
docker.io   docker.io/kz8s/centos                            Official CentOS plus epel-release               0                    [OK]
docker.io   docker.io/repositoryjp/centos                    Docker Image for CentOS.                        0                    [OK]
docker.io   docker.io/smartentry/centos                      centos with smartentry                          0                    [OK]
docker.io   docker.io/vcatechnology/centos                   A CentOS Image which is updated daily           0                    [OK]
[root@linux
-node1 ~]# docker pull centos:6.6          # 拉取指定的镜像 Trying to pull repository docker.io/library/centos ... 6.6: Pulling from docker.io/library/centos 90577c79babf: Pull complete Digest: sha256:e21297742183af3e64cbd42585c1718b53c677797a77044ba13c1425c21ef06b
[root@linux
-node1 layerdb]# cd /var/lib/docker/image/devicemapper/layerdb    # 实际镜像存储位置 [root@linux-node1 layerdb]# tree . ├── mounts ├── sha256 │   └── 373c3c960ecf7d8419a1444bb371baa88cfb1c68b8209f1b0d58c1cb4fc80d3e │   ├── cache-id │   ├── diff │   ├── size │   └── tar-split.json.gz └── tmp 4 directories, 4 files # 注释:详细的存储目录说明 http://licyhust.com/%E5%AE%B9%E5%99%A8%E6%8A%80%E6%9C%AF/2016/09/27/docker-image-data-structure/
[root@linux-node1 ~]# docker run -it -d --name=centos-6.6-01 centos:6.6 sh        
6d13a7232a388476d0fd5219d271e241eb1f8b49e7166419e48312654a521a25
[root@linux
-node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d13a7232a38 centos:6.6 "sh" 6 seconds ago Up 5 seconds centos-6.6-01

[root@linux-node1 ~]# docker exec -it centos-6.6-01 bash [root@6d13a7232a38 /]# cd [root@6d13a7232a38 ~]# ls -l total 0 [root@6d13a7232a38 ~]# # ctrl+p+q 退出容器 # 参数说明 -t:在新容器内指定一个伪终端或终端 -i:允许你对容器内的标准输入(STDIN)进行交互 -d:后台运行 --name:指定容器名称

 

[root@linux-node1 ~]# docker inspect centos-6.6-01  # 查看容器的所有信息
[
    {
        "Id": "6d13a7232a388476d0fd5219d271e241eb1f8b49e7166419e48312654a521a25",
        "Created": "2017-06-19T14:06:39.782219421Z",
        "Path": "sh",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 34527,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-06-19T14:06:40.194889044Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:d0362617006138b556bed588d181c107d2db47f71dd5be78704ab822f6ce1d63",
        "ResolvConfPath": "/var/lib/docker/containers/6d13a7232a388476d0fd5219d271e241eb1f8b49e7166419e48312654a521a25/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/6d13a7232a388476d0fd5219d271e241eb1f8b49e7166419e48312654a521a25/hostname",
        "HostsPath": "/var/lib/docker/containers/6d13a7232a388476d0fd5219d271e241eb1f8b49e7166419e48312654a521a25/hosts",
        "LogPath": "",
        "Name": "/centos-6.6-01",
        "RestartCount": 0,
        "Driver": "devicemapper",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": [
            "5a5c3858d90b0d218cccda4d00ee5125d7667f8394878767225d1a9902c7d33b"
        ],
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "journald",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "docker-runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "devicemapper",
            "Data": {
                "DeviceId": "9",
                "DeviceName": "docker-8:3-135201927-7e8795de189a83b6b563e1711af3a03d4978c693a209b156c61924d0ac2a4810",
                "DeviceSize": "10737418240"
            }
        },
        "Mounts": [],
        "Config": {
            "Hostname": "6d13a7232a38",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "sh"
            ],
            "Image": "centos:6.6",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "a98be384bcc5460090ccaee31e9f04d7ba96d1c46db61d9b57b6089509cc8baa",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/a98be384bcc5",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "94de7bf9b816f6ac17582472b27957843bee9fb3cc7efc53bf5742def6d2e1c7",
            "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": "39b0d172812f93e14bb599922d47c17ae4b7eba18ea18e3f8f6f7c8e6e3dc8c2",
                    "EndpointID": "94de7bf9b816f6ac17582472b27957843bee9fb3cc7efc53bf5742def6d2e1c7",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02"
                }
            }
        }
    }
]
[root@linux-node1 ~]# docker attach centos-6.6-01        # 第一种方法进入容器进行操作
sh-4.1# ls -l
total 28
dr-xr-xr-x   2 root root 4096 Mar  4  2015 bin
drwxr-xr-x   5 root root  380 Jun 19 14:06 dev
drwxr-xr-x  45 root root 4096 Jun 19 14:06 etc
drwxr-xr-x   2 root root    6 Sep 23  2011 home
dr-xr-xr-x   7 root root   76 Mar  4  2015 lib
dr-xr-xr-x   6 root root 8192 Mar  4  2015 lib64
drwx------   2 root root    6 Mar  4  2015 lost+found
drwxr-xr-x   2 root root    6 Sep 23  2011 media
drwxr-xr-x   2 root root    6 Sep 23  2011 mnt
drwxr-xr-x   2 root root    6 Sep 23  2011 opt
dr-xr-xr-x 253 root root    0 Jun 19 14:06 proc
dr-xr-x---   2 root root   86 Mar  4  2015 root
drwxr-xr-x   3 root root   20 Jun 19 14:06 run
dr-xr-xr-x   2 root root 4096 Mar  4  2015 sbin
drwxr-xr-x   3 root root   21 Mar  4  2015 selinux
drwxr-xr-x   2 root root    6 Sep 23  2011 srv
dr-xr-xr-x  13 root root    0 Jun 19 14:06 sys
drwxrwxrwt   2 root root    6 Mar  4  2015 tmp
drwxr-xr-x  13 root root  143 Mar  4  2015 usr
drwxr-xr-x  17 root root 4096 Mar  4  2015 var
[root@linux-node1 ~]# docker exec -it centos-6.6-01 bash  # 第二种方法进入容器(推荐)
[root@6d13a7232a38 /]#
[root@linux-node1 ~]# yum install util-linux              # 第三种进入容器方法
[root@linux-node1 ~]# cat docker_in.sh 
#!/bin/env bash
if [ ! $# -eq 1 ]
then
    echo "Usages:$0 CONTAINER_ID or CONTAINER_NAME"
    exit 1
fi

pid=`docker inspect --format "{{.State.Pid}}" $1`
nsenter -t $pid -m -u -i -n -p
[root@linux-node1 ~]# sh docker_in.sh 
Usages:docker_in.sh CONTAINER_ID or CONTAINER_NAME
[root@linux-node1 ~]# sh docker_in.sh centos-6.6-01
[root@6d13a7232a38 /]# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:648 (648.0 b)  TX bytes:648 (648.0 b)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

[root@6d13a7232a38 /]# 
[root@linux-node1 ~]# docker rm centos-6.6-01    # 删除运行中的容器
Error response from daemon: You cannot remove a running container 6d13a7232a388476d0fd5219d271e241eb1f8b49e7166419e48312654a521a25. Stop the container before attempting removal or use -f
[root@linux-node1 ~]# docker rm -f centos-6.6-01
centos-6.6-01
[root@linux-node1 ~]# docker run -it --rm --name=centos-6.6-01 centos:6.6 sh    # --rm参数 容器停止后自动删除相关文件
sh-4.1# exit
[root@linux-node1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@linux-node1 ~]# docker logs centos-6.6-01     # 查看容器日志
[root@linux-node1 ~]# docker logs -f centos-6.6-01  # 实时查看容器日志

 

posted @ 2017-06-20 09:16  每天进步一点点!!!  阅读(1453)  评论(0编辑  收藏  举报