Docker containers Advanced Operations [Docker Series -3]

No public concern, we can reply, "blog Park" No background in public, freely available knowledge of Java / information interview must-see.

 

The article introduces the reader to an example of Nginx, Nginx configuration to be modified for purposes of such a container, when it started successfully, we inevitably need to be Nginx, then this modification to how to accomplish it? Look below.

This article is the third in this series, read the previous article contribute to a better understanding of this article:


1.Docker entry and installation [Docker series -1]

2.Docker container Basic Operation [Docker series-2]


Dependence container

docker attach

This is mainly dependent on container for purposes of interactive container, the command has some limitations, as you can understand, the real work to use less.

If I had docker attachthe command, first make sure the container has been started, then use this command to enter into the container. Specific steps are as follows:

  • Create a container, and then start:

  • Do not close the current window, and then open a new terminal, execute  docker attach ubuntu :

At this time, the container can enter commands to operate the line.

If the container has been closed container or containers is a background, the command overbuilt.

We can see from the above operation, the limitations of this great command, usage scenarios are not many, so we can understand as a.

Execute commands within the container

If the container started in the background, you can use the execute command in the container. Unlike used even when the user exits from the terminal, the container does not stop running, and the use , if the user exits from the terminal, then the container will be stopped. As shown below:dockerexecdocker attachdockerexecdocker attach

Based on such characteristics, we have after the inner container during operation, by basically achieved command.dockerexec

View container information

After the container is successfully created, the user can docker inspectview the details of the command vessel, these details include the id of the container, vessel name, environment variables, run the command, host configuration, network configuration, and volume configuration data and other information. Operative results as shown below:

使用 format 参数可以只查看用户关心的数据,例如:

  • 查看容器运行状态

  • 查看容器ip地址

  • 查看容器名、容器id

  • 查看容器主机信息

容器的详细信息,在我们后边配置容器共享目录、容器网络时候非常有用,这个我们到后面再来详细介绍。

查看容器进程

使用 docker top 命令可以查看容器中正在运行的进程,首先确保容器已经启动,然后执行 docker top 命令,如下:

查看容器日志

交互型容器查看日志很方便,因为日志就直接在控制台打印出来了,但是对于后台型容器,如果要查看日志,则可以使用docker提供的 docker logs 命令来查看,如下:

 

如下图,首先启动一个不停打日志的容器,然后利用 docker logs 命令查看日志,但是默认情况下只能查看到历史日志,无法查看实时日志,使用 -f 参数后,就可以查看实时日志了。

使用 --tail 参数可以精确控制日志的输出行数, -t 参数则可以显示日志的输出时间。

 

 

该命令在执行的过程中,首先输出最近的三行日志,同时由于添加了 -f 参数,因此,还会有其他日志持续输出。同时,因为添加了 -t 参数,时间随同日志一起打印出来了。

docker 的一大优势就是可移植性,容器因此 docker 容器可以随意的进行导入导出操作。

 

容器导出

既然是容器,我们当然希望 Docker 也能够像 VMWare 那样方便的在不同系统之间拷贝,不过 Docker 并不像 VMWare 导出容器那样方便(事实上,VMWare 中不存在容器导出操作,直接拷贝安装目录即可),在 Docker 中,使用 export 命令可以导出容器,具体操作如下:

  • 创建一个容器,进行基本的配置操作

本案例中我首先创建一个 nginx 容器,然后启动,启动成功后,将本地一个 index.html 文件上传到容器中,修改 nginx 首页的显示内容。具体操作步骤如下:

  1. docker run -itd --name nginx -p 80:80 nginx

  2. vi ./blog/docker/index.html

  3. docker cp ./blog/docker/index.html nginx:/usr/share/nginx/html/

 

首先运行一个名为 nginx 的容器,然后在宿主机中编辑一个 index.html 文件,编辑完成后,将该文件上传到容器中。然后在浏览器中输入 http://localhost:80 可以看到如下结果:

容器已经修改成功了。

 

接下来通过 export 命令将容器导出,如下:

该命令将容器导入到 docker 目录下。导出成功之后,我们就可以随意传播这个导出文件了,可以发送给其他小伙伴去使用了,相对于 VMWare 中庞大的文件,这个导出文件非常小。一般可能只有几百兆,当然也看具体情况。

 

容器导入

其他小伙伴拿到这个文件,通过执行如下命令可以导入容器(如果自己重新导入,需要记得将 docker 中和 nginx 相关的容器和镜像删除):

容器导入成功后,就可以使用 docker run 命令运行了。运行成功之后,我们发现自己定制的 index.html 页面依然存在,说明这是我们自己的那个容器。

 

参考资料:

[1] 曾金龙,肖新华,刘清.Docker开发实践[M].北京:人民邮电出版社,2015.

 


 

密封线

我们在知识星球,欢迎你加入

前1000人,50元/年

长按二维码▽

密封线

 


 

Java 极客技术公众号,是由一群热爱 Java 开发的技术人组建成立,专注分享原创、高质量的 Java 文章。如果您觉得我们的文章还不错,请帮忙赞赏、在看、转发支持,鼓励我们分享出更好的文章。

 

Guess you like

Origin www.cnblogs.com/justdojava/p/11271200.html