docker安装与使用

Posted walkinginthesun

tags:

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

参考:

1. docker安装

2. docker服务启动

systemctl start docker

3. docker pull ubuntu

运行

docker run -it ubuntu bash

查看系统版本

cat /etc/os-release

停止容器

docker container stop redis

docker stop httpd

4. commit定制镜像

docker run --name redis -d -p 1234:1234 redis:latest
docker container ls


docker exec -it redis bash

docker diff redis
docker commit --author "yvhqbat" --message "mkdir ld" redis redis:v2

docker run -it redis:v2 bash
 
docker history redis:v2

5. Dockerfile

6. docker container prune

[[email protected] docker]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
7afd18cf46b8        registry            "/entrypoint.sh /e..."   23 hours ago        Up 23 hours         0.0.0.0:5000->5000/tcp   registry

[[email protected] docker]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                         PORTS                    NAMES
7de7af264a3f        ubuntu:v1           "bash"                   About a minute ago   Exited (0) 24 seconds ago                               sleepy_franklin
066f4d6839d8        ubuntu              "/bin/bash"              4 minutes ago        Exited (0) 3 minutes ago                                relaxed_liskov
99b075b89e01        ubuntu              "/bin/bash"              7 minutes ago        Exited (0) 5 minutes ago                                quirky_northcutt
5e75b82eaa03        httpd:my            "bash"                   44 minutes ago       Exited (0) 8 minutes ago                                heuristic_bose
9a9e0d4d8d5b        httpd:my            "httpd-foreground"       44 minutes ago       Exited (0) 44 minutes ago                               ecstatic_austin
7367ecbbc71c        httpd               "httpd-foreground"       58 minutes ago       Exited (0) 54 minutes ago                               httpd
2546b0337227        redis:latest        "docker-entrypoint..."   About an hour ago    Exited (0) About an hour ago                            redis
2b6ed6a380a0        ubuntu:latest       "/bin/bash"              About an hour ago    Exited (0) About an hour ago                            agitated_mirzakhani
a9449d7edb28        ubuntu:latest       "-d"                     About an hour ago    Created                                                 wizardly_volhard
0a1c1523c6dd        ubuntu:latest       "bash"                   About an hour ago    Exited (0) About an hour ago                            priceless_tesla
7afd18cf46b8        registry            "/entrypoint.sh /e..."   23 hours ago         Up 23 hours                    0.0.0.0:5000->5000/tcp   registry
8220a188b7f2        ubuntu              "/bin/echo ‘hello ..."   23 hours ago         Exited (0) 23 hours ago                                 blissful_panini
4a57565d7375        ubuntu              "bash"                   47 hours ago         Exited (0) 32 hours ago                                 elated_pasteur
599630fcf41d        ubuntu              "/bin/bash"              47 hours ago         Exited (0) 47 hours ago                                 gracious_brahmagupta
ad59d83cee87        ubuntu              "/bin/bash"              47 hours ago         Exited (0) 47 hours ago                                 objective_raman

[[email protected] docker]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
7de7af264a3fdd087f94328cae3961307420707330e3b6696d9cd4662c0c984f
066f4d6839d81a0e33bd075b3b22100da19dddf477f5f731ec44a7ad8afaf56e
99b075b89e010e2a3e5387166e7b354248689af46e2d9a7746b634db54931557
5e75b82eaa03ae198c6de7fe378db112edbfe5878beab1592e88e9d3aa44867c
9a9e0d4d8d5b8569ccdec74a88d5263bd13b3a1da24a1d52ac485918461bd93b
7367ecbbc71cd8504b873e1c9b9cdb59e5abc56f83429040a3db32ea3f2e535e
2546b03372278578804b74956ca45f7b8f6276096f859a1cc8c66e01238e674c
2b6ed6a380a0572d858f92794324c87d6c83c9ed108b9a642ba9661130fb9449
a9449d7edb28f02d46dda662b1a48e1e7cffab3fa0372a59195b0299bac21639
0a1c1523c6dd2b08a681ce458a60b52c769a812dc8ab61613bc441397ed43569
8220a188b7f24a0a60e17f925c9f3421056d046edc25a95d9432a508e1a8da36
4a57565d737598089cfe3366c2ba775ab7b88ee737eb1847b37f3e61f6e1390a
599630fcf41d6f7a456606965cd77873b3dc49f4763c9f7cbe0bfe20813c2366
ad59d83cee877a25d955ef1aa253e101ef66450c464d6e1d2b8ce14898482214

Total reclaimed space: 640 B

[[email protected] docker]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
7afd18cf46b8        registry            "/entrypoint.sh /e..."   23 hours ago        Up 23 hours         0.0.0.0:5000->5000/tcp   registry
[[email protected] docker]# 

7. 查看容易操作日志

docker logs -f ubuntu

8. docker inspect 查看所有变量

[[email protected]]# docker inspect ubuntu
[
    {
        "Id": "acee95e509e9c95ea5eb77599e03b674a94ab20bc0d9a33101857fc125545373",
        "Created": "2018-07-14T18:11:56.187157022Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4480,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-07-14T18:11:56.368810149Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:113a43faa1382a7404681f1b9af2f0d70b182c569aab71db497e33fa59ed87e6",
        "ResolvConfPath": "/var/lib/docker/containers/acee95e509e9c95ea5eb77599e03b674a94ab20bc0d9a33101857fc125545373/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/acee95e509e9c95ea5eb77599e03b674a94ab20bc0d9a33101857fc125545373/hostname",
        "HostsPath": "/var/lib/docker/containers/acee95e509e9c95ea5eb77599e03b674a94ab20bc0d9a33101857fc125545373/hosts",
        "LogPath": "/var/lib/docker/containers/acee95e509e9c95ea5eb77599e03b674a94ab20bc0d9a33101857fc125545373/acee95e509e9c95ea5eb77599e03b674a94ab20bc0d9a33101857fc125545373-json.log",
        "Name": "/ubuntu",
        "RestartCount": 0,
        "Driver": "overlay",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "my_volume:/home/liudong"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "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": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 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": "overlay",
            "Data": {
                "LowerDir": "/var/lib/docker/overlay/0f8fa7e8637e05c6094587fda3d786e0e0c26d9f530560d439bfaf41b17302b1/root",
                "MergedDir": "/var/lib/docker/overlay/c4cd806abb1f1d16dc5029285a5b648d99062c870b5f5f4092643d01a25d0bde/merged",
                "UpperDir": "/var/lib/docker/overlay/c4cd806abb1f1d16dc5029285a5b648d99062c870b5f5f4092643d01a25d0bde/upper",
                "WorkDir": "/var/lib/docker/overlay/c4cd806abb1f1d16dc5029285a5b648d99062c870b5f5f4092643d01a25d0bde/work"
            }
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "my_volume",
                "Source": "/var/lib/docker/volumes/my_volume/_data",
                "Destination": "/home/liudong",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "acee95e509e9",
            "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": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "ubuntu:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "6542e10d91e379b81fc2a79c059b77151d96a7ec4c745a84f62bf5e9a73c3f02",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/6542e10d91e3",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "2a0cc7ac632aec52aaf5227fdd2de27fd995467783e31466003c6240593f84bb",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "b260ea9e5346e40ced1bc6e19cd61bb11a4f4defa4e4ddf7a2e736f76e154b6e",
                    "EndpointID": "2a0cc7ac632aec52aaf5227fdd2de27fd995467783e31466003c6240593f84bb",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03"
                }
            }
        }
    }
]

9. 端口映射

9.1 docker port 查看映射端口配置

[[email protected]]# docker port registry 5000
0.0.0.0:5000

10. docker redis

参考:

[[email protected] _data]# docker run -p 6379:6379 -v $PWD/data:/data -d redis:latest redis-server
3cf7b0de4572a671e9b8206f0131ba683bc7f427439e376594025d1241852645
[[email protected] _data]# ls
data  test.txt
[[email protected] _data]# cd data
[[email protected] data]# ls
[[email protected] data]# cd ..
[[email protected] _data]# ls
data  test.txt
[[email protected] _data]# cd data
[[email protected] data]# ls
[[email protected] data]# docker exec -it 3cf7b0de4572a671e9b8206 redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name "dog"
OK
127.0.0.1:6379> get name
"dog"
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> ls
(error) ERR unknown command ‘ls‘
127.0.0.1:6379> 

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

Docker删除报错:Error response from daemon: conflict: unable to delete 08b152afcfae (must be forced)(代码片段

Docker决战到底 Jenkins的安装与使用

Docker安装与镜像的使用

Python3 - Docker Compose的安装与使用

Python3 - Docker Compose的安装与使用

Python3 - Docker Compose的安装与使用