Docker Data Management
Data volumes
Create a data volume
docker volume create my-vol
See all data volumes
docker volume ls
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local my-vol
View details of the data volume
docker volume inspect my-vol
[root@localhost ~]# docker volume inspect my-vol
[
{
"CreatedAt": "2018-08-17T08:15:42+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
"Name": "my-vol",
"Options": {},
"Scope": "local"
}
]
Mount Hosting Directory
Host directory to mount the container
docker run -p 8080:8080 -d --mount type=bind,source=/root/first,target=/webapp centos-nodejs:1.0
[root@localhost ~]# docker run -p 8080:8080 -d --mount type=bind,source=/root/first,target=/webapp centos-nodejs:1.0
ecc09d5c7ecc5fd532bddcba6bdf1bca3db46f0554351e1a52e3c957c6759c0f
View container mounting information
docker inspect ecc09d5c7ecc
"Mounts": [
{
"Type": "bind",
"Source": "/root/first",
"Target": "/webapp"
}
],
Docker Network Configuration
Docker basic network configuration
External access to the container
When the starting container, or -P -p parameter used to specify the port mapping, -P randomly generated local port binding container designated port, -
P manually specify container port mapping host port
docker run -p 8080:8080 -d centos-nodejs:1.0
View port mapping information
docker ps
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ecc09d5c7ecc centos-nodejs:1.0 "node /app/index.js" About an hour ago Up About an hour 0.0.0.0:8080->8080/tcp xenodochial_mestorf
Port Mapping Description
The default mapping of all addresses of all ports
-p 8080:8080
效果同外部访问容器
Mapped to the specified address specified port of the machine
-p 127.0.0.1:8080:8080
docker run -p 127.0.0.1:8080:8080 -d centos-nodejs:1.0
[root@localhost ~]# docker run -p 127.0.0.1:8080:8080 -d centos-nodejs:1.0
6bb9a458519cc02ad3803e18d799f798a0c64cf36764a99f4b04f9a5a84f86cc
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6bb9a458519c centos-nodejs:1.0 "node /app/index.js" 3 seconds ago Up 2 seconds 127.0.0.1:8080->8080/tcp dreamy_panini
Mapped to any port of the host address
-p 127.0.0.1::8080
docker run -p 127.0.0.1::8080 -d centos-nodejs:1.0
[root@localhost ~]# docker run -p 127.0.0.1::8080 -d centos-nodejs:1.0
be23197aef2a41efd029c63fb266088cc1ad90dffe486107288fb77849778329
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be23197aef2a centos-nodejs:1.0 "node /app/index.js" 57 seconds ago Up 56 seconds 127.0.0.1:32768->8080/tcp inspiring_lamarr
View container port corresponding to the host port bindings
docker port be23197aef2a(容器id) 8080
[root@localhost ~]# docker port be23197aef2a 8080
127.0.0.1:32768
Interconnection container
Internet principle container
When Docker starts automatically created on the host docker0 a virtual bridge is actually a bridge Linux, and can be understood as a software switch. It will be forwarded to mount between its Ethernet port.
Meanwhile, Docker randomly assigned to a private network of a local unoccupied (defined in the RFC1918) address to a docker0 interface. For example, a typical 172.17.42.1, mask is 255.255.0.0. A network interface will automatically assign the same network segment (172.17.0.0/16) address within the container after the start.
When creating a Docker container when, at the same time creates a pair veth pair interfaces (when a packet is sent to the interface, another interface may receive the same data packet). This end of the interface in the container, i.e. eth0; locally and the other end mounted to docker0 bridge names start with Veth (e.g. vethAQI2QT). In this way, the host can communicate with each other with communication between the container, the container. Docker to create a virtual shared between the host and all containers networks.
Create your own virtual bridge
docker network create -d bridge my-bridge
Create two new container bridge to link two
docker run -it --name test5 --network my-bridge centos
docker run -it --name test6 --network my-bridge centos
Able to ping each other, each able to obtain their own local network segment IP.
In test6 ping test5
[root@9ce1b27d2ec8 /]# ping test5
PING test5 (172.18.0.2) 56(84) bytes of data.
64 bytes from test5.my-bridge (172.18.0.2): icmp_seq=1 ttl=64 time=0.120 ms
64 bytes from test5.my-bridge (172.18.0.2): icmp_seq=2 ttl=64 time=0.094 ms
64 bytes from test5.my-bridge (172.18.0.2): icmp_seq=3 ttl=64 time=0.086 ms
--- test5 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.086/0.100/0.120/0.014 ms
In test 6 to view its IP address
test6 IP address: 172.18.0.3
[root@9ce1b27d2ec8 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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
26: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.3/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
In test5 ping test6
[root@4abf9907b82c /]# ping test6
PING test6 (172.18.0.3) 56(84) bytes of data.
64 bytes from test6.my-bridge (172.18.0.3): icmp_seq=1 ttl=64 time=0.060 ms
64 bytes from test6.my-bridge (172.18.0.3): icmp_seq=2 ttl=64 time=0.075 ms
64 bytes from test6.my-bridge (172.18.0.3): icmp_seq=3 ttl=64 time=0.123 ms
--- test6 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.060/0.086/0.123/0.026 ms
View the ip address test5
[root@4abf9907b82c /]# ip a
test5:172.18.0.2
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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
24: eth0@if25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever