$ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from hello-world a8219747be10: Pull complete 91c95931e552: Already exists hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security. Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d Status: Downloaded newer image for hello-world:latest Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (Assuming it was not already locally available.) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash For more examples and ideas, visit: http://docs.docker.com/userguide/
Docker 的镜像是只读的,可以理解为是一个软件。Docker 镜像通过镜像 ID 进行识别。镜像 ID 是一个 64 字符的十六进制的字符串。通常我们不会使用镜像 ID 来引用镜像,而是使用镜像名来引用。
查看本地镜像:
1 2 3 4 5
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker-whale latest 03643993f0ca 7 minutes ago 255.6 MB hello-world latest 690ed74de00f 4 months ago 960 B docker/whalesay latest 6b362a9f73eb 8 months ago 247 MB
这里的 Image ID 只有前 12 个字符,想要显示全部,需要添加选项:–no-trunc。
1 2 3 4 5
$ docker images --no-trunc REPOSITORY TAG IMAGE ID CREATED SIZE docker-whale latest sha256:03643993f0cacd07968d99265815543a0f2d296e4fc434253b2b4ac2e2c84aae About an hour ago 255.6 MB hello-world latest sha256:690ed74de00f99a7d00a98a5ad855ac4febd66412be132438f9b8dbd300a937d 4 months ago 960 B docker/whalesay latest sha256:6b362a9f73eb8c33b48c95f4fcce1b6637fc25646728cf7fb0679b2da273c3f4 8 months ago 247 MB
容器就是运行镜像的地方。当执行 docker run 的时候,就会启动一个容器,而 docker run hello-world 便会把 hello-world 这个程序加载到这个容器里面运行。每个容器相互独立,我们可以使用同一个镜像启动多个容器(多个虚拟环境),我们对其中一个容器所做的变更只会局限于那个容器本身,对容器的变更是写入到容器的文件系统的,而不是写入到 Docker 镜像中的。
容器之间可以通过暴露端口进行通信,这个在以后学习中再说。
Docker 使用 64 字符的十六进制的字符串来定义容器 ID,它是容器的唯一标识符。容器之间的交互是依靠容器 ID 识别的,由于容器 ID 的字符太长,我们通常只需键入容器 ID 的前 4 个字符即可。当然,我们还可以使用容器名,但显然用 4 字符的容器 ID 更为简便。
查看当前运行的容器可以使用命令:
1 2 3 4
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a45a2d3b95e7 sample-spring-boot-app "/bin/sh -c 'java -DC" 3 seconds ago Up 2 seconds 0.0.0.0:8080->8080/tcp silly_morse 0b2494881e6c sample-spring-boot-app "/bin/sh -c 'java -DC" 3 minutes ago Up 3 minutes 0.0.0.0:32771->8080/tcp compassionate_austin