1 进入容器

[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS               NAMES
dcd05bed40d6        ansible/centos7-ansible   "/bin/bash"         37 seconds ago      Up 35 seconds                           nginx

启动

[root@localhost ~]# docker start nginx
nginx
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS               NAMES
dcd05bed40d6        ansible/centos7-ansible   "/bin/bash"         3 minutes ago       Up 20 seconds  
                   nginx

进入

[root@localhost ~]# docker exec -it dcd05bed40d6 /bin/bash
[root@dcd05bed40d6 ansible]# exit
exit
[root@localhost ~]# docker exec -it nginx /bin/bash
[root@dcd05bed40d6 ansible]# exit
exit

exec 表示在一个运行的容器中执行命令,参数-i,表示交互式执行,-t表示分配一个伪终端tty,
容器的名称和id都可以,通常都是执行的命令,分配到一个shell容器执行命令,一般都是/bin/bash

2、 查看容器的日志


[root@localhost /]# docker logs nginx
172.17.0.1 - - [31/Jul/2018:09:27:21 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0" "-"
[root@localhost /]# docker logs nginx
172.17.0.1 - - [31/Jul/2018:09:27:21 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0" "-"
[root@localhost /]# docker logs nginx >nginxaccess.log
[root@localhost /]# more nginxaccess.log 
172.17.0.1 - - [31/Jul/2018:09:27:21 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0" "-"

3 启动,停止 重启容器

[root@localhost /]# docker stop nginx
nginx
[root@localhost /]# docker start nginx
nginx
[root@localhost /]# docker restart nginx
nginx
[root@localhost /]# docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                NAMES
6af5902d98c0        nginx                     "nginx -g 'daemon of…"   5 minutes ago       Up 5 seconds        0.0.0.0:80->80/tcp   nginx
4bc701965bca        ansible/centos7-ansible   "/bin/bash"              34 minutes ago      Up 34 minutes                            ws
[root@localhost /]# docker logs nginx
172.17.0.1 - - [31/Jul/2018:09:27:21 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0" "-"

nginx实例操作

  1. 端口映射
    [root@localhost /]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    be8881be8156: Pull complete 
    32d9726baeef: Pull complete 
    87e5e6f71297: Pull complete 
    Digest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424
    Status: Downloaded newer image for nginx:latest
    [root@localhost /]# docker images
    REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
    nginx                     latest              c82521676580        6 days ago          109MB
    ansible/centos7-ansible   latest              688353a31fde        19 months ago       447MB
    [root@localhost /]# docker run -itd --name nginx  -p 80:80  docker.io/nginx
    6af5902d98c086d28964e09669347a74354436d06eb8620f6a6bc65064974d8e
    [root@localhost /]# docker ps
    CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                NAMES
    6af5902d98c0        nginx                     "nginx -g 'daemon of…"   48 seconds ago      Up 47 seconds       0.0.0.0:80->80/tcp   nginx
    [root@localhost /]# curl -I localhost:80
    HTTP/1.1 200 OK
    Server: nginx/1.15.2
    Date: Tue, 31 Jul 2018 09:27:21 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 24 Jul 2018 13:02:29 GMT
    Connection: keep-alive
    ETag: "5b572365-264"
    Accept-Ranges: bytes
    [root@localhost /]# netstat -ntlp | grep 80
    tcp6       0      0 :::80                   :::*                    LISTEN      2605/docker-proxy   
    [root@localhost /]# docker port nginx
    80/tcp -> 0.0.0.0:80

首先从公共仓库中获取到nginx的镜像,pull下载到本地;docker image查看本地镜像,然后根据镜像创建一个docker的实例,也就是容器nginx,然后运行实例,请求这个端口得到结果,查看本机已经监听了这个端口,docker port主要用来查看端口的映射关系。

2 查看网络

[root@localhost ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno1:  mtu 1500 qdisc mq state UP qlen 1000
    link/ether 08:94:ef:18:b0:8f brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.138/24 brd 192.168.10.255 scope global eno1
       valid_lft forever preferred_lft forever
    inet6 fe80::a94:efff:fe18:b08f/64 scope link 
       valid_lft forever preferred_lft forever
3: eno2:  mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 08:94:ef:18:b0:90 brd ff:ff:ff:ff:ff:ff
4: eno3:  mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 08:94:ef:18:b0:91 brd ff:ff:ff:ff:ff:ff
5: eno4:  mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 08:94:ef:18:b0:92 brd ff:ff:ff:ff:ff:ff
6: enp0s20u1u5:  mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 0a:94:ef:18:b0:96 brd ff:ff:ff:ff:ff:ff
7: docker0:  mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:78:60:47:92 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:78ff:fe60:4792/64 scope link 
       valid_lft forever preferred_lft forever
33: vethea9786a@if32:  mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether 4a:b6:db:6f:15:e7 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::48b6:dbff:fe6f:15e7/64 scope link 
       valid_lft forever preferred_lft forever
47: vetha11f427@if46:  mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether f6:4d:ab:6b:8e:3e brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::f44d:abff:fe6b:8e3e/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# ifconfig docker0
docker0: flags=4163  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:78ff:fe60:4792  prefixlen 64  scopeid 0x20
        ether 02:42:78:60:47:92  txqueuelen 0  (Ethernet)
        RX packets 195570  bytes 14682556 (14.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 184822  bytes 104464536 (99.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "6bdcec432a070b1e29a03ad5c706fd0aff49fd25bb9d0af139ae5c3f617c641e",
        "Created": "2018-07-31T11:32:44.832618166+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "4bc701965bca81f89b684a2aa2c320c0f2bb48e14a29f99f80e5d5bfa877df06": {
                "Name": "ws",
                "EndpointID": "5b0020a6cdb814dc7dff1c780fd86a8898c1172ca0d6269333d48e51d008cc7c",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "6af5902d98c086d28964e09669347a74354436d06eb8620f6a6bc65064974d8e": {
                "Name": "nginx",
                "EndpointID": "ade30754ab51e3e53008caac9affc0cb542e3316594cac0c623a8c75c200be2d",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

参考 https://mp.weixin.qq.com/s/-ePfdqp114JXjeNyrl7CxA