Fundamentally, a container is nothing but a running process, with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers. One of the most important aspects of container isolation is that each container interacts with its own private filesystem; this filesystem is provided by a Docker image. An image includes everything needed to run an application - the code or binary, runtimes, dependencies, and any other filesystem objects required.
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
By contrast, a virtual machine (VM) runs a full-blown "guest" operating system with virtual access to host resources through a hypervisor. In general, VMs incur a lot of overhead beyond what is being consumed by your application logic.
$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE $ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202 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. (amd64) 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
Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/ $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 7 months ago 13.3kB
Load an image or repository from a tar archive (even if compressed with gzip, bzip2, or xz) from a file or STDIN. It restores both images and tags. 从tar归档文件(即使使用gzip、bzip2或xz压缩后的),从一个文件或者STDIN中,加载镜像或仓库。它可以恢复镜像和标签。
$ sudo docker run -it ubuntu bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 3ff22d22a855: Pull complete e7cb79d19722: Pull complete 323d0d660b6a: Pull complete b7f616834fd0: Pull complete Digest: sha256:5d1d5407f353843ecf8b16524bc5565aa332e9e6a1297c73a92d3e754b8a636d Status: Downloaded newer image for ubuntu:latest root@966d5fa519a5:/# # 此时进入了基于ubuntu:latest创建的容器中。
查看镜像信息
退出容器后,docker ps 查看本机的镜像信息列表和容器信息列表。
root@d9e6e9b9323a:/# exit exit $ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1e4467b07108 9 days ago 73.9M $ sudo docker ps # 查看正在运行的容器信息 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES $ sudo docker ps -a # 查看所有容器信息 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 966d5fa519a5 ubuntu "bash" 38 seconds ago Exited (0) 9 seconds ago relaxed_kirch
$ sudo docker run -it --gpus all --name testv3 -v /home/lart/Downloads/:/data nvidia/cuda:10.0-base bash root@efd722f0321f:/# exit exit $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES efd722f0321f nvidia/cuda:10.0-base "bash" 19 seconds ago Exited (0) 3 seconds ago testv3
可以看到,我这里存在一个已经退出的容器. 我们想要进入退出的容器首先需要启动已经退出的容器:
$ sudo docker start testv3 testv3 $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
efd722f0321f nvidia/cuda:10.0-base "bash" 31 seconds ago Up 1 second testv3
$ sudo docker stop testv3 testv3 $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES efd722f0321f nvidia/cuda:10.0-base "bash" 2 minutes ago Exited (0) 4 seconds ago testv3 $ sudo docker restart testv3 # restart 不仅可以重启关掉的容器,也可以重启运行中的容器 testv3 $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES efd722f0321f nvidia/cuda:10.0-base "bash" 2 minutes ago Up 1 second testv3
后台分离模式运行的容器
对于创建分离模式的容器我们可以使用 docker run -d 。另外前面提到的 start 或者 restart 启动的容器会自动以分离模式运行。
The docker cp utility copies the contents of SRC_PATH to the DEST_PATH. You can copy from the container's file system to the local machine or the reverse, from the local filesystem to the container.
If - is specified for either the SRC_PATH or DEST_PATH, you can also stream a tar archive from STDIN or to STDOUT.
The CONTAINER can be a running or stopped container.
The SRC_PATH or DEST_PATH can be a file or directory.
The docker build command builds Docker images from a Dockerfile and a "context". A build's context is the set of files located in the specified PATH or URL.
The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.
It can be useful to commit a container's file changes or settings into a new image. This allows you to debug a container by running an interactive shell, or to export a working dataset to another server.
Generally, it is better to use Dockerfiles to manage your images in a documented and maintainable way. Read more about valid image names and tags.
The commit operation will not include any data contained in volumes mounted inside the container.
By default, the container being committed and its processes will be paused while the image is committed. This reduces the likelihood of encountering data corruption during the process of creating the commit. If this behavior is undesired, set the --pause option to false.
一般使用指定容器的ID就可以进行提交了。
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3f279d17e0a ubuntu:12.04 /bin/bash 7 days ago Up 25 hours desperate_dubinsky 197387f1b436 ubuntu:12.04 /bin/bash 7 days ago Up 25 hours focused_hamilton $ docker commit c3f279d17e0a svendowideit/testimage:version3 f5283438590d $ docker images REPOSITORY TAG ID CREATED SIZE svendowideit/testimage version3 f5283438590d 16 seconds ago 335.7 MB
Produces a tarred repository to the standard output stream. Contains all parent layers, and all tags + versions, or specified repo:tag , for each argument provided.
最全面的删除:docker system prune 这一指令会询问你是否执行_Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes. _注意,这个命令会把你暂时关闭的容器,以及暂时没有用到的 Docker 镜像都删掉。
添加参数 --all 或者 -a 来_Remove all unused images not just dangling ones_.
添加参数 --volumes 来_Remove all volumes not used by at least one container._
对于volumes的删除:docker volume prune Remove all unused local volumes. Unused local volumes are those which are not referenced by any containers.
对于容器的删除:docker container prune Remove all stopped containers.
对于镜像的删除:docker image pruneRemove all dangling images. If -a is specified, will also remove all images not referenced by any container.