Docker images and container commands

Docker is an open source engine that makes it easy to create a lightweight, portable, self-sufficient container for any application. Containers that developers compile and test on laptops can be deployed in production environments in batches, including VMs (virtual machines), bare metal, OpenStack clusters and other basic application platforms. 
Docker is usually used in the following scenarios:
automated packaging and publishing of web applications;
automated testing and continuous integration and publishing;
deployment and adjustment of databases or other background applications in a service-based environment;

Build your own PaaS environment by compiling or extending an existing OpenShift or Cloud Foundry platform from scratch.

1. Mirror related commands

1. Get the image

[plain]  view plain copy
  1. sudo docker pull ubuntu:12.04  

2. List local mirrors

[plain]  view plain copy
  1. sudo docker images  

In the listed information, you can see

which warehouse the information comes from, such as
the mark of the ubuntu image, such as 14.04,
its ID number (unique)
creation time , the
size
of the image, and the ID of the image which uniquely identifies the image. Note that ubuntu: 14.04 and ubuntu:trusty have the same mirror ID, which means they are actually the same mirror.
TAG information is used to tag different images from the same repository. For example, there are multiple mirrors in the ubuntu repository, and the release version is distinguished by TAG information, such as 10.04, 12.04, 12.10, 13.04, 14.04, etc. For example the following command specifies to use the image ubuntu:14.04 to start a container.

3. Create a mirror

method one:

[plain]  view plain copy
  1. docker commit  

方法二:

[plain]  view plain copy
  1. dockerFile  

4、移除本地镜像

[plain]  view plain copy
  1. 可以使用 docker rmi 命令。注意 docker rm 命令是移除容器。  

*注意:在删除镜像之前要先用 docker rm 删掉依赖于这个镜像的所有容器。

5、存出和载入镜像
存出镜像
如果要导出镜像到本地文件,可以使用 docker save 命令。


6、载入镜像

可以使用 docker load 从导出的本地文件中再导入到本地镜像库,例如

[plain]  view plain copy
  1. sudo docker load --input ubuntu_14.04.tar  

[plain]  view plain copy
  1. $ sudo docker load < ubuntu_14.04.tar  

这将导入镜像以及其相关的元数据信息(包括标签等)。


二、容器相关命令

1、启动容器

启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(stopped)的容器重新启动。

因为 Docker 的容器实在太轻量级了,很多时候用户都是随时删除和新创建容器。所需要的命令主要为 docker run。
(1)新建并启动

下面的命令则启动一个 bash 终端,允许用户进行交互。

[plain]  view plain copy
  1. sudo docker run -t -i training/sinatra /bin/bash  

其中,-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上, -i 则让容器的标准输入保持打开。也可以合并起来写

[plain]  view plain copy
  1. sudo docker run -ti ubuntu:14.04 /bin/bash  

可见,容器中仅运行了指定的 bash 应用。这种特点使得 Docker 对资源的利用率极高,是货真价实的轻量级虚拟化。不加-t -i的话,执行完就退出容器例如,下面的命令输出一个 “Hello World”,之后终止容器。

[plain]  view plain copy
  1. sudo docker run ubuntu:14.04 /bin/echo 'Hello world'  

这跟在本地直接执行 /bin/echo 'hello world' 几乎感觉不出任何区别。
[plain]  view plain copy
  1.   

在交互模式下,用户可以通过所创建的终端来输入命令,例如

如果,只想让容器在后台运行呢?那就看下面的吧!

(2)守护态运行更多的时候,需要让 Docker 容器在后台以守护态(Daemonized)形式运行。此时,可以通过添加 -d 参数来实现。例如下面的命令会在后台运行容器。

容器启动后会返回一个唯一的 id,也可以通过 docker ps 命令来查看容器信息。

然后使用

[plain]  view plain copy
  1. docker attach 容器name  
就可以进入容器交互界面
容器name可以通过以下获得

[plain]  view plain copy
  1. docker ps -a   
如进入上面的:


然后进入:

当利用 docker run 来创建容器时,Docker 在后台运行的标准操作包括:
检查本地是否存在指定的镜像,不存在就从公有仓库下载
利用镜像创建并启动一个容器
分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
从地址池配置一个 ip 地址给容器
执行用户指定的应用程序
执行完毕后容器被终止

(3)启动终止的容器

可以利用 docker start + 容器ID,命令,直接将一个已经终止的容器启动运行。

先找到要启动容器的id

[plain]  view plain copy
  1. docker ps -a  


Exited表示的就是终止的。然后使用

[plain]  view plain copy
  1. docker start b3f9d3239bed   

上面我以后台运行的方式启动了两个新的容器

这是以后台运行的方式来执行的,那怎样才能再进入容器呢?可以用docker attact +容器name

先通过docker ps -a 取得正在运行的容器名字,然后

[plain]  view plain copy
  1. docker attach goofy_mclean  

如下:


容器的核心为所执行的应用程序,所需要的资源都是应用程序运行所必需的。除此之外,并没有其它的资源。可以在伪终端中利用 ps 或 top 来查看进程信息。

(4)退出container但是保持运行

默认情况下,如果使用ctrl-d退出container,那么container也会stop,按ctrl-p ctrl-q可以退出到宿主机,而保持container仍然在运行.然后要进入再使用docker attach

2、停止容器

输入exit或ctrl+d

3、获取容器信息

要获取容器的输出信息,可以通过 docker logs 命令。

[plain]  view plain copy
  1. docker logs 容器name  


4、在容器中安装新的程序

下一步我们要做的事情是在容器里面安装一个简单的程序(ping)。我们之前下载的tutorial镜像是基于ubuntu的,所以你可以使用ubuntu的apt-get命令来安装ping程序:apt-get install -y ping。
备注:apt-get 命令执行完毕之后,容器就会停止,但对容器的改动不会丢失。


5. Save the changes to the container
When you make changes to a container (by running a command in the container), you can save the changes to the container, so that you can run the container from the latest state after saving next time. . The process of saving state in docker is called committing, and it saves the difference between the old and new state, resulting in a new version. Or when finished, we use exit to exit, and now that our container has been changed by us, use the docker commit command to commit the updated copy.

Get the modified container ID first

Saving the container is actually saving it as a new image

Among them, -m specifies the submitted description information, which is the same as the version control tool we use; -a can specify the updated user information; followed by the ID of the container used to create the image; finally, specify the repository name and tag information of the target image. After the creation is successful, the ID information of the image will be returned.

Use docker images to see the newly created image.


After that, the new image can be used to start the container

6. Delete a container

You can use docker rm to delete a container in a terminated state. E.g


If you want to delete a running container, you can add the -f parameter. Docker will send the SIGKILL signal to the container.

7. Check running containers

Use the docker ps command to view a list of all running containers, and use the docker inspect command to view more detailed information about a container. Find the id of a running container and use the docker inspect command to view container information. The front part of the image id can be used, the full id is not required.



Reprinted in: https://my.oschina.net/zhanghaiyang/blog/595086

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324133411&siteId=291194637