docker基础用法


什么是docker

docker中的容器:

  • lxc --> libcontainer --> runC

OCI&OCF

OCI

Open Container-initiative

  • 由Linux基金会主导于2015年6月创立
  • 旨在围绕容器格式和运行时制定一个开放的工业化标准
  • contains two specifications
    • the Runtime Specification(runtime-spec)
    • the Image Specification(image-spec)

OCF

Open Container Format

runC is a CLI tool for spawning and running containers according to the OCI specification

  • Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon
  • runC is built on libcontainer, the same container technology powering millions of Docker Engine installations

docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com

docker架构

docker镜像与镜像仓库

为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。

镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。

docker对象

When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.

  • IMAGES
    • An image is a read-only template with instructions for creating a docker container.
    • Often, an image is based on another image, with some additional customization.
    • You might create your own images or you might only use those created by others and published in a registry.
  • CONTAINERS
    • A conntainer is a runnable instance of an image.
    • You can create, run, stop, move, or delete a container using the docker API or CLI.
    • You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

安装及使用docker

docker安装

cd /etc/yum.repos.d/
curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo

yum -y install docker-ce

docker加速

docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
systemctl start docker

cat > /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
EOF

systemctl restart docker

docker version
docker info

docker常用操作

命令 功能
docker search Search the Docker Hub for images
docker pull Pull an image or a repository from a registry
docker images List images
docker create Create a new conntainer
docker start Start one or more stopped containers
docker run Run a command in a new container
docker attach Attach to a runninng container
docker ps List containers
docker logs Fetch the logs of a container
docker restart Restart a container
docker stop Stop one or more running containers
docker kill Kill one or more running containers
docker rm Remove onne or more containers
docker exec Run a command in a running container
docker info Display system-wide information
docker inspect Return low-level information on Docker objects

docker event state