• create
  • run

    create

    • 用法
    1. Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
    2. Create a new container
    3. -a, --attach=[] Attach to STDIN, STDOUT or STDERR
    4. --add-host=[] Add a custom host-to-IP mapping (host:ip)
    5. --blkio-weight=0 Block IO (relative weight), between 10 and 1000
    6. -c, --cpu-shares=0 CPU shares (relative weight)
    7. --cap-add=[] Add Linux capabilities
    8. --cap-drop=[] Drop Linux capabilities
    9. --cgroup-parent= Optional parent cgroup for the container
    10. --cidfile= Write the container ID to the file
    11. --cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period
    12. --cpu-quota=0 Limit the CPU CFS quota
    13. --cpuset-cpus= CPUs in which to allow execution (0-3, 0,1)
    14. --cpuset-mems= MEMs in which to allow execution (0-3, 0,1)
    15. --device=[] Add a host device to the container
    16. --dns=[] Set custom DNS servers
    17. --dns-search=[] Set custom DNS search domains
    18. -e, --env=[] Set environment variables
    19. --entrypoint= Overwrite the default ENTRYPOINT of the image
    20. --env-file=[] Read in a file of environment variables
    21. --expose=[] Expose a port or a range of ports
    22. -h, --hostname= Container host name
    23. --help=false Print usage
    24. -i, --interactive=false Keep STDIN open even if not attached
    25. --init= Run container following specified init system container method (systemd)
    26. --ipc= IPC namespace to use
    27. -l, --label=[] Set meta data on a container
    28. --label-file=[] Read in a line delimited file of labels
    29. --link=[] Add link to another container
    30. --log-driver= Logging driver for container
    31. --log-opt=[] Log driver options
    32. --lxc-conf=[] Add custom lxc options
    33. -m, --memory= Memory limit
    34. --mac-address= Container MAC address (e.g. 92:d0:c6:0a:29:33)
    35. --memory-swap= Total memory (memory + swap), '-1' to disable swap
    36. --name= Assign a name to the container
    37. --net=bridge Set the Network mode for the container
    38. --oom-kill-disable=false Disable OOM Killer
    39. -P, --publish-all=false Publish all exposed ports to random ports
    40. -p, --publish=[] Publish a container's port(s) to the host
    41. --pid= PID namespace to use
    42. --privileged=false Give extended privileges to this container
    43. --read-only=false Mount the container's root filesystem as read only
    44. --restart=no Restart policy to apply when a container exits
    45. --security-opt=[] Security Options
    46. -t, --tty=false Allocate a pseudo-TTY
    47. -u, --user= Username or UID (format: <name|uid>[:<group|gid>])
    48. --ulimit=[] Ulimit options
    49. --uts= UTS namespace to use
    50. -v, --volume=[] Bind mount a volume
    51. --volumes-from=[] Mount volumes from the specified container(s)
    52. -w, --workdir= Working directory inside the container
    • 例子
    1. $ sudo docker create ubuntu /bin/echo 'Hello world'
    2. a637c1d67506951928be296f2db02fa3e2b6e974ef371b181d9c26d1c8995963
    3. $...

    若有如上输出则代表容器创建成功

    1. $ sudo docker ps -a
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. a637c1d67506 ubuntu:latest "/bin/echo 'Hello wo 4 minutes ago mad_hopper
    4. ...

    然后在启动它

    1. $ sudo docker start a637c1d67506
    2. a637c1d67506
    3. $...

    启动成功后会反回容器ID。

    1. $ docker ps -a
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. a637c1d67506 ubuntu:latest "/bin/echo 'Hello wo 10 minutes ago Exited (0) 2 minutes ago mad_hopper
    • 总结

    当我们去查看容器状态时,容器没有在运行,这时因为我们在创建容器的时候,让容器执行的命令是/bin/echo ‘Hello world’,当容器执行完命令的时候就终止结束了。

    run

    • 用法
    1. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    2. Run a command in a new container
    3. -a, --attach=[] Attach to STDIN, STDOUT or STDERR
    4. --add-host=[] Add a custom host-to-IP mapping (host:ip)
    5. --blkio-weight=0 Block IO (relative weight), between 10 and 1000
    6. -c, --cpu-shares=0 CPU shares (relative weight)
    7. --cap-add=[] Add Linux capabilities
    8. --cap-drop=[] Drop Linux capabilities
    9. --cgroup-parent= Optional parent cgroup for the container
    10. --cidfile= Write the container ID to the file
    11. --cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period
    12. --cpu-quota=0 Limit the CPU CFS quota
    13. --cpuset-cpus= CPUs in which to allow execution (0-3, 0,1)
    14. --cpuset-mems= MEMs in which to allow execution (0-3, 0,1)
    15. -d, --detach=false Run container in background and print container ID
    16. --device=[] Add a host device to the container
    17. --dns=[] Set custom DNS servers
    18. --dns-search=[] Set custom DNS search domains
    19. -e, --env=[] Set environment variables
    20. --entrypoint= Overwrite the default ENTRYPOINT of the image
    21. --env-file=[] Read in a file of environment variables
    22. --expose=[] Expose a port or a range of ports
    23. -h, --hostname= Container host name
    24. --help=false Print usage
    25. -i, --interactive=false Keep STDIN open even if not attached
    26. --init= Run container following specified init system container method (systemd)
    27. --ipc= IPC namespace to use
    28. -l, --label=[] Set meta data on a container
    29. --label-file=[] Read in a line delimited file of labels
    30. --link=[] Add link to another container
    31. --log-driver= Logging driver for container
    32. --log-opt=[] Log driver options
    33. --lxc-conf=[] Add custom lxc options
    34. -m, --memory= Memory limit
    35. --mac-address= Container MAC address (e.g. 92:d0:c6:0a:29:33)
    36. --memory-swap= Total memory (memory + swap), '-1' to disable swap
    37. --name= Assign a name to the container
    38. --net=bridge Set the Network mode for the container
    39. --oom-kill-disable=false Disable OOM Killer
    40. -P, --publish-all=false Publish all exposed ports to random ports
    41. -p, --publish=[] Publish a container's port(s) to the host
    42. --pid= PID namespace to use
    43. --privileged=false Give extended privileges to this container
    44. --read-only=false Mount the container's root filesystem as read only
    45. --restart=no Restart policy to apply when a container exits
    46. --rm=false Automatically remove the container when it exits
    47. --security-opt=[] Security Options
    48. --sig-proxy=true Proxy received signals to the process
    49. -t, --tty=false Allocate a pseudo-TTY
    50. -u, --user= Username or UID (format: <name|uid>[:<group|gid>])
    51. --ulimit=[] Ulimit options
    52. --uts= UTS namespace to use
    53. -v, --volume=[] Bind mount a volume
    54. --volumes-from=[] Mount volumes from the specified container(s)
    55. -w, --workdir= Working directory inside the container
    • 例子

    用法与create类似,只是在创建容器后不需要进行start操作就可以运行。

    1. $ sudo docker run ubuntu /bin/echo 'Hello world'
    2. Hello world
    3. $...

    与上面一样,在运行完Hello world 之后也会退出容器。

    Daemonized(守护态)

    往往我们需要容器在后台一致执行,这时我们就需要在创建镜像的时候让容器以守护台方式(-d 参数)运行。

    1. $ sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
    2. 61f37c1940c8ec9f08b107e99655b8a5181ded340415e3c15cf413069d556b73
    3. $...

    这时,我们查看一下容器状态:

    1. $ sudo docker ps -a
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. 61f37c1940c8 ubuntu:latest "/bin/sh -c 'while t 4 seconds ago Up 3 seconds prickly_galileo
    4. ...

    查看容器输出的信息

    1. $ sudo docker logs 61f37c1940c8
    2. hello world
    3. hello world
    4. hello world
    5. hello world
    6. ...
    • 总结

      让容器以后台方式运行,并不是加一个 -d 参数就可以,命令行COMMAND所执行的动作必须为持续运行的状态。