您的位置:首页 > 运维架构 > Docker

如何在docker容器中运行一个程序

2015-10-27 16:05 756 查看
使用docker容器输出hello world,第一次运行会从docker hub下载ubuntu:14.04,输出结果后即终止

docker run ubuntu:14.04
/bin/echo 'Hello world'

交互式运行docker容器

[root@gage ~]#docker run -t -i ubuntu:14.04 /bin/bash

root@89471dce598b:/# ls

bin   dev  home  lib64  mnt  proc  run   srv  tmp  var

boot  etc  lib   media  opt  root  sbin  sys  usr

root@89471dce598b:/# pwd

/

root@89471dce598b:/# exit

exit

后台运行一个容器让其输出hello world

[root@gage ~]# docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"

4bc1b2f2257e253e45b672811653f409b53176ac6670d4ddaeabe028dcd35b8e

显示正在运行的docker容器

[root@gage ~]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES

4bc1b2f2257e        ubuntu:14.04        "/bin/sh -c 'while tr"   27 seconds ago      Up 26 seconds                           insane_bhabha

使用docker logs命令查看容器标准输出

[root@gage ~]# docker logs insane_bhabha

hello world

hello world

hello world

hello world

使用docker stop 停止容器

[root@gage ~]# docker stop insane_bhabha

insane_bhabha

[root@gage ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

官方链接:https://docs.docker.com/userguide/dockerizing/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: