什么是docker
docker是一个用Go语言实现的开源项目,可以让我们方便的创建和使用容器,docker将程序以及程序所有的依赖都打包到docker container,这样你的程序可以在任何环境都会有一致的表现,这里程序运行的依赖也就是容器就好比集装箱,容器所处的操作系统环境就好比货船或港口,程序的表现只和集装箱有关系(容器),和集装箱放在哪个货船或者哪个港口(操作系统)没有关系。
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.(您可以使用docker API或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安装
首先配置yum仓库
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
配置docker的yum源
[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]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
部署安装docker所需要的环境
[root@localhost yum.repos.d]# yum install -y yum-utils device-mapper-persistent-data lvm2
安装docker-ce以及docker组件
[root@localhost yum.repos.d]# yum -y install docker-ce --allowerasing
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
-
docker的加速有多种方式:
docker cn 中国科技大学加速器 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
#设置docker hub阿里云镜像加速
[root@localhost ~]# systemctl start docker
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://in3617d8.mirror.aliyuncs.com"]
}
EOF
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl daemon-reload
docker常用操作
命令 | 功能 |
---|---|
docker search | Search the Docker Hub for images 从Docker Hub镜像仓库上查找镜像 |
docker pull | Pull an image or a repository from a registry 从Docker Hub镜像仓库上拉取镜像 |
docker images | List images 列出本地的镜像 |
docker create | Create a new conntainer 创建一个新的容器(与run命令不同,使用create命令只能创建容器而并不启动) |
docker start | Start one or more stopped containers 启动容器 |
docker run | Run a command in a new container 根据镜像新建并启动容器 |
docker attach | Attach to a runninng container 进入到一个正在运行容器内(使用exit命令退出后,容器会停止) |
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 进入到一个正在运行容器内(使用exit命令退出后,容器会继续运行) |
docker info | Display system-wide information 显示当前系统信息、docker容器、镜像个数、设置等信息 |
docker inspect | Return low-level information on Docker objects 用于以JSON格式显示容器与镜像的详细信息 |
实例:
命令演示:
创建docker
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# docker create nginx
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
docker version
查看docker的版本信息
[root@localhost yum.repos.d]# docker version
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:36:58 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:35:20 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
docker info
查看docker的详细信息
[root@localhost ~]# docker info
Client: #docker客户端信息
Context: default
Debug Mode: false
Plugins: #插件
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
scan: Docker Scan (Docker Inc., v0.9.0)
Server: #docker服务器端信息
Containers: 0 #容器数量
Running: 0 #正在运行的数量
Paused: 0 #暂停的数量
Stopped: 0 #已停止的数量
Images: 0 #镜像数量
Server Version: 20.10.11 #docker服务器版本
Storage Driver: overlay2 #docker存储驱动程序
Backing Filesystem: xfs #文件系统
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file #日志驱动程序
Cgroup Driver: cgroupfs #Cgroup驱动程序
Cgroup Version: 1
Plugins: #插件信息
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive #Swarm状态
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc #runtimes信息
Default Runtime: runc #默认runtime
Init Binary: docker-init
containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc version: v1.0.2-0-g52b36a2
init version: de40ad0
Security Options: #安全选项
seccomp
Profile: default
Kernel Version: 4.18.0-193.el8.x86_64 #linux内核版本
Operating System: CentOS Stream 8 #linux操作系统
OSType: linux #操作系统类型
Architecture: x86_64
CPUs: 2 #宿主机CPU数量
Total Memory: 780.4MiB #宿主机内存
Name: localhost.localdomain #宿主机名称
ID: J5OK:3PYZ:VTBU:7KZE:QWCX:2ZSA:DLGQ:T7KC:QD4M:HA6M:QENN:HAYA
Docker Root Dir: /var/lib/docker #docker根目录
Debug Mode: false
Registry: https://index.docker.io/v1/ #镜像仓库
Labels:
Experimental: false
Insecure Registries: #非安全镜像仓库
127.0.0.0/8
Registry Mirrors: #镜像加速器
https://in3617d8.mirror.aliyuncs.com/
Live Restore Enabled: false
docker inspect
用于以JSON格式显示容器与镜像的详细信息
[root@localhost ~]# docker inspect nginx
[
{
"Id": "sha256:ea335eea17ab984571cd4a3bcf90a0413773b559c75ef4cda07d0ce952b00291",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e"
],
"Parent": "",
"Comment": "",
"Created": "2021-11-17T10:38:14.652464384Z",
"Container": "8a038ff17987cf87d4b7d7e2c80cb83bd2474d66e2dd0719e2b4f7de2ad6d853",
"ContainerConfig": {
"Hostname": "8a038ff17987",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.4",
"NJS_VERSION=0.7.0",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:2fb4060b053a39040c51ff7eadd30325de2c76650fc50aa42839070e16e8bdcb",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.4",
"NJS_VERSION=0.7.0",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:2fb4060b053a39040c51ff7eadd30325de2c76650fc50aa42839070e16e8bdcb",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 141490847,
"VirtualSize": 141490847,
"GraphDriver": {
"Data": {
"LowerDir":
·······
docker search
从Docker Hub镜像仓库上查找centos镜像
[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6904 [OK]
ansible/centos7-ansible Ansible on Centos7 135 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 131 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 121 [OK]
centos/systemd systemd enabled base container. 105 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
tutum/centos Simple CentOS docker image with SSH access 48
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 45
kinogmt/centos-ssh CentOS with SSH 29 [OK]
guyton/centos6 From official centos6 container with full up… 10 [OK]
nathonfowlie/centos-jre Latest CentOS image with the JRE pre-install… 8 [OK]
centos/tools Docker image that has systems administration… 7 [OK]
centos/redis Redis built for CentOS 6 [OK]
drecom/centos-ruby centos ruby 6 [OK]
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
amd64/centos The official build of CentOS. 2
miko2u/centos6 CentOS6 日本語環境 2 [OK]
dokken/centos-7 CentOS 7 image for kitchen-dokken 2
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
mcnaughton/centos-base centos base image 1 [OK]
smartentry/centos centos with smartentry 0 [OK]
jelastic/centosvps An image of the CentOS Elastic VPS maintaine… 0
starlabio/centos-native-build Our CentOS image for native builds 0 [OK]
docker pull
从Docker Hub镜像仓库上拉取centos镜像(centos后不接版本号的话默认最新版)
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
docker image
删除本地的镜像
[root@localhost ~]# docker image rm nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Deleted: sha256:ea335eea17ab984571cd4a3bcf90a0413773b559c75ef4cda07d0ce952b00291
Deleted: sha256:cc284e9b1cbed75793782165a07a0c2139d8ec0116d1d562c0e2e504ed586238
Deleted: sha256:6207e091bef7f1c94a109cb455ba163d53d7c2c641de65e71d3a0f33c0ebd8ae
Deleted: sha256:97a18ff8c6973f64d763f004cad932319a1428e0502c0ec3e671e78b2f14256b
Deleted: sha256:319130834f01416a2e8f9a4f2b2fa082c702ac21f16e0e2a206e23d53a0a3bae
Deleted: sha256:1bc375f72973dc110c9629a694bc7476bf878d244287c0214e6436afd6a9d1b0
Deleted: sha256:e1bbcf243d0e7387fbfe5116a485426f90d3ddeb0b1738dca4e3502b6743b325
docker images
列出本地的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 2 weeks ago 141MB
centos latest 5d0da3dc9764 2 months ago 231MB
docker create
创建容器可指定位置
[root@localhost ~]# docker create --name nginx1 nginx /bin/bash
b7d966253d09fa71c82db510a1d990d5bfb3572fc5aafbe6c02f7312afef21a2
docker start/ps/stop
启动/列出/停止容器
[root@localhost ~]# docker start nginx1 #启动容器nginx1
nginx1
[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
b7d966253d09 nginx "/docker-entrypoint.…" 2 minutes ago Exited (0) 17 seconds ago nginx1
642538b02d53 nginx "/docker-entrypoint.…" 14 minutes ago Created intelligent_curie
[root@localhost ~]# docker ps -q #列出正在运行的容器的id
[root@localhost ~]# docker ps -aq #列出所有容器的id
b7d966253d09
642538b02d53
[root@localhost ~]# docker stop nginx1 #停止正在运行的容器
nginx1
docker kill
杀死正在运行的容器
[root@localhost ~]# docker kill nginx1
nginx1
docker rm
删除容器
[root@localhost ~]# docker rm nginx1
nginx1
docker rmi
删除镜像
[root@localhost ~]# docker rmi centos:latest
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
docker run
-d为后台运行
-it指定shell
-p端口映射(本机端口:容器端口)
-v目录映射(本地目录:容器目录)
[root@localhost ~]# docker run -it --name centos1 -d centos /bin/bash #新建并启动容器centos1
ca47be39b8b40d4a2abb971abdd0d328929fd94c6b8d8b965e5feedec53e7bdc
[root@localhost ~]# docker run --name nginx -d nginx:latest #镜像新建并启动容器nginx(后台运行)
e0c911f7ba02d8f4ea835d5771c33149f327affd92c2f659021be8d25d7f25c2
[root@localhost ~]# docker run --name nginx1 -p 80:8080/tcp -d nginx #新建并启动容器nginx01(端口映射)
5ec1c4b8a1afa57dcd3e9bb66eceaa1b71c629f43f73946f4e1c59f973e71ef2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ec1c4b8a1af nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp, 0.0.0.0:80->8080/tcp, :::80->8080/tcp nginx1
e0c911f7ba02 nginx:latest "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp nginx
ca47be39b8b4 centos "/bin/bash" 3 minutes ago Up 3 minutes centos1
docker attach
进入到一个正在运行容器中
[root@localhost ~]# docker attach centos1
[root@ca47be39b8b4 /]#
docker exec
进入到一个在在运行容器
[root@localhost ~]# docker exec -it nginx1 /bin/bash
root@5ec1c4b8a1af:/#
不进入容器,在外面执行
[root@localhost ~]# docker exec nginx1 ls /root/
anaconda-ks.cfg