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

DOCKER学习之(二) 容器部分常用命令

2016-07-19 17:36 771 查看

一、运行容器

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container
参数
-a, --attach=[]            Attach to STDIN, STDOUT or STDERR
--add-host=[]              Add a custom host-to-IP mapping (host:ip)
-c, --cpu-shares=0         CPU shares (relative weight)
--cap-add=[]               Add Linux capabilities
--cap-drop=[]              Drop Linux capabilities
--cgroup-parent=           Optional parent cgroup for the container
--cidfile=                 Write the container ID to the file
--cpuset-cpus=             CPUs in which to allow execution (0-3, 0,1)
-d, --detach=false         Run container in background and print container ID
--device=[]                Add a host device to the container
--dns=[]                   Set custom DNS servers
--dns-search=[]            Set custom DNS search domains
-e, --env=[]               Set environment variables//见Dockfile中的ENV
--entrypoint=              Overwrite the default ENTRYPOINT of the image
--env-file=[]              Read in a file of environment variables
--expose=[]                Expose a port or a range of ports
-h, --hostname=            Container host name
--help=false               Print usage
-i, --interactive=false    Keep STDIN open even if not attached
--ipc=                     IPC namespace to use
-l, --label=[]             Set meta data on a container
--label-file=[]            Read in a line delimited file of labels
--link=[]                  Add link to another container
--log-driver=              Logging driver for container
--lxc-conf=[]              Add custom lxc options
-m, --memory=              Memory limit
--mac-address=             Container MAC address (e.g. 92:d0:c6:0a:29:33)
--memory-swap=             Total memory (memory + swap), '-1' to disable swap
--name=                    Assign a name to the container
--net=bridge               Set the Network mode for the container
-P, --publish-all=false    Publish all exposed ports to random ports//公开Dockfile 中的保留端口到随机端口
-p, --publish=[]           Publish a container's port(s) to the host //宿主机端口:容器端口(80则宿主机端口随机,(1)在ps中可以查看,(2) docker port name/id 端口)
--pid=                     PID namespace to use
--privileged=false         Give extended privileges to this container
--read-only=false          Mount the container's root filesystem as read only
--restart=no               Restart policy to apply when a container exits//检查容器退出代码,自动重启容器。例如: --restart=on-failure:5 //退出代码为非0时,才会自动重启。--restart=always 总是重启
--rm=false                 Automatically remove the container when it exits//退出时自动删除
--security-opt=[]          Security Options
--sig-proxy=true           Proxy received signals to the process
-t, --tty=false            Allocate a pseudo-TTY
-u, --user=                Username or UID (format: <name|uid>[:<group|gid>])//见Dockfile中的USER
--ulimit=[]                Ulimit options
-v, --volume=[]            Bind mount a volume//加载卷列表 见Dockfile中的VOLUME
--volumes-from=[]          Mount volumes from the specified container(s)
-w, --workdir=             Working directory inside the container//指定容器工作目录 见Dockfile中的WORKDIR


例如1 以交互方式运行一个容器

sudo docker run -it ubuntu /bin/bash


例如2给容器指定一个命名

sudo docker run --name one-name -it ubuntu /bin/bash


例如3创建守护式容器

sudo docker run --name one-name -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done"


二、 容器日志

Usage: docker logs [OPTIONS] CONTAINER

Fetch the logs of a container
参数
-f, --follow=false        Follow log output
--help=false              Print usage
-t, --timestamps=false    Show timestamps
--tail=all                Number of lines to show from the end of the logs


例如:

sudo docker logs name/id

–tail 0 -f 最新日志

–tail 10 -f 最后10行记录

三、 查看容器内的进程

Usage: docker top [OPTIONS] CONTAINER [ps OPTIONS]


例如:

sudo docker top name/id


四、 在容器内部运行进程

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
参数:
-d, --detach=false         Detached mode: run command in the background
--help=false               Print usage
-i, --interactive=false    Keep STDIN open even if not attached
-t, --tty=false            Allocate a pseudo-TTY


例如:交互式命令

sudo docker exec -it name/id /bin/bash


创建文件

sudo docker exec -d  name/id touch /etc/new_file


五、停止守护容器

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop a running container by sending SIGTERM and then SIGKILL after a
grace period

--help=false       Print usage
-t, --time=10      Seconds to wait for stop before killing it


六、重新启动已经停止的容器(restart)

Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers
参数
-a, --attach=false         Attach STDOUT/STDERR and forward signals
--help=false               Print usage
-i, --interactive=false    Attach container's STDIN


例如:

sudo docker start name/id


sudo docker restart name/id


七、删除容器

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers
参数
-f, --force=false      Force the removal of a running container (uses SIGKILL)
--help=false           Print usage
-l, --link=false       Remove the specified link
-v, --volumes=false    Remove the volumes associated with the container


八、深入容器信息

包括配置信息,名称,命令、网路配置以及很多有用数据

Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]

Return low-level information on a container or image
参数
-f, --format=      Format the output using the given go template
--help=false       Print usage


九、 容器列表

Usage: docker ps [OPTIONS]

List containers
参数:
-a, --all=false       Show all containers (default shows just running)
--before=             Show only container created before Id or Name
-f, --filter=[]       Filter output based on conditions provided
--help=false          Print usage
-l, --latest=false    Show the latest created container, include non-running
-n=-1                 Show n last created containers, include non-running
--no-trunc=false      Don't truncate output
-q, --quiet=false     Only display numeric IDs
-s, --size=false      Display total file sizes
--since=              Show created since Id or Name, include non-running
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  docker 容器 命令