docker中的容器:
- lxc --> libcontainer --> runC
Open Container-initiative
-
由Linux基金会主导于2015年6月创立
-
旨在围绕容器格式和运行时制定一个开放的工业化标准
-
包含两种规格
-
运行时规范(Runtime spec)
-
图像规格(image-spec)
OCF
打开容器格式
runC是一个CLI工具,用于根据OCI规范生成和运行容器
-
容器作为runC的子进程启动,并且可以嵌入到各种其他系统中,而无需运行守护进程
-
runC是建立在libcontainer之上的,libcontainer是一种支持数百万Docker引擎安装的容器技术
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
-
Registry为网上的开源镜像资源网站,相当于仓库。里面的图标相当于网站里每个不同的镜像资源,dockerhost就是我们装有docker的主机,client为客户端,里面为各种命令。其中docker bulid为创建镜像。这些命令相当于交给装有docker主机的服务。containers为镜像启动后的容器。客户端和主机可以在一台主机里,但仓库客户端主机一般不会在一台主机里。如果这台主机要运行镜像的话,会使用docker run先在本地lmages查找有没此镜像,如果有就直接拿来用,如果本地没有则使用docker pull从网上下载到本地来使用,然后生成容器。
docker镜像与镜像仓库
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
docker对象
使用docker时,您正在创建和使用图像、容器、网络、卷、插件和其他对象。
-
-
图像是一个只读模板,其中包含创建docker容器的说明。
-
通常,一个图像基于另一个图像,并进行了一些额外的定制。
-
您可以创建自己的图像,也可以只使用其他人创建并在注册表中发布的图像。
-
-
容器
-
conntainer是映像的可运行实例。
-
您可以使用docker API或CLI创建、运行、停止、移动或删除容器。
-
安装及使用docker
docker安装以及使用加速器
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
//下载docker服务 [root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo [root@localhost yum.repos.d]# yum clean all [root@localhost yum.repos.d]# yum -y install docker-ce [root@localhost ~]# systemctl enable --now docker //设置加速器 [root@localhost ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"] } [root@localhost ~]# systemctl restart docker [root@localhost ~]# 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(加-d后台运行) |
Attach to a runninng container | |
docker ps | List containers(加-a看所有进程) |
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(加-f强制删除,加i删除没有容器的镜像) |
docker exec | Run a command in a running container(加-it指定交互模式进入容器,比如加/bin/bash或/bin/sh) |
docker info | Display system-wide information |
docker inspect | Return low-level information on Docker objects(可以看进程的IP) |
//使用search命令查找网上的镜像httpd [root@localhost ~]# docker search httpd NAME DESCRIPTION STARS OFFICIAL AUTOMATED httpd The Apache HTTP Server Project 3371 [OK] centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 36 centos/httpd 33 [OK] polinux/httpd-php Apache with PHP in Docker (Supervisor, CentO… 4 [OK] salim1983hoop/httpd24 Dockerfile running apache config 2 [OK] //使用pull命令下载网上的镜像,不加版本默认为最新,需要指定版本要使用:加指定版本 [root@localhost ~]# docker pull httpd //使用images查看本地的镜像 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest 683a7aad17d3 6 weeks ago 138MB //使用create在一个镜像中创建容器 [root@localhost ~]# docker create 683a7aad17d3 //使用ps -a查看刚创建的容器 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d90dadac9b09 683a7aad17d3 "httpd-foreground" 2 minutes ago Created nice_nash //使用start使容器启动 [root@localhost ~]# docker start d90dadac9b09 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d90dadac9b09 683a7aad17d3 "httpd-foreground" 3 minutes ago Up 18 seconds 80/tcp nice_nash //使用restart重启容器 [root@localhost ~]# docker restart d90dadac9b09 //使用stop停止容器 [root@localhost ~]# docker stop d90dadac9b09 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES //使用kill强制终止容器(不推荐使用) [root@localhost ~]# docker start d90dadac9b09 [root@localhost ~]# docker kill d90dadac9b09 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d90dadac9b09 683a7aad17d3 "httpd-foreground" 11 minutes ago Exited (137) About a minute ago nice_nash //使用rm删除容器,使用rmi删除镜像(删除时容器应处于停止状态,若容器还在运行则删除失败,可以使用rm -f 强制删除。删除镜像时应没有容器在运行,如果镜像里有容器在运行,仅只会把镜像的名称删除,镜像依旧存在且镜像里的容器还会继续运行) [root@localhost ~]# docker rm d90dadac9b09 d90dadac9b09 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# docker rmi httpd [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE //使用run可以自动进行创建和运行容器,加上-d使其在后台运行。 [root@localhost ~]# docker pull httpd [root@localhost ~]# docker run -d httpd a86f97f67d40b4714bd3efbd3ff165fc238838dfed6b9c0b01b65cf32ca38147 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a86f97f67d40 httpd "httpd-foreground" 6 seconds ago Up 5 seconds 80/tcp suspicious_rosalind //使用logs查看容器的日志 [root@localhost ~]# docker logs a86f97f67d40 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Thu Feb 25 09:41:43.745756 2021] [mpm_event:notice] [pid 1:tid 140526198039680] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations [Thu Feb 25 09:41:43.745876 2021] [core:notice] [pid 1:tid 140526198039680] AH00094: Command line: 'httpd -D FOREGROUND' //使用info查看整个docker的信息,比如正在运行的容器 [root@localhost ~]# docker info ..... Server: Containers: 1 Running: 1 Paused: 0 Stopped: 0 Images: 1 ..... //使用inspect查看容器的各种信息,比如IP [root@localhost ~]# docker inspect a86f97f67d40 .... "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", .... //使用attach进入到容器的内部,但不能操作且退出时容器也会停止,不推荐使用。 [root@localhost ~]# docker attach a86f97f67d40 [root@192 ~]# curl 172.17.0.2 <html><body><h1>It works!</h1></body></html> 172.17.0.1 - - [25/Feb/2021:09:56:46 +0000] "GET / HTTP/1.1" 200 45 ^C[Thu Feb 25 09:58:08.163404 2021] [mpm_event:notice] [pid 1:tid 140526198039680] AH00491: caught SIGTERM, shutting down [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a86f97f67d40 httpd "httpd-foreground" 16 minutes ago Exited (0) 7 seconds ago suspicious_rosalind //使用exec -it指定交互模式进入容器,比如/bin/bash或/bin/sh,由此可以实现操作且退出时容器不会停止。 [root@localhost ~]# docker start a86f97f67d40 [root@localhost ~]# docker exec -it a86f97f67d40 /bin/bash root@a86f97f67d40:/usr/local/apache2# ls bin build cgi-bin conf error htdocs icons include logs modules root@a86f97f67d40:/usr/local/apache2# ls conf extra httpd.conf magic mime.types original root@a86f97f67d40:/usr/local/apache2# exit exit [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a86f97f67d40 httpd "httpd-foreground" 20 minutes ago Up About a minute 80/tcp suspicious_rosalind