8

docker学习笔记2-容器常用操作

 1 year ago
source link: http://saiya.xyz/2020/12/17/docker2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

将宿主机/home/saiya/test目录拷贝到容器96f7f14e99ab的/www目录下。

docker cp /home/saiya/test 96f7f14e99ab:/www/

将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中

docker cp  96f7f14e99ab:/www /tmp/

docker attach 或者docker exec
建议使用docker exec

docker exec -it my_tomcat bash
docker logs -f <容器名称或者id>

构建自定义镜像,注意命令最后的点,表示当前目录

docker build -t mytomcat:1.5 .

docker查看容器的网络ip

可直接获得容器的ip地址如:172.18.0.4

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

显示所有容器IP地址:

docker inspect --format='{{.Name}} - {{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

常用方法有两种

docker inspect 容器ID | grep IPAddress

查看docker name:

sudo docker inspect -f='{{.Name}}' $(sudo docker ps -a -q)

查看dockers ip:

sudo docker inspect -f='{{.NetworkSettings.IPAddress}}' $(sudo docker ps -a -q)

综上,我们可以写出以下脚本列出所有容器对应的名称,端口,及ip

docker inspect -f='{{.Name}} {{.NetworkSettings.IPAddress}} {{.HostConfig.PortBindings}}' $(docker ps -aq)

查看容器的网络组成

不显示的通过--net参数指定网络,默认都是docker0。

  安装 Docker 时,它会自动创建 3 个网络。可以使用 docker network ls命令列出这些网络。

  $ docker network ls 
  NETWORK ID          NAME                DRIVER 
  7fca4eb8c647        bridge              bridge 
  9f904ee27bf5        none                null 
  cf03ee007fb4        host                host
  查看网络包含的容器等
  [root@localhost ~]# docker network inspect test-network 
  [ 
     { 
         "Name": "test-network", 
         "Id": "e2f569d57eb8506602fdfc3e8a20b12073782dcfd6046ce4ef76de8db3275d21", 
         "Scope": "local", 
         "Driver": "bridge", 
         "IPAM": { 
             "Driver": "default", 
             "Config": [ 
                 {} 
             ] 
         }, 
         "Containers": { 
             "7699fe682353835166482d1416a70f0361ddf88940bd7102e2f84f6d46b3d113": { 
                 "EndpointID": "cf1f3e319a1c1ec83c3eaf0d5380b9ee50c1f2d37e713425996ada6788f8e77a", 
                 "MacAddress": "02:42:ac:12:00:02", 
                 "IPv4Address": "172.18.0.2/16", 
                 "IPv6Address": ""
             } 
         }, 
         "Options": {} 
     } 
  ]

也可以动态地将容器连接到网络。

  [root@localhost ~]# docker run -itd --name=test2 ppc64le/busybox /bin/sh 
  69d79cddbdf8a920d24993bfc16e8d064479327d8cc23f10ae25e96a2b9b057a 

  [root@localhost ~]# docker network connect test-network test2 
  [root@localhost ~]# docker network inspect test-network 
  [ 
     { 
         "Name": "test-network", 
         "Id": "e2f569d57eb8506602fdfc3e8a20b12073782dcfd6046ce4ef76de8db3275d21", 
         "Scope": "local", 
         "Driver": "bridge", 
         "IPAM": { 
             "Driver": "default", 
             "Config": [ 
                 {} 
             ] 
         }, 
         "Containers": { 
             "69d79cddbdf8a920d24993bfc16e8d064479327d8cc23f10ae25e96a2b9b057a": { 
                 "EndpointID": "5dc4877e71eca2243167e97153d56c5334d98991fcc3eb2f7f968d0b68416255", 
                 "MacAddress": "02:42:ac:12:00:03", 
                 "IPv4Address": "172.17.0.3/16", 
                 "IPv6Address": ""
             }, 
             "7699fe682353835166482d1416a70f0361ddf88940bd7102e2f84f6d46b3d113": { 
                 "EndpointID": "cf1f3e319a1c1ec83c3eaf0d5380b9ee50c1f2d37e713425996ada6788f8e77a", 
                 "MacAddress": "02:42:ac:12:00:02", 
                 "IPv4Address": "172.17.0.2/16", 
                 "IPv6Address": ""
             } 
         }, 
         "Options": {} 
     } 
  ]

为容器添加网络

docker network connect host 容器名称

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK