Docker containers Basic Operation [Docker series-2]

Docker entry and installation [Docker series -1]


Like a mirror installer, and the container application is a runtime state.

View container

View container

After starting docker, use the docker pscommand to view the currently running container:

View all containers

The above command is to view the container is currently running, if you need to see all containers, you can view the command:docker ps-a

When viewing the container involves several view parameter meanings are as follows:

  • CONTAINER ID: CONTAINER ID refers to the id of the container, it is a unique identifier, which is a 64-bit hexadecimal integer, in the case of confusion may not only use the first few id identifies a container.

  • IMAGE: IMAGE represent mirror used when creating container.

  • COMMAND: COMMAND command vessel represents the final run.

  • CREATED: creation time of the container.

  • STATUS: state of the container, a container may show a start time, a container closing time can be displayed. Specific display which depends on the current state of the container.

  • PORTS: Port container opening.

  • NAMES: the name of the vessel, if not set, there will be a default name.

View the newly created container

Use can view recently created a container, as follows:docker ps-l

 

View the newly created n containers

You can use the n containers to view the latest created as follows:docker ps-n=XXX

Create a container

It created a whole container in two different ways, you can create, re-start, you can also create even start with one step, whether it is that way, processes are similar, when created after executing a command, docker will first Find next to go to the local path if there is a corresponding image, if not, go docker search hub, if searched, then downloaded, and then use that image and start to create a container. The container file is to add a file system layer of the read-write image file on a read-only, so that the data can be changed without the mirror, only the changed record. The following description separately these two ways.

Create a container

Developers can use the first docker createcommand to create a container, this time created out of the container is at a standstill, not running, for example, to create a nginx container, create the following command:

  1. docker create nginx

Once created, you can check whether the container was created successfully:

Container created this time is not running, is stopped, the container name is randomly generated, developers can also specify a name when you create the container, as follows:

  1. docker create --name=nginx nginx

Results are as follows:

At this time, the name attribute is not randomly generated, but a user-specified name.

This is just a simple way to create a user, did not start.

+ Create a container to start

If developers need to both create and launch container, you can use the docker runcommand. docker runCommand and you can start both containers different modes: interactive and background-type container vessels, by definition, is a background type container vessels running in the background, silently in the background and perform calculations on the line, and developers do not need to interact and interactive type container receiving input developer is required for processing the feedback given. For developers, created in most cases are the background type container, but in many cases, even the background type container may inevitably need to interact, the following were run.

Background-type container

Background-type container with nginx for example, generally nginx can run in the background:

  1. docker run --name nginx1 -d -p 8080:80 nginx

--name And meaning as above, represents the name of the container created, -d represents vessel runs in the background, -p 8080 represents mapping of the container port 80 to the host, the creation process follows:

First will still go to the local inspection, the local no corresponding container, it will go to search, find the download and run, and generates a container id on Docker Hub. After the successful operation, input in the browser will be able to see the default page of the Nginx, as follows:http://localhost:8080

This is the basic way to create a background type container.

Interactive container

Interactive type container can also create, for example, create a ubuntu container, the developer may need to enter commands related to operations performed ubuntu above, the container create interactive follows:

  1. docker run --name ubuntu -it ubuntu /bin/bash

参数含义都和上文一致,除了 -it,-it 参数,i 表示开发容器的标准输入(STDIN),t 则表示告诉 docker,为容器创建一个命令行终端。执行结果如下:

该命令执行完后,会打开一个输入终端,读者就可以在这个终端里愉快的操作 ubuntu 了。

想要退出该终端,只需要输入 exit 命令即可。

容器启动

启动

如果开发者使用了 docker run 命令创建了容器,则创建完成后容器就已经启动了,如果使用了 docker create 命令创建了容器,则需要再执行 docker start 命令来启动容器,使用 docker start 命令结合容器 id 或者容器 name 可以启动一个容器,如下:

docker start 启动的是一个已经存在的容器,要使用该命令启动一个容器,必须要先知道容器的 id 或者 name ,开发者可以通过这两个属性启动一个容器(案例中,nginx 是通过 name 启动,而 ubuntu 则是通过 id 启动)。一般来说,第一次可以使用 docker run 启动一个容器,以后直接使用 docker start 即可。

重启

容器在运行过程中,会不可避免的出问题,出了问题时,需要能够自动重启,在容器启动时使用 --restart 参数可以实现这一需求。根据 docker 官网的解释,docker 的重启策略可以分为 4 种,如下:

四种的含义分别如下:

  1. no表示不自动重启容器,默认即此。

  2. on:failure:[max-retries]表示在退出状态为非0时才会重启(非正常退出),有一个可选择参数:最大重启次数,可以设置最大重启次数,重启次数达到上限后就会放弃重启。

  3. always表示始终重启容器,当docker守护进程启动时,也会无论容器当时的状态为何,都会尝试重启容器。

  4. ubless-stopped表示始终重启容器,但是当docker守护进程启动时,如果容器已经停止运行,则不会去重启它。

容器停止

通过 docker stop 命令可以终止一个容器,如下:

可以通过 name 或者 id 终止一个容器。

容器删除

单个删除

容器停止后还依然存在,如果需要,还可以通过 docker start 命令再次重启一个容器,如果不需要一个容器,则可以通过 docker rm 命令删除一个容器。删除容器时,只能删除已经停止运行的容器,不能删除正在运行的容器。如下:

可以通过 name 或者 id 删除一个容器。如果非要删除一个正在运行的容器,可以通过 -f 参数实现,如下:

批量删除

容器也可以批量删除,命令如下:

  1. docker rm $(docker ps -a -q)

docker ps-a-q 会列出所有容器的 id ,供 rm 命令删除。

如下命令也支持删除已退出的孤立的容器:

  1. docker container prune

总结

本文主要向大家介绍了 Docker 容器的基本操作,更多高级操作我们将在下篇文章中介绍。

参考资料:

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

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

关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料。

 

Guess you like

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