CentOS7安装Docker与使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7安装Docker与使用相关的知识,希望对你有一定的参考价值。

1.安装docker 
yum install docker -y
[[email protected] jenkins]# docker version 
Client:
 Version:         1.12.6
 API version:     1.24
 Package version: docker-1.12.6-28.git1398f24.el7.centos.x86_64
 Go version:      go1.7.4
 Git commit:      1398f24/1.12.6
 Built:           Fri May 26 17:28:18 2017
 OS/Arch:         linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
[[email protected] jenkins]# service docker start 
Redirecting to /bin/systemctl start  docker.service
2.使用docker
使用命令行的工具来检索名字为“tutorial”的镜像 
[[email protected] jenkins]# docker search tutorial
INDEX       NAME                                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/georgeyord/reactjs-tutorial                This is the backend of the React comment b...   4                    [OK]
docker.io   docker.io/egamas/docker-tutorial                     Funny manpages                                  2                    [OK]
docker.io   docker.io/mhausenblas/kairosdb-tutorial              GitHub fetcher for KairosDB tutorial            1                    [OK]
docker.io   docker.io/mjansche/tts-tutorial                      Software for a Text-to-Speech tutorial          1                    [OK]
docker.io   docker.io/odk211/spree-tutorial                                                                      1                    [OK]
docker.io   docker.io/trausch/tutorial-delly                     Cancer Genomics Tutorial of Delly               1                    [OK]
docker.io   docker.io/activeeon/par-connector-tutorial           Do the par-connector tutorial with R. The ...   0                    [OK]
docker.io   docker.io/ajyounge/muelu-tutorial                    Modified muelu tutorial                         0                    [OK]
docker.io   docker.io/algas/wercker-tutorial                     wercker tutorial                                0                    [OK]
docker.io   docker.io/biopython/biopython-tutorial               Biopython with Tutorial running on top of ...   0                    [OK]
docker.io   docker.io/camphor/python-tutorial                    camphor-/python-tutorial                        0                    [OK]
docker.io   docker.io/chris24walsh/flask-aws-tutorial            Runs a simple flask webapp demo, with the ...   0                    [OK]
docker.io   docker.io/delta2323/jnns2015-tutorial                                                                0                    [OK]
docker.io   docker.io/filipe/react-tutorial                      React comment box example, React tutorial:...   0                    [OK]
docker.io   docker.io/guillon/qemu-tutorial                      Image for QEMU tutorial based on qemu-plugins   0                    [OK]
docker.io   docker.io/kobe25/docker-tutorial                     Docker Tutorial                                 0                    [OK]
docker.io   docker.io/locksmithdon/ssb-tutorial                  The image used in a Secure Scuttlebutt tut...   0                    [OK]
docker.io   docker.io/michelesr/docker-tutorial                  Docker Tutorial                                 0                    [OK]
docker.io   docker.io/onekit/rest-tutorial                       REST API server-side tutorial. How to do i...   0                    [OK]
docker.io   docker.io/paulcos11/docker-tutorial                  docker tutorial                                 0                    [OK]
docker.io   docker.io/rinnocente/gromed-ts-tutorial-2017         GRO[macs] + [plu]MED for the Trieste tutor...   0                    [OK]
docker.io   docker.io/rubygem/hydra-tutorial                     Auto-Generated Image for Ruby Gem hydra-tu...   0                    [OK]
docker.io   docker.io/sampige/koha-tutorial                      Koha Tutorial for Schools                       0                    [OK]
docker.io   docker.io/tiagofilipe12/bionode-watermill-tutorial   This is a docker image for running bionode...   0                    [OK]
docker.io   docker.io/zinuzoid/docker-swarm-tutorial-worker      https://github.com/zinuzoid/docker-swarm-t...   0                    [OK]
使用docker命令来下载镜像 
[[email protected] jenkins]# docker pull learn/tutorial
Using default tag: latest
Trying to pull repository docker.io/learn/tutorial ... 
latest: Pulling from docker.io/learn/tutorial
271134aeb542: Downloading [================>                                  ] 23.25 MB/71.04 MB
271134aeb542: Downloading [================>                                  ] 23.79 MB/71.04 MB
271134aeb542: Pull complete 
Digest: sha256:2933b82e7c2a72ad8ea89d58af5d1472e35dacd5b7233577483f58ff8f9338bd
查看已下载的镜像 
[[email protected] jenkins]# docker images 
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
docker.io/learn/tutorial   latest              a7876479f1aa        4 years ago         128 MB
在docker容器中运行hello world!
docker run命令有两个参数,一个是镜像名,一个是要在镜像中运行的命令。 
[[email protected] jenkins]# docker run learn/tutorial  echo “hello word”
“hello word”
在容器中安装新的程序(wget命令) 
[[email protected] jenkins]# docker run learn/tutorial apt-get install -y wget
Reading package lists...
Building dependency tree...
The following extra packages will be installed:
  libidn11
The following NEW packages will be installed:
  libidn11 wget
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 389 kB of archives.
After this operation, 970 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main libidn11 amd64 1.23-2 [112 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ precise/main wget amd64 1.13.4-2ubuntu1 [277 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 389 kB in 25s (15.5 kB/s)
Selecting previously unselected package libidn11.
(Reading database ... 7545 files and directories currently installed.)
Unpacking libidn11 (from .../libidn11_1.23-2_amd64.deb) ...
Selecting previously unselected package wget.
Unpacking wget (from .../wget_1.13.4-2ubuntu1_amd64.deb) ...
Setting up libidn11 (1.23-2) ...
Setting up wget (1.13.4-2ubuntu1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
使用docker ps -l命令获得安装完wget命令之后容器的id。然后把这个镜像保存为learn/wget。 
[[email protected] jenkins]# docker ps -l 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
ea5537bf71f7        learn/tutorial      "apt-get install -y w"   54 seconds ago      Exited (0) 27 seconds ago                       kickass_aryabhata
保存对容器的修改 
[[email protected] jenkins]# docker commit ea5537bf71f7 learn/wget 
sha256:7c5939899fb36a9aed6b7dc9e12edd3704cef1ae5a3e9ae52ea714082e79e972
执行完docker commit命令之后,返回新版本镜像的id号
运行新的镜像 
到现在为止,我们已经建立了一个完整的、自成体系的docker环境,并且安装了wget命令在里面。它可以在任何支持docker环境的系统中运行
在新的镜像中运行wget “http://blog.csdn.net/yown” 命令。 
[[email protected] jenkins]# docker run learn/wget wget “http://blog.csdn.net/yown”
“http://blog.csdn.net/yown”: Scheme missing.
[[email protected] jenkins]# docker ps -l 
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
2e16a889dfa1        learn/wget          "wget “http://blog."   5 minutes ago       Exited (1) 5 minutes ago                       focused_dubinsky
检查运行中的镜像 
[[email protected] jenkins]# docker inspect ea5537bf71f7
[
    {
        "Id": "ea5537bf71f7263ceaf58d0246fc9705dce97cc4e57dab08eebd280a469bbc03",
        "Created": "2017-07-03T07:45:17.356235721Z",
        "Path": "apt-get",
        "Args": [
            "install",
            "-y",
            "wget"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-07-03T07:45:17.800829779Z",
            "FinishedAt": "2017-07-03T07:45:44.067117909Z"
        },
        "Image": "sha256:a7876479f1aae32c0716d7a85b5151af26f533fe48efa086010105cba02f5163",
        "ResolvConfPath": "/var/lib/docker/containers/ea5537bf71f7263ceaf58d0246fc9705dce97cc4e57dab08eebd280a469bbc03/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/ea5537bf71f7263ceaf58d0246fc9705dce97cc4e57dab08eebd280a469bbc03/hostname",
        "HostsPath": "/var/lib/docker/containers/ea5537bf71f7263ceaf58d0246fc9705dce97cc4e57dab08eebd280a469bbc03/hosts",
        "LogPath": "",
        "Name": "/kickass_aryabhata",
        "RestartCount": 0,
        "Driver": "devicemapper",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "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": "6",
                "DeviceName": "docker-253:1-528373-12c0f46fc46238bab100eec9a829f441b40439a9da2c6c097dbbe08d0528ad06",
                "DeviceSize": "10737418240"
            }
        },
        "Mounts": [],
        "Config": {
            "Hostname": "ea5537bf71f7",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [],
            "Cmd": [
                "apt-get",
                "install",
                "-y",
                "wget"
            ],
            "Image": "learn/tutorial",
            "Volumes": {},
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "19d48e001859f8e25085fd4b13db7f925529541c3bef199514d6aaaf03ec621d",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": null,
            "SandboxKey": "/var/run/docker/netns/19d48e001859",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "c178ae9004cdca9f3438687ccf856df3caf694a418864c722c7721f430c38eb2",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": ""
                }
            }
        }
    }
]

如果发布自己的镜像 :

a. 首先在www.docker.com注册账户,登录并在https://cloud.docker.com/repository/list 页面创建自己的库

b. 将刚才我们做的镜像起个新的名字: 
$docker tag learn/wget:latest yown/learn:latest

c. 在命令行登录: 
$docker login

d. 上传 
$docker push yown/learn:latest

Do you really want to push to public registry? [y/n]: y 
The push refers to a repository [docker.io/yown/learn] (len: 1) 
ad2245a8acd8: Pushing [=========================> ] 6.482 MB/12.48 MB




以上是关于CentOS7安装Docker与使用的主要内容,如果未能解决你的问题,请参考以下文章

centos7 安装docker

Docker容器管理--CentOS7安装docker

Centos7.X通过rpm包安装Docker

Centos7安装DockerCE

Centos7 安装指定版本的 Docker

linux(centos7)安装docker