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

SSH into a running container

Change the working dir

Container's port is unaccessible from outsider host

No such host

Remove the old-dated images

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

Dockerfile best practices

useful commands for docker beginner的更多相关文章

  1. Common-used commands in Docker

    1. Start running a image in background mode docker run -it -d <image>:<tag> e.g. docker ...

  2. docker官方文档学习-1-Docker for mac安装配置

    https://docs.docker.com/docker-for-mac/ Get started with Docker for Mac 首先像在本博客docker-1-环境安装及例子实践处将环 ...

  3. 【云计算】docker的小知识,帮你更深入理解容器技术

    关于docker的15个小tip   1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubuntu): $ ID=$(docker run ubuntu e ...

  4. docker note

    docker --bip="10.1.42.1/16" -d 挂载宿主机目录 Docker支持挂载宿主机目录,支持宿主机目录和容器之间文件目录进行映射,彼此共享: docker r ...

  5. (转)关于docker的15个小tip

    转自:https://www.cnblogs.com/elnino/p/3899136.html 1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubunt ...

  6. Get started with Docker for Windows

    Welcome to Docker for Windows! Docker is a full development platform for creating containerized apps ...

  7. Docker:Deploy your app

    Prerequisites Install Docker. Get Docker Compose as described in Part 3 prerequisites. Get Docker Ma ...

  8. Docker GitHub 网站中 Readme.md 以技术者的角度翻译

    Docker 是一个开源的轻量级容器项目,用于让你的应用在它上面打包.集装和运行.Docker 运行的环境既包含未知硬件也包含未知操作系统.这句话的意思是它可以运行在任何地方,小到你的笔记本大到一个大 ...

  9. CentOS7系列--5.2CentOS7中配置和管理Docker

    CentOS7配置和管理Docker Docker是操作系统级别的虚拟化工具,它能自动化布署在容器中的应用 1. 安装Docker 1.1. 安装Docker相关软件 [root@server1 ~] ...

随机推荐

  1. H5实现摇一摇技术总结

    摇一摇遇到的问题 一.如何对摇晃效果进行反馈 刚开始的处理方式是,摇晃过程中不做任何处理,但后来反馈说这种效果不好,好像就没有摇动一样,如果声音也不响的话,就真的和什么都没发生一样. 后来想了想,加入 ...

  2. ABP文档 - Hangfire 集成

    文档目录 本节内容: 简介 集成 Hangfire 面板授权 简介 Hangfire是一个综合的后台作业管理器,可以在ABP里集成它替代默认的后台作业管理器,你可以为Hangfire使用相同的后台作业 ...

  3. Js 变量声明提升和函数声明提升

    Js代码分为两个阶段:编译阶段和执行阶段 Js代码的编译阶段会找到所有的声明,并用合适的作用域将它们关联起来,这是词法作用域的核心内容 包括变量声明(var a)和函数声明(function a(){ ...

  4. 如何正确使用日志Log

    title: 如何正确使用日志Log date: 2015-01-08 12:54:46 categories: [Python] tags: [Python,log] --- 文章首发地址:http ...

  5. Python-Jenkins API使用 —— 在后端代码中操控Jenkins

    最近在工作中需要用到在后台代码中触发Jenkins任务的构建,于是想到Jenkins是否有一些已经封装好的API类库提供,用于处理跟Jenkins相关的操作.下面就简单介绍下我的发现. Linux C ...

  6. ASP.NET 5 RC1 升级 ASP.NET Core 1.0 RC2 记录

    升级文档: Migrating from DNX to .NET Core Migrating from ASP.NET 5 RC1 to ASP.NET Core 1.0 RC2 Migrating ...

  7. MJRefresh 源码解读 + 使用

    MJRefresh这个刷新控件是一款非常好用的框架,我们在使用一个框架的同时,最好能了解下它的实现原理,不管是根据业务要求在原有的基础上修改代码,还是其他的目的,弄明白作者的思路和代码风格,会受益匪浅 ...

  8. 【踩坑速记】二次依赖?android studio编译运行各种踩坑解决方案,杜绝弯路,总有你想要的~

    这篇博客,只是把自己在开发中经常遇到的打包编译问题以及解决方案给大家稍微分享一下,不求吸睛,但求有用. 1.大家都知道我们常常会遇到dex超出方法数的问题,所以很多人都会采用android.suppo ...

  9. Android实现TCP断点上传,后台C#服务实现接收

    终端实现大文件上传一直都是比较难的技术,其中涉及到后端与前端的交互,稳定性和流量大小,而且实现原理每个人都有自己的想法,后端主流用的比较多的是Http来实现,因为大多实现过断点下载.但稳定性不能保证, ...

  10. C# 程序中嵌入百度地图

    本例是对WinForm中使用百度地图的简要介绍.百度地图目前支持Android开发,IOS开发,Web开发,服务接口,具体可以参照'百度地图开放平台'. [动态加载百度地图]涉及到的知识点: WebB ...