Docker基础(2)——基础操作

  • 来源:网络
  • 更新日期:2020-09-23

摘要:云计算 1.Docker登入docker hub账号,(Docker hub上申请的账号即可)[root@www ~]# docker login Login with your Docker ID to push and pul

云计算

1.Docker登入docker hub账号,(Docker hub上申请的账号即可)

[root@www ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: ******
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
 
Login Succeeded

2.搜索镜像库中的镜像;

[root@www ~]# docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        10250               [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1462                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   647                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   438                                     [OK]
kong                                                   Open-source Microservice & API Management la…   247                 [OK]                
webdevops/php-nginx                                    Nginx with PHP-FPM                              117                                     [OK]
kitematic/hello-world-nginx                            A light-weight nginx container that demonstr…   112                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   75                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      58                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-Mysql-5   ubuntu-16-nginx-php-phpmyadmin-MYSQL-5          47                                      [OK]
linuxserver/nginx                                      An Nginx container, brought to you by LinuxS…   43                                      
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   23                                      [OK]
blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
nginx/nginx-ingress                                    NGINX Ingress Controller for Kubernetes         12                                      
wodby/drupal-nginx                                     Nginx for Drupal container image                11                                      [OK]
centos/nginx-18-centos7                                Platform for running nginx 1.8 or building n…   8                                       
nginxdemos/hello                                       NGINX webserver that serves a simple page co…   8                                       [OK]
centos/nginx-112-centos7                               Platform for running nginx 1.12 or building …   5                                       
1science/nginx                                         Nginx Docker images that include Consul Temp…   4                                       [OK]
travix/nginx                                           NGinx reverse proxy                             2                                       [OK]
pebbletech/nginx-proxy                                 nginx-proxy sets up a container running ngin…   2                                       [OK]
mailu/nginx                                            Mailu nginx frontend                            2                                       [OK]
toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on same…   1                                       [OK]
wodby/nginx                                            Generic nginx                                   0                                       [OK]
ansibleplaybookbundle/nginx-apb                        An APB to deploy NGINX                          0                                       [OK]

3.docker拉取存在docker hub上的镜像至本地(下载Docker hub上的镜像);

[root@www ~]# docker pull httpd:2.4.37-alpine
2.4.37-alpine: Pulling from library/httpd
4fe2ade4980c: Already exists
42101a4e4c4e: Pull complete
73eadb9961ff: Pull complete
830983fb5ec2: Pull complete
78fb51bbfc27: Pull complete
Digest: sha256:b875793145fe613aa2d1f73f1cd8ec09b775abcd6980024bc60a6a9c73d644fc
Status: Downloaded newer image for httpd:2.4.37-alpine
[root@www ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
httpd               2.4.37-alpine       11fc0c2a2dfa        4 days ago          91.8MB
nginx               1.14-alpine         77bae8d00654        13 days ago         17.7MB
ljymyy/nginx        latest              77bae8d00654        13 days ago         17.7MB

4.基于容器制作镜像

[root@www ~]# docker container  run --name httpd1 httpd:2.4.37-alpine
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Tue Nov 20 09:19:04.710155 2018] [mpm_event:notice] [pid 1:tid 140394175941512] AH00489: Apache/2.4.37 (Unix) configured -- resuming normal operations
[Tue Nov 20 09:19:04.719195 2018] [core:notice] [pid 1:tid 140394175941512] AH00094: Command line: 'httpd -D FOREGROUND'
[root@www ~]# docker exec -it httpd1 /bin/sh
/usr/local/apache2 #
/usr/local/apache2 # vi htdocs/index.html
/usr/local/apache2 # exit
[root@www ~]# docker commit httpd1 ljymyy/httpd:private
sha256:0c28c4522c128a7dca17c26d2fd8eee4e57642b455c067832d5567da1128da5e
[root@www ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ljymyy/httpd        private             0c28c4522c12        6 seconds ago       91.8MB
httpd               2.4.37-alpine       11fc0c2a2dfa        4 days ago          91.8MB
nginx               1.14-alpine         77bae8d00654        13 days ago         17.7MB
ljymyy/nginx        latest              77bae8d00654        13 days ago         17.7MB
 
 
[root@www ~]# curl 172.17.0.2
<html><body><h2>It works!</h2></body></html>
 
 
test successful

 

测试:

[root@www ~]# docker run --name httpd2 --rm ljymyy/httpd:private
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Tue Nov 20 09:39:27.805890 2018] [mpm_event:notice] [pid 1:tid 140674896550792] AH00489: Apache/2.4.37 (Unix) configured -- resuming normal operations
[Tue Nov 20 09:39:27.806444 2018] [core:notice] [pid 1:tid 140674896550792] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [20/Nov/2018:09:39:39 +0000] "GET / HTTP/1.1" 200 63
 
 
[root@www ~]# curl 172.17.0.3
<html><body><h2>It works!</h2></body></html>
 
 
test successful

5.为镜像打标签;(IMAGE ID为相同的)

[root@www ~]# docker tag ljymyy/httpd:private ljymyy/httpd:v2.0
[root@www ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ljymyy/httpd        private             0c28c4522c12        5 minutes ago       91.8MB
ljymyy/httpd        v2.0                0c28c4522c12        5 minutes ago       91.8MB
httpd               2.4.37-alpine       11fc0c2a2dfa        4 days ago          91.8MB
ljymyy/nginx        latest              77bae8d00654        13 days ago         17.7MB
nginx               1.14-alpine         77bae8d00654        13 days ago         17.7MB


6.将本地镜像上传至docker hub库中;

在docker hub上的dashboard中创建一个个人的Repository;创建相对应的namespace和repository name;例如:ljymyy/httpd

[root@www ~]# docker push ljymyy/httpd:v2.0
The push refers to repository [docker.io/ljymyy/httpd]
9dd07a03f263: Layer already exists
86503dd49098: Layer already exists
9caa02067460: Layer already exists
05f824de9453: Layer already exists
7df053e22514: Layer already exists
df64d3292fd6: Layer already exists
v2.0: digest: sha256:5b3aea8b8ed49fc642f46282c8bfeabf023fcbde0635341401a37ba0778af2a3 size: 1569

7.将镜像导入至文件中和导出文件中的镜像至本地;

[root@www ~]# docker save  -o test.file nginx:1.14-alpine
[root@www ~]# docker load -q -i test.file
Loaded image: nginx:1.14-alpine

8.显示容器的详细信息;

[root@www ~]# docker container  ls
CONTAINER ID        IMAGE                 COMMAND              CREATED             STATUS              PORTS               NAMES
6246bc53550b        httpd:2.4.37-alpine   "httpd-foreground"   3 hours ago         Up 18 seconds       80/tcp              httpd1
[root@www ~]# docker inspect httpd1
[
    {
        "Id": "6246bc53550bcfa1149f9f51b48a5b992969d3e2eb50df457cb0f06589102a01",
        "Created": "2018-11-20T09:18:58.780749029Z",
        "Path": "httpd-foreground",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 67850,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-11-20T11:50:26.409306755Z",
            "FinishedAt": "2018-11-20T09:40:54.02714799Z"
        },
        "Image": "sha256:11fc0c2a2dfa90eb5804742e5b0927c9cacc3664ffde7839dcc6f39e6fd44fcb",
        "ResolvConfPath": "/var/lib/docker/containers/6246bc53550bcfa1149f9f51b48a5b992969d3e2eb50df457cb0f06589102a01/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/6246bc53550bcfa1149f9f51b48a5b992969d3e2eb50df457cb0f06589102a01/hostname",
        "HostsPath": "/var/lib/docker/containers/6246bc53550bcfa1149f9f51b48a5b992969d3e2eb50df457cb0f06589102a01/hosts",
        "LogPath": "/var/lib/docker/containers/6246bc53550bcfa1149f9f51b48a5b992969d3e2eb50df457cb0f06589102a01/6246bc53550bcfa1149f9f51b48a5b992969d3e2eb50df457cb0f06589102a01-json.log",
        "Name": "/httpd1",
        "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,
            "dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "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,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/asound",
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/02719a1efc3bd4ced5a4692b75468dd4d2d55fa254869cbc26d9487ebea1c5ea-init/diff:/var/lib/docker/overlay2/0403b4e021b6fb5d30d752fa1f8d0dbe7e5b5fae974ddcc3acc8c32fe49e51f8/diff:/var/lib/docker/overlay2/7e600ef93457c34d8423b1fa59037800b630f992ca26b0289106f4bb4edbbda8/diff:/var/lib/docker/overlay2/56de63abd81931dc78929016b95e51e3d74f4702d90af28590b31ad44bd9335e/diff:/var/lib/docker/overlay2/9efe9a470ec8834a8605274fa2b156015bc703c790b429ac13fdd10f19693ccd/diff:/var/lib/docker/overlay2/2cd347e2a7d82ceeb12f18b279dda0fa0853f13ab3149aa0787f3f7c685ba622/diff",
                "MergedDir": "/var/lib/docker/overlay2/02719a1efc3bd4ced5a4692b75468dd4d2d55fa254869cbc26d9487ebea1c5ea/merged",
                "UpperDir": "/var/lib/docker/overlay2/02719a1efc3bd4ced5a4692b75468dd4d2d55fa254869cbc26d9487ebea1c5ea/diff",
                "WorkDir": "/var/lib/docker/overlay2/02719a1efc3bd4ced5a4692b75468dd4d2d55fa254869cbc26d9487ebea1c5ea/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "6246bc53550b",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "HTTPD_PREFIX=/usr/local/apache2",
                "HTTPD_VERSION=2.4.37",
                "HTTPD_SHA256=3498dc5c6772fac2eb7307dc7963122ffe243b5e806e0be4fb51974ff759d726",
                "HTTPD_PATCHES=",
                "APACHE_DIST_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename= \\thttps://www-us.apache.org/dist/ \\thttps://www.apache.org/dist/ \\thttps://archive.apache.org/dist/"
            ],
            "Cmd": [
                "httpd-foreground"
            ],
            "ArgsEscaped": true,
            "Image": "httpd:2.4.37-alpine",
            "Volumes": null,
            "WorkingDir": "/usr/local/apache2",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "948daf1d078778001fa043b74a10733c11e215be3a8b457b16a47d8b46f087aa",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/948daf1d0787",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "a591d3f3fbf35606173c7f121a3e23503d075473d256cae6c55933cf91815172",
            "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": "6714eb554a2843bd0582a3e7d63b09b175c5351c100aab08b1a051bc8187cb5c",
                    "EndpointID": "a591d3f3fbf35606173c7f121a3e23503d075473d256cae6c55933cf91815172",
                    "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
                }
            }
        }
    }
]


新网虚拟主机