什么是docker
docker中的容器:
lxc --> libcontainer --> runC
Docker的思想来自于集装箱,集装箱解决了什么问题?在一艘大船上,可以把货物规整的摆放起来。并且各种各样的货物被集装箱标准化了,集装箱和集装箱之间不会互相影响。那么我就不需要专门运送水果的船和专门运送化学品的船了。只要这些货物在集装箱里封装的好好的,那我就可以用一艘大船把他们都运走。
docker就是类似的理念。现在都流行云计算了,云计算就好比大货轮。docker就是集装箱。
1.不同的应用程序可能会有不同的应用环境,比如.net开发的网站和php开发的网站依赖的软件就不一样,如果把他们依赖的软件都安装在一个服务器上就要调试很久,而且很麻烦,还会造成一些冲突。比如IIS和Apache访问端口冲突。这个时候你就要隔离.net开发的网站和php开发的网站。常规来讲,我们可以在服务器上创建不同的虚拟机在不同的虚拟机上放置不同的应用,但是虚拟机开销比较高。docker可以实现虚拟机隔离应用环境的功能,并且开销比虚拟机小,小就意味着省钱了。
2.你开发软件的时候用的是Ubuntu,但是运维管理的都是centos,运维在把你的软件从开发环境转移到生产环境的时候就会遇到一些Ubuntu转centos的问题,比如:有个特殊版本的数据库,只有Ubuntu支持,centos不支持,在转移的过程当中运维就得想办法解决这样的问题。这时候要是有docker你就可以把开发环境直接封装转移给运维,运维直接部署你给他的docker就可以了。而且部署速度快。
3.在服务器负载方面,如果你单独开一个虚拟机,那么虚拟机会占用空闲内存的,docker部署的话,这些内存就会利用起来。总之docker就是集装箱原理。
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是一个CLI工具,用于根据OCI规范生成和运行容器
- 容器作为runC的子进程启动,并且可以嵌入到各种其他系统中,而无需运行守护进程
- runC是建立在libcontainer之上的,libcontainer是一种支持数百万Docker引擎安装的容器技术
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
镜像与镜像仓库的关系
镜像仓库:是存放镜像的场所,dockerhub是最大的开源仓库,可以通过docker search 查找所需镜像并通过docker pull拉取
镜像:docker 真正的操作对象
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
下图中圆饼为镜像文件,可以有很多的版本
长方形为镜像仓库
docker对象
使用docker时,您正在创建和使用镜像、容器、网络、卷、插件和其他对象。
镜像
- 镜像是一个只读模板,其中包含创建docker容器的说明。
- 通常,一个镜像基于另一个镜像,并进行了一些额外的定制。
- 您可以创建自己的镜像,也可以只使用其他人创建并在注册表中发布的镜像。
容器
- 容器是镜像的可运行实例。
- 您可以使用docker API或CLI创建、运行、停止、移动或删除容器。
- 您可以将容器连接到一个或多个网络,将存储连接到容器,甚至可以基于其当前状态创建新镜像。
docker工作原理
当您请求 Docker 运行容器时,Docker 会在您的计算机上设置一个资源隔离的环境。然后 Docker 会将打包的应用程序和关联的文件复制到 Namespace 内的文件系统中,此时环境的配置就完成了。之后 Docker 会执行您指定的命令运行应用程序。
Docker 在运行时分为 Docker 引擎(服务端守护进程) 和 客户端工具,我们日常使用各种 docker 命令,其实就是在使用 客户端工具 与 Docker 引擎 进行交互。
docker安装
配置yum仓库
[root@localhost ~]# vim /etc/yum.repos.d/docker-ce.repo
[Docker-ce]
name=Docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/ 这里用的是网易的
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
安装和启动
yum -y install docker-ce
systemctl start docker
docker常用操作
命令 | 功能 |
---|---|
docker search | 在Docker Hub中搜索镜像 |
docker pull | 从注册表中提取镜像或存储库 |
docker images | 镜像列表 |
docker create | 创建一个新的容器 |
docker start | 启动一个或多个停止的容器 |
docker run | 在新容器中运行命令 |
docker attach | 连接到正在运行的容器 |
docker ps | 列表容器 |
docker logs | 获取容器的日志 |
docker restart | 重启一个容器 |
docker stop | 停止一个或多个正在运行的容器 |
docker kill | 杀死一个或多个正在运行的容器 |
docker rm | 移除一个或多个容器 |
docker exec | 在正在运行的容器中运行命令 |
docker info | 显示整个系统的信息 |
docker inspect | 返回Docker对象的低级信息 |
docker search
[root@localhost ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3598 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 40
centos/httpd 34 [OK]
arm64v8/httpd The Apache HTTP Server Project 7
polinux/httpd-php Apache with PHP in Docker (Supervisor, CentO… 5 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
hypoport/httpd-cgi httpd-cgi 2 [OK]
centos/httpd-24-centos8 1
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 1
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1 [OK]
dockerpinata/httpd 1
dariko/httpd-rproxy-ldap Apache httpd reverse proxy with LDAP authent… 1 [OK]
manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
jonathanheilmann/httpd-alpine-rewrite httpd:alpine with enabled mod_rewrite 1 [OK]
publici/httpd httpd:latest 1 [OK]
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
interlutions/httpd httpd docker image with debian-based config … 0 [OK]
appertly/httpd Customized Apache HTTPD that uses a PHP-FPM … 0 [OK]
amd64/httpd The Apache HTTP Server Project 0
manasip/httpd 0
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
trollin/httpd 0
itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK]
ysli/httpd Httpd for DeepWeb 0 [OK]
e2eteam/httpd 0
docker pull
[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
33847f680f63: Pull complete
d74938eee980: Pull complete
963cfdce5a0c: Pull complete
8d5a3cca778c: Pull complete
e06a573b193b: Pull complete
Digest: sha256:61e49dd08a51d6fc421ed257bd8eb461cf2d48269d9ab2b4ff5d4c69826c3c9c
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 73b8cfec1155 5 days ago 138MB
docker create
[root@localhost ~]# docker create httpd
c2ec2e58f0f112b784d4bf530058954e38f95a0d560f517c2bf60a2d863ecb28
docker ps
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2ec2e58f0f1 httpd "httpd-foreground" About a minute ago Created keen_ishizaka
docker run
[root@localhost ~]# docker run httpd
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
[Wed Jul 28 03:26:17.202954 2021] [mpm_event:notice] [pid 1:tid 140607461721216] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 03:26:17.210118 2021] [core:notice] [pid 1:tid 140607461721216] AH00094: Command line: 'httpd -D FOREGROUND'
#这样运行是在前台运行,复制终端查看
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa69d845c204 httpd "httpd-foreground" About a minute ago Up About a minute 80/tcp cranky_bardeen
#ctrl+c中止以后使用CONTAINER ID运行就可以在后台运行了
[root@localhost ~]# docker start aa69d845c204
aa69d845c204
docker logs
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa69d845c204 httpd "httpd-foreground" 16 minutes ago Up 9 minutes 80/tcp cranky_bardeen
[root@localhost ~]# docker start aa69d845c204
aa69d845c204
[root@localhost ~]# docker logs aa69d845c204
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
[Wed Jul 28 03:26:17.202954 2021] [mpm_event:notice] [pid 1:tid 140607461721216] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 03:26:17.210118 2021] [core:notice] [pid 1:tid 140607461721216] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Jul 28 03:31:02.144648 2021] [mpm_event:notice] [pid 1:tid 140607461721216] AH00491: caught SIGTERM, shutting down
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
[Wed Jul 28 03:32:52.981919 2021] [mpm_event:notice] [pid 1:tid 139695784125568] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 03:32:52.982410 2021] [core:notice] [pid 1:tid 139695784125568] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Jul 28 03:42:18.882420 2021] [mpm_event:notice] [pid 1:tid 139695784125568] AH00494: SIGHUP received. Attempting to restart
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
[Wed Jul 28 03:42:18.892000 2021] [mpm_event:notice] [pid 1:tid 139695784125568] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 03:42:18.892035 2021] [core:notice] [pid 1:tid 139695784125568] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Jul 28 03:42:18.908738 2021] [mpm_event:notice] [pid 1:tid 139695784125568] AH00494: SIGHUP received. Attempting to restart
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
[Wed Jul 28 03:42:18.912319 2021] [mpm_event:notice] [pid 1:tid 139695784125568] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 03:42:18.912334 2021] [core:notice] [pid 1:tid 139695784125568] AH00094: Command line: 'httpd -D FOREGROUND'
docker rm
[root@localhost ~]# docker stop aa69d845c204
aa69d845c204
[root@localhost ~]# docker rm aa69d845c204
aa69d845c204
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2ec2e58f0f1 httpd "httpd-foreground" 20 minutes ago Created keen_ishizaka
docker exec
[root@localhost ~]# docker start c2ec2e58f0f1
c2ec2e58f0f1
[root@localhost ~]# docker exec -it c2ec2e58f0f1 /bin/bash
root@c2ec2e58f0f1:/usr/local/apache2# pwd
/usr/local/apache2