useful commands for docker beginner
You may want to add my wechat public account or add my technical blog's RSS feed
This list is meant to record some useful docker commands, it can help docker beginners to solve the problems they met. I'll try to keep this list current and up to date. Please leave your comments if you want to share some useful docker related commands.
How to inspect/debug a failed docker build
Tail the logs of a running container
Container's port is unaccessible from outsider host
Automatically remove the container when it exits
Update 2017-3-25:
Copy file to or from a running container
Update 2017-4-3:
Clean up your docker system
Update 2017-4-8:
Display container resource usage statistics
Update 2017-5-18:
Clear container logs
Find the log location for a container
How to inspect/debug a failed docker build
After docker successfully executes a RUN command from a Dockerfile, a new layer in the image filesystem is committed. Conveniently you can use those layers's ids as images to start a new container. So when one of the Dockerfile command fails, what you need to do is to look for the id of the preceding layer and run a shell in a container created from that id:
docker run --rm -it <id_last_working_layer> bash -il
Once in the container, you can try the command failed to reproduce the issue, fix and test it. Finally update your Dockerfile with the fixed command.
But what if the failed command takes several hours? Rewinding prior to the failed command and running it again will be frustrating. The solution is running into the container in which the command failed.
First, find the container that failed:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6934ada98de6 42e0228751b3 "/bin/sh -c './utils/" 24 minutes ago Exited (1) About a minute ago sleepy_bell
Second, commit it to an image:
$ docker commit 6934ada98de6
sha256:7015687976a478e0e94b60fa496d319cdf4ec847bcd612aecf869a72336e6b83
And then run the image [if necessary, running bash]:
$ docker run -it 7015687976a4 bash -il
Now you are actually looking at the state of the build at the time that it failed, instead of at the time before running the command that caused the failure.
Tail the logs of a running container
docker logs -f container-id
If the container has too much logs, you can use --tail number to specify number of lines to show from the end of the logs
SSH into a running container
sudo docker exec -it container-id /bin/bash
-i, --interactive, Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
NOTE:
docker attach will let you connect to your Docker container, but this isn't really the same thing as ssh. If your container is running a web server, for example, docker attach will probably connect you the stdout of the web service process
Change the working dir
RUN cd /tmp doesn't have any effect on the next instruction, use WORKDIR /tmp instead, the Docker daemon runs instruction in the Dockerfile one-by-one, committing the result of each instruction to a new image if necessary, so RUN cd /tmp will not have any effect on the next instructions.
Container's port is unaccessible from outsider host
Already EXPOSE port on Dockerfile, but port is unaccessible from outsider host.
The EXPOSE instruction informs Docker that the container listens on the specified network ports at run time. EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports.
No such host
This error occurred:
dial tcp: lookup auth.docker.io on 10.0.2.3:53: no such host
You can solve this by add DNS server
docker-machine ssh
echo "nameserver 8.8.8.8" > /etc/resolv.conf
Remove the old-dated images
Images will take disk spaces, you may want to remove the old dated images:
docker images | grep "weeks" | awk '{print $3}' | xargs --no-run-if-empty docker rmi
I think you can think up the command to remove all untagged images
Automatically remove the container when it exits
The containers that are not running are not taking any system resources besides disk space. It is usually good to clean up the container when the container exits:
docker run --rm ubuntu:14.04
--rm Automatically remove the container when it exits
Too many open files
Change the max open files then restart docker service
ulimit -a
ulimit -n 100000 #change max open files
Debug an stopped container
docker start -ai container-id
-a, --attach=false
-i, --interactive=false
Copy file to or from a running container
Copy file from a running container
docker cp container:src_path dest_path
Copy file to a running container
docker cp src_path container:dest_path
Clean up your docker system
Show docker disk usage:
docker system df
Remove unused data:
docker system prune
Display container resource usage statistics
Display a live stream of continer(s) resource(CPU/Memory/IO) usage statistics
$ docker stats
Clear container logs
$ docker logs -c (clear) <container>
Find the log location for a container
$ docker inspect --format='{{.LogPath}}' $INSTANCE_ID
Other Resources You Should Know
useful commands for docker beginner的更多相关文章
- Common-used commands in Docker
1. Start running a image in background mode docker run -it -d <image>:<tag> e.g. docker ...
- docker官方文档学习-1-Docker for mac安装配置
https://docs.docker.com/docker-for-mac/ Get started with Docker for Mac 首先像在本博客docker-1-环境安装及例子实践处将环 ...
- 【云计算】docker的小知识,帮你更深入理解容器技术
关于docker的15个小tip 1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubuntu): $ ID=$(docker run ubuntu e ...
- docker note
docker --bip="10.1.42.1/16" -d 挂载宿主机目录 Docker支持挂载宿主机目录,支持宿主机目录和容器之间文件目录进行映射,彼此共享: docker r ...
- (转)关于docker的15个小tip
转自:https://www.cnblogs.com/elnino/p/3899136.html 1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubunt ...
- Get started with Docker for Windows
Welcome to Docker for Windows! Docker is a full development platform for creating containerized apps ...
- Docker:Deploy your app
Prerequisites Install Docker. Get Docker Compose as described in Part 3 prerequisites. Get Docker Ma ...
- Docker GitHub 网站中 Readme.md 以技术者的角度翻译
Docker 是一个开源的轻量级容器项目,用于让你的应用在它上面打包.集装和运行.Docker 运行的环境既包含未知硬件也包含未知操作系统.这句话的意思是它可以运行在任何地方,小到你的笔记本大到一个大 ...
- CentOS7系列--5.2CentOS7中配置和管理Docker
CentOS7配置和管理Docker Docker是操作系统级别的虚拟化工具,它能自动化布署在容器中的应用 1. 安装Docker 1.1. 安装Docker相关软件 [root@server1 ~] ...
随机推荐
- 学习AOP之透过Spring的Ioc理解Advisor
花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- SQL Server-聚焦UNIOL ALL/UNION查询(二十三)
前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...
- WebApi - 路由
这段时间的博客打算和大家一起分享下webapi的使用和心得,主要原因是群里面有朋友说希望能有这方面的文章分享,随便自己也再回顾下:后面将会和大家分不同篇章来分享交流心得,希望各位多多扫码支持和点赞,谢 ...
- 如何利用tcpdump对mysql进行抓包操作
命令如下: tcpdump -s -l -w - dst -i eno16777736 |strings 其中-i指定监听的网络接口,在RHEL 7下,网络接口名不再是之前的eth0,而是 eno16 ...
- 界面设计技法之css布局
css布局之于页面就如同ECMAScript之于JS一般,细想一番,html就如同语文,css就如同数学,js呢,就是物理,有些扯远,这里就先不展开了. 回到主题,从最开始的css到如今的sass(l ...
- 调用微信退款接口或发红包接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
我总结了一下出现证书无法加载的原因有以下三个 1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应 ...
- Configure a VLAN on top of a team with NetworkManager (nmcli) in RHEL7
SOLUTION VERIFIED September 13 2016 KB1248793 Environment Red Hat Enterprise Linux 7 NetworkManager ...
- PHP 设计模式概述
一.设计模式(Design pattern)是什么? 设计模式是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. ...
- Java虚拟机 JVM
finalize();(不建议使用,代价高,不确定性大) 如果你在一个类中覆写了finalize()方法, 那么你可以在第一次被GC的时候,挽救一个你想挽救的对象,让其不被回收,但只能挽救一次. GC ...