Working with Containers

回顾之前学习过的一些命令

docker run -i...交互式运行
docker run -d...background运行`daemon`守护进程
docker ps...Lists containers.(容器列表)
docker logs...Shows us the standard output of a container.(显示容器的标准输出)
docker stop...Stops running containers.

docker命令格式:

[sudo] docker [command] [flags] [arguments]...

Seeing what the Docker client can do

docker client能干什么

[sudo] docker…显示docker后能执行的命令

Seeing Docker command usage

docker 命令使用

docker command --help...查看特定命令的使用方式

Running a Web Application in Docker

docker中运行一个web应用

 sudo docker run -d -P training/webapp python app.py

-P参数表示将容器内部要用到的网络端口映射到主机

docker ps -l -a

-l显示容器的详细信息,-a表示显示所有的容器信息(包含以前运行的)

docker run -d -P training/webapp python app.py
docker run -d -p 5000:5000 training/webapp python app.py

-P表示将image镜像的任何端口映射到我们自己的主机

-p自己指定image的网络端口和主机的端口

实战:

命令行输入

docker run -d -P training/webapp python app.py
docker ps -l -a
out:
[email protected]:~$ docker ps -a -l
CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                     NAMES
1179c34ac0e0        training/webapp:latest   "python app.py"     2 minutes ago       Up 2 minutes        0.0.0.0:32769->5000/tcp   elegant_curie

这里重要的是port下面的值:0.0.0.0:32769->5000/tcp,我的理解是:这条命令把0.0.0.0:32769这个自己主机的端口映射到容器里的5000端口,所以当你在自己的浏览器输入0.0.0.0:32769是它会映射到images的5000端口从而访问app.py的网页

A Network Port Shortcut

网站端口Shortcut

docker port [container id|container name]

这条命令表示输出容器的端口和映射端口,

5000/tcp -> 0.0.0.0:32769
adolph@geek:~$ docker port 1179c 5000
0.0.0.0:32769

Viewing the Web Application’s Logs

查看web应用的logs

adolph@geek:~$ docker logs -f elegant_curie
 * Running on http://0.0.0.0:5000/
172.17.42.1 - - [12/May/2015 17:49:25] "GET / HTTP/1.1" 200 -
172.17.42.1 - - [12/May/2015 17:49:26] "GET /favicon.ico HTTP/1.1" 404 -

-f功能类似于tail -f而且我们可以看到标准输出的信息

Looking at our Web Application Container’s processes

查看web应用容器的进程

使用docker top命令

adolph@geek:~$ docker top elegant_curie
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                32280               2150                0                   01:48               ?                   00:00:00            python app.py

Inspecting our Web Application Container

检查web应用容器

docker inspect comtainer name

Json格式输出信息

Stopping&Start&Remove our Web Application Container

停止和启动我们的web容器

adolph@geek:~$ docker ps -l
CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                     NAMES
1179c34ac0e0        training/webapp:latest   "python app.py"     25 minutes ago      Up 25 minutes       0.0.0.0:32769->5000/tcp   elegant_curie
adolph@geek:~$ docker stop elegant_curie
elegant_curie
adolph@geek:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
adolph@geek:~$ docker start elegant_curie
elegant_curie
adolph@geek:~$ docker ps -l
CONTAINER ID        IMAGE                    COMMAND             CREATED             STATUS              PORTS                     NAMES
1179c34ac0e0        training/webapp:latest   "python app.py"     26 minutes ago      Up 7 seconds        0.0.0.0:32770->5000/tcp   elegant_curie       

adolph@geek:~$ docker stop elegant_curie
elegant_curie
adolph@geek:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
adolph@geek:~$ docker rm elegant_curie
elegant_curie

命令总结

docker...显示docker的命令
docker command --help...显示某个命令的帮助
docker ps -a -l...显示所有的容器信息
docker run -d -P ...-P映射容器的5000端口到主机的任意端口[32768-61000]
docker run -d -p ...-p自己指定映射端口
docker port [comtainer id|container name]...查看容器的端口情况
docker logs -f [container name|id] ...输出容器标准输出
docker inspect [container name]...输出json格式的容器的详细信息
docker top [container name...查看容器的进程
docker stop|start|rm [container name]...停止|开始|删除容器
时间: 2024-08-06 15:21:13

Working with Containers的相关文章

Learning docker--Chapter2--Handling Docker Containers

Content: (1)    Clarifying the Docker terms (2)    Working with the Docker images and containers (3)    The Docker Hub Registry (4)    Searching the Docker images (5)    Working with an interactive container (6)    Tracking the changes inside the con

Understand the Qt containers(有对应表)

Container classes are one of the cornerstones of object-oriented programming, invaluable tools that free us from having to permanently think about memory management. Qt comes with its own set of container classes, closely modeled after those in the S

[转载]Vertica “ERROR: Too many ROS containers exist”

最近在用Vertica的时候碰到一个问题,Vertica在运行了一段时间后总是出现类似下面的错误 1: java.sql.SQLException: [Vertica][VJDBC](5065) 2: ERROR: Too many ROS containers exist for the following projections: 3: 4: <projection> (limit = 18078, ROS files = 12088, DV files = 5992, new files

[Docker] Prune Old Unused Docker Containers and Images

In this lesson, we will look at docker container prune to remove old docker containers. We can also use docker system prune to clean up any containers, images, volumes, and networks all in one command. This will remove all containers that are not cur

Linking Containers Together

Linking Containers Together In the Using Docker section we touched on connecting to a service running inside a Docker container via a network port. This is one of the ways that you can interact with services and applications running inside Docker con

[LINK]List of .NET Dependency Injection Containers (IOC)

http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx I'm trying to expand my mind around dependency injection in .NET (beyond the two frameworks I've personally used) and an starting to put together a list of .NET Dependency I

关联式容器(associative containers)

根据数据在容器中的排列特性,容器可分为序列式(sequence)和关联式(associative)两种. 标准的STL关联式容器分为set(集合)和map(映射表)两大类,以及两大类的衍生体multiset(多键集合)和multimap(多键映射表).这些容器的底层机制均是以RB-tree(红黑树)完成.RB-tree也是一个独立的容器,但并不开放给外界使用. 此外,SGI STL还提供一个不在规格标准之列的关联式容器:hash table(散列表),以及以此hash table为底层机制而完成

[D3] SVG Graphics Containers and Text Elements in D3 v4

SVG is a great output format for data visualizations because of its scalability, but it comes with some idiosyncrasies and unique challenges. In this lesson we’ll learn how to use graphics containers, the SVG equivalent of a div, as well as text elem

Vertica “ERROR: Too many ROS containers exist”

原文链接:Vertica "ERROR: Too many ROS containers exist" 近期在用Vertica的时候碰到一个问题,Vertica在执行了一段时间后总是出现相似以下的错误 java.sql.SQLException: [Vertica][VJDBC](5065) ERROR: Too many ROS containers exist for the following projections: <projection> (limit = 18

关于Docker中的Images与Containers

Docker engine提供了启动Images和containers核心的技术的支持.当你运行docker run hello-world 命令时,实际上可分为三个部分: 告诉你操作系统你正在使用的docker程序 一个子命令创建并且运行docker容器 告诉docker将载入到容器中的Image映像 一个映像是一个文件系统,是在运行时使用的参数,它没有状态和不会改变.容器用来运行映像的实例.当你运行下面命令的时候将会发生下面这些情况: 检查你是否有hello-world软件映像 从Docke