docker安装并修改Nginx镜像

1、安装nginx镜像,命令:docker pull nginx

2、创建nginx容器,并启动,命令:docker run --name webserver -d -p 192.168.51.227:9999:80 nginx

  run = create容器+start容器;

  --name:指定容器的名字;

  -d:后台运行;

  -p:使用 -p 参数时,可以指定要映射的端口,并且在一个指定的端口上只可以绑定一个容器。支持的格式有:

  1. IP:HostPort:ContainerPort
  2. IP:ContainerPort
  3. HostPort:ContainerPort

  nginx:要创建容器使用的镜像

  注:-p参数端口映射,不指定IP可能远程无法访问。

3、查看启动中的容器的信息,命令:docker ps

[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
cd6957191c52        nginx               "nginx -g 'daemon ..."   3 minutes ago       Up 3 minutes        192.168.51.227:9999->80/tcp   webserver

4、查看容器端口映射相关信息,命令:docker port webserver

[root@localhost nginx]# docker port webserver
80/tcp -> 192.168.51.227:9999

5、查看容器详细信息,命令:docker inspect cd6957191c52

[root@localhost nginx]# docker inspect cd6957191c52
[
    {
        "Id": "cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d",
        "Created": "2018-06-01T01:06:34.089549987Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 17659,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-06-01T01:06:34.307275905Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ae513a47849c895a155ddfb868d6ba247f60240ec8495482eca74c4a2c13a881",
        "ResolvConfPath": "/var/lib/docker/containers/cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d/hostname",
        "HostsPath": "/var/lib/docker/containers/cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d/hosts",
        "LogPath": "",
        "Name": "/webserver",
        "RestartCount": 0,
        "Driver": "overlay2",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "journald",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "192.168.51.227",
                        "HostPort": "9999"
                    }
                ]
            },
            "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,
            "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": "overlay2",
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/d398ea082f4d98f4c42a66576114d08741846ff5ed42dc7630d28467d0df7e5d-init/diff:/var/lib/docker/overlay2/7b46becc5c07932a491ba03fa4a63cdcfa82c3939565109ec042123212355eb9/diff:/var/lib/docker/overlay2/7f407789f9c68b0b99591d4fdce67e3a13fb8a88d70a5c86aae4ef0ed41ca577/diff:/var/lib/docker/overlay2/3bb3dbd12f24a463d2c800dbba46b1bccee27557b670396426efff1fdec55539/diff",
                "MergedDir": "/var/lib/docker/overlay2/d398ea082f4d98f4c42a66576114d08741846ff5ed42dc7630d28467d0df7e5d/merged",
                "UpperDir": "/var/lib/docker/overlay2/d398ea082f4d98f4c42a66576114d08741846ff5ed42dc7630d28467d0df7e5d/diff",
                "WorkDir": "/var/lib/docker/overlay2/d398ea082f4d98f4c42a66576114d08741846ff5ed42dc7630d28467d0df7e5d/work"
            }
        },
        "Mounts": [],
        "Config": {
            "Hostname": "cd6957191c52",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.13.12-1~stretch",
                "NJS_VERSION=1.13.12.0.2.0-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "c72e2e3656c36f679b1d6db45c36b586b5643acc17f1a429ae7f38525458ae8a",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "192.168.51.227",
                        "HostPort": "9999"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/c72e2e3656c3",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "f010ca8232a1a8d1054305ecc49bf0d3ca4666b5a9c6b1a2d5b060a209d963d2",
            "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": "ec84265a9aadee0c581ebeec22571e0f1c151dd96f0f6bbde7abad7148a0e3ce",
                    "EndpointID": "f010ca8232a1a8d1054305ecc49bf0d3ca4666b5a9c6b1a2d5b060a209d963d2",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02"
                }
            }
        }
    }
]

 

6、获得容器IP,命令:docker inspect cd6957191c52 |grep IPAddress

[root@localhost nginx]# docker inspect cd6957191c52 |grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

 

7、访问nginx:http://192.168.51.227:9999/

8、容器的停止,命令:docker stop cd6957191c52

[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
cd6957191c52        nginx               "nginx -g 'daemon ..."   30 minutes ago      Up 30 minutes       192.168.51.227:9999->80/tcp   webserver
[root@localhost nginx]# docker stop cd6957191c52
cd6957191c52
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost nginx]# 

9、查看不在运行状态的容器,命令:docker ps -a

[root@localhost nginx]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                          PORTS               NAMES
cd6957191c52        nginx               "nginx -g 'daemon ..."   32 minutes ago      Exited (0) About a minute ago                       webserver

10、启动容器,命令:docker start cd6957191c52

11、进入已经创建的容器,命令:docker exec -it cd6957191c52 bash

[root@localhost nginx]# docker start cd6957191c52
cd6957191c52
[root@localhost nginx]# docker exec -it cd6957191c52 /bin/bash
root@cd6957191c52:/# ls
bin  boot  dev    etc  home  lib    lib64  media  mnt  opt    proc  root  run  sbin  srv  sys  tmp  usr  var
root@cd6957191c52:/# 

12、修改nginx访问页面内容

[root@localhost nginx]# docker exec -it cd6957191c52 bash
root@cd6957191c52:/# 
root@cd6957191c52:/# 
root@cd6957191c52:/# 
root@cd6957191c52:/# echo '<h1>Hello,Docker!</h1>' > /usr/share/nginx/html/index.html
root@cd6957191c52:/# exit
exit

13、刷新页面或者重新访问:http://192.168.51.227:9999/,见下图

14、我们修改了容器的文件,也就是改动了容器的存储层。我们可以通过 docker的diff 命令看到具体的改动,命令如下:docker diff webserver

[root@localhost nginx]# docker diff webserver
C /root
A /root/.bash_history
C /var
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/uwsgi_temp
A /var/cache/nginx/client_temp
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
C /usr
C /usr/share
C /usr/share/nginx
C /usr/share/nginx/html
C /usr/share/nginx/html/index.html
C /run
A /run/nginx.pid
A /run/secrets

15、现在我们定制好了变化,我们希望能将其保存下来形成镜像。要知道,当我们运行一个容器的时候(如果不使用卷的话),我们做的任何文件修改都会被记录于容器存储层里。而 Docker 提供了一个 docker commit 命令,可以将容器的存储层保存下来成为镜像。换句话说,就是在原有镜像的基础上,再叠加上容器的存储层,并构成新的镜像。以后我们运行这个新镜像的时候,就会拥有原有容器最后的文件变化。


  docker commit 的语法格式为:
  docker commit [选项] <容器ID或容器名> [<仓库名>[:<标签>]]


我们可以用下面的命令将容器保存为镜像:docker commit --author "dyh <dyh@126.com>" --message "修改默认网页" webserver nginx:v2

[root@localhost nginx]# docker commit --author "dyh <dyh@126.com>" --message "修改默认网页" webserver nginx:v2
sha256:1eca2575e4f55cac30e2b7f0dbcb719696a0dd8a68c46181c5c7c91205275f12

16、查看镜像信息,命令:docker images nginx

[root@localhost nginx]# docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
nginx               v2                  1eca2575e4f5        About a minute ago   109 MB
docker.io/nginx     latest              ae513a47849c        4 weeks ago          109 MB

17、查看镜像内的历史记录,命令:docker history nginx:v2

[root@localhost nginx]# docker history nginx:v2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
1eca2575e4f5        12 minutes ago      nginx -g daemon off;                            103 B               修改默认网页
ae513a47849c        4 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daem...   0 B                 
<missing>           4 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL [SIGTERM]         0 B                 
<missing>           4 weeks ago         /bin/sh -c #(nop)  EXPOSE 80/tcp                0 B                 
<missing>           4 weeks ago         /bin/sh -c ln -sf /dev/stdout /var/log/ngi...   22 B                
<missing>           4 weeks ago         /bin/sh -c set -x  && apt-get update  && a...   53.7 MB             
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=1.13.12...   0 B                 
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.13....   0 B                 
<missing>           4 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX ...   0 B                 
<missing>           4 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0 B                 
<missing>           4 weeks ago         /bin/sh -c #(nop) ADD file:ec5be7eec56a749...   55.3 MB

 

18、使用新的镜像创建容器,命令:docker run --name webserver2 -d -p 192.168.51.227:8888:80 nginx:v2

[root@localhost nginx]# docker run --name webserver2 -d -p 192.168.51.227:8888:80 nginx:v2
10af87f7973e9c04e352783521eb0f50bdbd6f3e1de85d2517e3c177977f3e5a

19、查看镜像进程

[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
10af87f7973e        nginx:v2            "nginx -g 'daemon ..."   5 seconds ago       Up 5 seconds        192.168.51.227:8888->80/tcp   webserver2
cd6957191c52        nginx               "nginx -g 'daemon ..."   About an hour ago   Up 41 minutes       192.168.51.227:9999->80/tcp   webserver

20、访问新容器:http://192.168.51.227:8888/

 

posted @ 2018-06-01 10:29  kszsa  阅读(1417)  评论(0编辑  收藏  举报