-a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter=[] Filter output based on conditions provided --format Pretty-print images using a Go template --help Print usage --no-trunc Don't truncate output -q, --quiet Only show numeric IDs
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) --blkio-weight Block IO (relative weight), between 10 and 1000 --blkio-weight-device=[] Block IO weight (relative device weight) --cpu-shares 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 --cpu-period Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota Limit CPU CFS (Completely Fair Scheduler) quota --cpuset-cpus CPUs in which to allow execution (0-3, 0,1) --cpuset-mems MEMs in which to allow execution (0-3, 0,1) -d, --detach Run container in background and print container ID --detach-keys Override the key sequence for detaching a container --device=[] Add a host device to the container --device-read-bps=[] Limit read rate (bytes per second) from a device --device-read-iops=[] Limit read rate (IO per second) from a device --device-write-bps=[] Limit write rate (bytes per second) to a device --device-write-iops=[] Limit write rate (IO per second) to a device --disable-content-trust=true Skip image verification --dns=[] Set custom DNS servers --dns-opt=[] Set DNS options --dns-search=[] Set custom DNS search domains -e, --env=[] Set environment variables --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 --group-add=[] Add additional groups to join -h, --hostname Container host name --help Print usage -i, --interactive Keep STDIN open even if not attached --ip Container IPv4 address (e.g. 172.30.100.104) --ip6 Container IPv6 address (e.g. 2001:db8::33) --ipc IPC namespace to use --isolation Container isolation level --kernel-memory Kernel memory limit -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 --log-opt=[] Log driver options -m, --memory Memory limit --mac-address Container MAC address (e.g. 92:d0:c6:0a:29:33) --memory-reservation Memory soft limit --memory-swap Swap limit equal to memory plus swap: '-1' to enable unlimited swap --memory-swappiness=-1 Tune container memory swappiness (0 to 100) --name Assign a name to the container --net=default Connect a container to a network --net-alias=[] Add network-scoped alias for the container --oom-kill-disable Disable OOM Killer --oom-score-adj Tune host's OOM preferences (-1000 to 1000) -P, --publish-all Publish all exposed ports to random ports -p, --publish=[] Publish a container's port(s) to the host --pid PID namespace to use --privileged Give extended privileges to this container --read-only Mount the container's root filesystem as read only --restart=no Restart policy to apply when a container exits --rm Automatically remove the container when it exits --security-opt=[] Security Options --shm-size Size of /dev/shm, default value is 64MB --sig-proxy=true Proxy received signals to the process --stop-signal=SIGTERM Signal to stop a container, SIGTERM by default -t, --tty Allocate a pseudo-TTY --tmpfs=[] Mount a tmpfs directory -u, --user Username or UID (format: <name|uid>[:<group|gid>]) --ulimit=[] Ulimit options --uts UTS namespace to use -v, --volume=[] Bind mount a volume --volume-driver Optional volume driver for the container --volumes-from=[] Mount volumes from the specified container(s) -w, --workdir Working directory inside the container
IMAGE可以为镜像名或者镜像ID。 示例:
1 2
sudo docker run -t -i ouruser/sinatra:v2 /bin/bash sudo docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done" #守护态运行
在执行 docker run 的时候如果添加 –rm 标记,则容器在终止后会立刻删除。注意,–rm 和 -d 参数不能同时使用。
docker logs
说明:获取容器输出的信息,适用于守护态运行docker容器。
1 2 3 4 5 6 7 8 9 10 11
[root@localhost ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
-f, --follow Follow log output --help Print usage --since Show logs since timestamp -t, --timestamps Show timestamps --tail=all Number of lines to show from the end of the logs
--help Print usage -t, --time=10 Seconds to wait for stop before killing the container
docker attach
说明:在使用 -d 参数时,容器启动后会进入后台。 某些时候需要进入容器进行操作就可以用它。
1 2 3 4 5 6 7 8 9 10
[root@localhost ~]# docker attach --help
Usage: docker attach [OPTIONS] CONTAINER
Attach to a running container
--detach-keys Override the key sequence for detaching a container --help Print usage --no-stdin Do not attach STDIN --sig-proxy=true Proxy all received signals to the process
-a, --all Show all containers (default shows just running) -f, --filter=[] Filter output based on conditions provided --format Pretty-print containers using a Go template --help Print usage -l, --latest Show the latest created container (includes all states) -n=-1 Show n last created containers (includes all states) --no-trunc Don't truncate output -q, --quiet Only display numeric IDs -s, --size Display total file sizes
示例:
1
[root@localhost ~]# docker ps -a
docker export
说明:导出本地某个容器为快照文件
1 2 3 4 5 6 7 8
[root@localhost ~]# docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
--help Print usage -o, --output Write to a file, instead of STDOUT
-f, --force Force the removal of a running container (uses SIGKILL) --help Print usage -l, --link Remove the specified link -v, --volumes Remove the volumes associated with the container
示例:
1
[root@localhost ~]# docker rm $(docker ps -a -q) #清理所有处于终止状态的容器
docker rmi
说明:移除本地镜像,移出前需要先移除到镜像对应的容器。
1 2 3 4 5 6 7 8 9
[root@localhost ~]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
-f, --force Force removal of the image --help Print usage --no-prune Do not delete untagged parents
Return low-level information on a container or image
-f, --format Format the output using the given go template --help Print usage -s, --size Display total file sizes if the type is container --type Return JSON for specified type, (e.g image or container)
docker port
说明:来查看当前映射的端口配置,也可以查看到绑定的地址。
1 2 3 4 5 6 7
[root@localhost ~]# docker port --help
Usage: docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]]
List port mappings or a specific mapping for the CONTAINER
Transaction check error: file /usr/lib/systemd/system/blk-availability.service from install of device-mapper-7:1.02.107-5.el7_2.1.x86_64 conflicts with file from package lvm2-7:2.02.105-14.el7.x86_64 file /usr/sbin/blkdeactivate from install of device-mapper-7:1.02.107-5.el7_2.1.x86_64 conflicts with file from package lvm2-7:2.02.105-14.el7.x86_64 file /usr/share/man/man8/blkdeactivate.8.gz from install of device-mapper-7:1.02.107-5.el7_2.1.x86_64 conflicts with file from package lvm2-7:2.02.105-14.el7.x86_64
[root@localhost ~]# docker pull ubuntu Using default tag: latest Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon. Is the docker daemon running on this host?). Using system default: https://index.docker.io/v1/ Cannot connect to the Docker daemon. Is the docker daemon running on this host? 解决方法: 重启docker服务
1 2
[root@localhost ~]# service docker restart Redirecting to /bin/systemctl restart docker.service
Docker被墙
[root@localhost ~]# docker pull centos Using default tag: latest Pulling repository docker.io/library/centos Error while pulling image: Get https://index.docker.io/v1/repositories/library/centos/images: dial tcp: lookup index.docker.io on 10.28.10.166:53: no such host
在 iOS 7 之前,当程序置于后台之后开发者们对他们程序所能做的事情非常有限。除了 VOIP 和基于地理位置特性以外,唯一能做的地方就是使用后台任务(background tasks)让代码可以执行几分钟。如果你想下载比较大的视频文件以便离线浏览,亦或者备份用户的照片到你的服务器上,你都仅能完成一部分工作。
iOS 7 添加了两个新的 API 以便你的程序可以在后台更新界面以及内容。首先是后台获取(Background Fetch),它允许你定期地从网络获取新的内容。第二个 API 就是远程通知(Remote Notifications),这是一个当事件发生时可以让推送通知主动提醒应用的新特性,这两者都为你的应用界面保持最新提供了极大的帮助。在新的后台传输服务 (Background Transfer Service) 中执行定期的任务,也允许你在进程之外可以执行网络传输(下载和上传)工作。
对用户来说,多任务处理有一点显而易见的改变就是新的应用切换程序 (the new app switcher),它用来呈现应用到后台时的界面快照。这些快照的存在是有一定理由的–现在你可以在后台完成工作后更新程序快照,以用来呈现新的内容。社交网络、新闻或者天气等应用现在都可以直接呈现最新的内容而不需要用户重新打开应用。我们稍后会介绍如何更新屏幕快照。
现在,我们需要实现 NSURLSessionDownloadDelegate 的委托方法,当下载完成时,调用回调函数。如果你需要处理认证或会话生命周期的其他事件,你可能还需要实现 NSURLSessionDelegate 或 NSURLSessionTaskDelegate 的方法。你应该阅读 Apple 的 Life Cycle of a URL Session with Custom Delegates 文档,它讲解了所有类型的会话任务的完整生命周期。
- (void)addCompletionHandler:(CompletionHandlerType)handler forSession:(NSString *)identifier { if ([self.completionHandlerDictionary objectForKey:identifier]) { NSLog(@"Error: Got multiple handlers for a single session identifier. This should not happen.\n"); }
这是一个可变参数的函数,第一个参数代表消息接收者,第二个代表 SEL 类型,后面的参数就是消息传递中使用的参数。
那么什么是 SEL 呢?SEL 就是代码在编译时,编译器根据方法签名来生成的一个唯一 ID。此 ID 可以用以区分不同的方法,只要 ID 一致,即看成同一个方法,ID 不同,即为不同的方法。
当进行消息传递,对象在响应消息时,是通过 SEL 在 methodlist 中查找函数指针 IMP,找到后直接通过指针调用函数,这其实就是前文介绍的 __动态绑定__。若是找到对应函数就跳转到实现代码,若找不到,就沿着继承链往上查找,直到找到相应的实现代码为止。若最终还是没找到实现代码,说明当前对象无法响应此消息,接下来就会执行 消息转发 操作,以试图找到一个能响应此消息的对象。
1 2 3 4
// 获取 SEL SEL sel = @selector(methodName); // 获取 IMP IMP imp = methodForSelector(sel);
消息转发
消息转发并不神奇,我们其实早已接触过,只是不知道而已
1 2
-[__NSCFNumber lowercaseString]:unrecognized selector sent to instance 0x87 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:'-[NSCFNumber lowercaseString]:unrecognized selector sent to instance 0x87'
先来看看NSObject Class Reference里对这两个方法说明: +(void)initialize
The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.
+(void)load The load message is sent to classes and categories that are both dynamically loaded and statically linked, but only if the newly loaded class or category implements a method that can respond. The order of initialization is as follows:
All initializers in any framework you link to. All +load methods in your image. All C++ static initializers and C/C++ attribute(constructor) functions in your image. All initializers in frameworks that link to you. In addition:
A class’s +load method is called after all of its superclasses’ +load methods. A category +load method is called after the class’s own +load method. In a custom implementation of load you can therefore safely message other unrelated classes from the same image, but any load methods implemented by those classes may not have run yet.