1================================================================================html
docker 列出每一個容器的IP docker inspect 容器ID | grep IPAddress | java
docker inspect -f='{{.Name}} {{.NetworkSettings.IPAddress}} {{.HostConfig.PortBindings}}' $(docker ps -aq)mysql
1================================================================================sql
docker 實時查看日誌: docker logs -f -t --tail 200 creditappdocker
1================================================================================ubuntu
docker logs --tail 100 --follow --timestamps creditappcentos
1================================================================================ruby
查看docker 容器 網絡bash
docker inspect --format='{{.NetworkSettings.IPAddress}}' f82599a970ef網絡
1================================================================================
實時查看docker容器名爲s12的最後10行日誌 $ sudo docker logs -f -t --tail 10 mysql5.7
sudo docker logs -f -t --tail 10 s12
1================================================================================
Dockers 服務開機啓動:sudo systemctl enable docker.service
容器隨docker 啓動而啓動 docker run -d -p 9000:9000 \ --restart=always \ -v /var/run/docker.sock:/var/run/docker.sock \ --name prtainer-test \ docker.io/portainer/portainer
在docker run中加入 –privileged=true 給容器加上特定權限 (docker install mysql 報錯無權限時)docker run --name mysql5.7 -p 3306:3306 --restart=always -v /my/mysql/datadir:/var/lib/mysql -v /my/mysql/conf.d:/etc/mysql/conf.d --privileged=true -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
1================================================================================
進入容器
- $ sudo docker ps
- $ sudo docker exec -it 775c7c9ee1e1 /bin/bash
1================================================================================
查看容器運行日誌
$ docker logs --tail="10" mytest
1================================================================================
查看容器ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
1================================================================================
查看全部啓動命令
docker inspect container
docker 網絡命令
- docker network create
- docker network connect
- docker network ls
- docker network rm
- docker network disconnect
- docker network inspect
1================================================================================
docker 網絡配置(網橋)
ifconfig
brctl show //顯示docker0網橋和網橋上面的接口
iptables -t nat -L //顯示路由規則和NAT地址轉換
1================================================================================
導出鏡像
若是要存出鏡像到本地文件,可使用docker save命令。例如,存出本地的ubuntu:14.04鏡像爲文件ubuntu_14.04.tar:
$ sudo docker save -o /home/user/images/ubuntu_14.04.tar ubuntu:14.04
導入鏡像
可使用docker load從存出的本地文件中再導入到本地鏡像庫,例如從文件ubuntu_14.04.tar導入鏡像到本地鏡像列表,以下所示:
$ sudo docker load --input ubuntu_14.04.tar
1================================================================================
具體docker的優點我就不說了,下面就使用docker的一些經常使用命令具體詳細說下:
一、docker的啓動、中止、重啓
- [root@localhost ~]# service docker restart
- Redirecting to /bin/systemctl restart docker.service
- [root@localhost ~]# service docker stop
- Redirecting to /bin/systemctl stop docker.service
- [root@localhost ~]# service docker start
- Redirecting to /bin/systemctl start docker.service
二、docker建立一個容器
- [root@localhost ~]# docker run -it -v /docker_test:/yufei --name yufei_6 centos
- [root@724e7701f0d4 /]#
-i:容許咱們對容器內的 (STDIN) 進行交互
-t:在新容器內指定一個僞終端或終端
-v:是掛在宿機目錄, /docker_test是宿機目錄,/yufei是當前docker容器的目錄,宿機目錄必須是絕對的。
--name:是給容器起一個名字,可省略,省略的話docker會隨機產生一個名字
三、docker啓動的容器列表
- [root@localhost ~]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 724e7701f0d4 centos "/bin/bash" 4 minutes ago Up 4 minutes yufei_6
- f9097691663e centos "/bin/bash" 5 minutes ago Up 5 minutes yufei_5
- [root@localhost ~]#
三、查看docker建立的全部容器
- [root@localhost ~]# docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 724e7701f0d4 centos "/bin/bash" 5 minutes ago Up 5 minutes yufei_6
- f9097691663e centos "/bin/bash" 6 minutes ago Up 6 minutes yufei_5
- e59a540fb979 centos "/bin/base" 6 minutes ago Created yufei_4
- ff49dfedea4f centos "/bin/bash" 2 hours ago Exited (137) 10 minutes ago yufei_03
- d2cc70abb5a5 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago yufei_02
- 2d48fc5b7c17 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago yufei_01
- [root@localhost ~]#
docker ps 默認列表是正在啓動的容器 -a是顯示全部建立的容器
四、啓動、中止、重啓某個docker 容器
- [root@localhost ~]# docker start yufei_01
- yufei_01
- [root@localhost ~]# docker stop yufei_01
- yufei_01
- [root@localhost ~]# docker restart yufei_01
- yufei_01
- [root@localhost ~]#
五、查看指定容器的日誌記錄
- <span style="color:#333333;">[root@localhost ~]# docker logs -f yufei_01
- </span><span style=""><span style="color:#ff0000;">[root@2d48fc5b7c17 /]# ls
- anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- [root@2d48fc5b7c17 /]# exit
- exit
- [root@2d48fc5b7c17 /]# ls
- anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- [root@2d48fc5b7c17 /]#
- [root@2d48fc5b7c17 /]#
- [root@2d48fc5b7c17 /]#
- [root@2d48fc5b7c17 /]#
- [root@2d48fc5b7c17 /]#
- [root@2d48fc5b7c17 /]#
- [root@2d48fc5b7c17 /]#
- [root@2d48fc5b7c17 /]# cd /
- [root@2d48fc5b7c17 /]# ls
- anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
- [root@2d48fc5b7c17 /]# mkdir yufei
- [root@2d48fc5b7c17 /]# ls
- anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var yufei
- [root@2d48fc5b7c17 /]# cd yufei
- [root@2d48fc5b7c17 yufei]# ls
- yufei
- [root@2d48fc5b7c17 yufei]# cd yufei
- [root@2d48fc5b7c17 yufei]# ls
- application
- [root@2d48fc5b7c17 yufei]# cd ../../
- [root@2d48fc5b7c17 /]# rm -rf yufei
- [root@2d48fc5b7c17 /]# eixt
- bash: eixt: command not found
- [root@2d48fc5b7c17 /]# exit
- exit</span></span><span style="color:#333333;">
- [root@2d48fc5b7c17 /]# </span>
上面紅色部分是日誌命令部分。
六、刪除某個容器,若正在運行,須要先中止
- [root@localhost ~]# docker rm yufei_01
- Error response from daemon: You cannot remove a running container 2d48fc5b7c17b01e6247cbc012013306faf1e54f24651d5e16d6db4e15f92d33. Stop the container before attempting removal or use -f
- [root@localhost ~]# docker stop yufei_01
- yufei_01
- [root@localhost ~]# docker rm yufei_01
- yufei_01
- [root@localhost ~]#
七、刪除全部容器
- [root@localhost ~]# docker rm $(docker ps -a -q)
- Error response from daemon: You cannot remove a running container 724e7701f0d4a830167e21f75b470235a0e408fd6cc2913403426e96f69cba11. Stop the container before attempting removal or use -f
- Error response from daemon: You cannot remove a running container f9097691663ee36f9d2ee56afbdcca0eeb8b63e5590ddf18c0c42954c93b9f06. Stop the container before attempting removal or use -f
- [root@localhost ~]#
- [root@localhost ~]#
- [root@localhost ~]# docker stop yufei_6
- yufei_6
- [root@localhost ~]# docker stop yufei_5
- yufei_5
- [root@localhost ~]# docker rm $(docker ps -a -q)
- 724e7701f0d4
- f9097691663e
- [root@localhost ~]#