文章目录
docker基础用法
1.什么是docker
docker中的容器:
- lxc --> libcontainer --> runC
2.OCI&OCF
OCI
Open Container-initiative
-
由Linux基金会主导于2015年6月创立
-
旨在围绕容器格式和运行时制定一个开放的工业化标准
-
包含两个规范
运行时规范(运行时规范)
镜像规格(镜像规格)
OCF
Open Container Format
runC是一个CLI工具,用于根据OCI规范生成和运行容器
-
容器作为runC的子进程启动,可以嵌入到各种其他系统中,而无需运行守护进程
-
runC构建在libcontainer上,这项容器技术为数百万Docker引擎安装提供了动力
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
3.docker架构
4.docker镜像与镜像仓库
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
5.docker对象
使用docker时,您正在创建和使用图像、容器、网络、卷、插件和其他对象。
镜像
镜像是只读模板,其中包含创建docker容器的说明。
通常,一个镜像基于另一个镜像,并进行一些额外的自定义。
您可以创建自己的镜像,也可以只使用其他人创建并在注册表中发布的镜像。
容器
conntainer是映像的可运行实例。
您可以使用docker API或CLI创建、运行、停止、移动或删除容器。
您可以将容器连接到一个或多个网络,将存储连接到容器,甚至可以基于其当前状态创建新映像。
6.安装及使用dockers
docker安装
[root@centos8-1 ~]# cd /etc/yum.repos.d/
[root@centos8-1 yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:-- 0 0 0 0 0 0 0 0 --:--100 1919 100 1919 0 0 7133 0 --:--:-- --:--:-- --:--:-- 7107
[root@centos8-1 yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@centos8-1 yum.repos.d]# yum makecache
[root@centos8-1 ~]# yum -y install docker-ce
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@centos8-1 ~]# cd /etc/docker/
[root@centos8-1 docker]# ls
key.json
[root@centos8-1 docker]# vi daemon.json
[root@centos8-1 docker]# cat daemon.json
{
"registry-mirrors": ["https://of7ut2lu.mirror.aliyuncs.com"]
}
[root@centos8-1 docker]# systemctl daemon-reload
[root@centos8-1 docker]# systemctl restart docker
[root@centos8-1 docker]#
7.docker常用操作
命令 | 功能 |
---|---|
docker search | Search the Docker Hub for images/在Docker Hub中搜索镜像 |
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对象的信息 |
docker search
[root@centos8-1 ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3784 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 40
centos/httpd 34 [OK]
arm32v7/httpd The Apache HTTP Server Project 10
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
hypoport/httpd-cgi httpd-cgi 2 [OK]
dariko/httpd-rproxy-ldap Apache httpd reverse proxy with LDAP authent… 1 [OK]
publici/httpd httpd:latest 1 [OK]
jonathanheilmann/httpd-alpine-rewrite httpd:alpine with enabled mod_rewrite 1 [OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 1
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
centos/httpd-24-centos8 1
manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1 [OK]
dockerpinata/httpd 1
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
e2eteam/httpd 0
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
ysli/httpd Httpd for DeepWeb 0 [OK]
trollin/httpd 0
itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK]
manasip/httpd 0
ppc64le/httpd The Apache HTTP Server Project 0
[root@centos8-1 ~]#
docker pull
[root@centos8-1 ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
eff15d958d66: Already exists
ba1caf8ba86c: Pull complete
ab86dc02235d: Pull complete
0d58b11d2867: Pull complete
e88da7cb925c: Pull complete
Digest: sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
[root@centos8-1 ~]#
docker images
[root@centos8-1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest ad17c88403e2 12 days ago 143MB
[root@centos8-1 ~]#
docker create
[root@centos8-1 ~]# docker create --name httpd httpd
[A4ba4c399ff21a68857fa6b9dce32bd86db3793176a097f112b3cc9e573354a43
[root@centos8-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ba4c399ff21 httpd "httpd-foreground" 11 seconds ago Created httpd
[root@centos8-1 ~]#
docker start
[root@centos8-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ba4c399ff21 httpd "httpd-foreground" 11 seconds ago Created httpd
[root@centos8-1 ~]# docker start 4ba4c399ff21
4ba4c399ff21
[root@centos8-1 ~]#
docker run
[root@centos8-1 ~]# docker run -d --name httpd01 httpd
f3574c83b422473f20ee24f6063189cf77d350769c3ec89f37eaec5b707a0be4
[root@centos8-1 ~]#
docker ps
[root@centos8-1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3574c83b422 httpd "httpd-foreground" 9 seconds ago Up 7 seconds 80/tcp httpd01
4ba4c399ff21 httpd "httpd-foreground" About a minute ago Up About a minute 80/tcp httpd
[root@centos8-1 ~]#
docker logs
[root@centos8-1 ~]# docker logs -f httpd01
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. 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.3. Set the 'ServerName' directive globally to suppress this message
[Wed Dec 01 10:57:28.492165 2021] [mpm_event:notice] [pid 1:tid 140621847756096] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 10:57:28.492290 2021] [core:notice] [pid 1:tid 140621847756096] AH00094: Command line: 'httpd -D FOREGROUND'
^C
[root@centos8-1 ~]#
docker rmi(删除镜像)
[root@centos8-1 ~]# docker rmi nginx
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
[root@centos8-1 ~]#
docker exec
[root@centos8-1 ~]# docker exec -it httpd01 /bin/bash
root@f3574c83b422:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@f3574c83b422:/usr/local/apache2# cd conf/
root@f3574c83b422:/usr/local/apache2/conf# ls
extra httpd.conf magic mime.types original
root@f3574c83b422:/usr/local/apache2/conf# cd ..
root@f3574c83b422:/usr/local/apache2# exit
exit
[root@centos8-1 ~]#
docker restart
[root@centos8-1 ~]# docker restart httpd
httpd
[root@centos8-1 ~]#
docker stop
[root@centos8-1 ~]# docker stop httpd
httpd
[root@centos8-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3574c83b422 httpd "httpd-foreground" 4 minutes ago Up 2 minutes 80/tcp httpd01
4ba4c399ff21 httpd "httpd-foreground" 6 minutes ago Exited (0) 10 seconds ago httpd
[root@centos8-1 ~]#
docker kill
[root@centos8-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3574c83b422 httpd "httpd-foreground" 5 minutes ago Up 2 minutes 80/tcp httpd01
4ba4c399ff21 httpd "httpd-foreground" 7 minutes ago Up 3 seconds 80/tcp httpd
[root@centos8-1 ~]# docker kill f3574c83b422
f3574c83b422
[root@centos8-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3574c83b422 httpd "httpd-foreground" 5 minutes ago Exited (137) 1 second ago httpd01
4ba4c399ff21 httpd "httpd-foreground" 7 minutes ago Up 14 seconds 80/tcp httpd
[root@centos8-1 ~]#
docker rm
[root@centos8-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3574c83b422 httpd "httpd-foreground" 5 minutes ago Exited (137) 30 seconds ago httpd01
4ba4c399ff21 httpd "httpd-foreground" 7 minutes ago Up 43 seconds 80/tcp httpd
[root@centos8-1 ~]# docker rm httpd01
httpd01
[root@centos8-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ba4c399ff21 httpd "httpd-foreground" 7 minutes ago Up 51 seconds 80/tcp httpd
[root@centos8-1 ~]#
docker info
[root@centos8-1 ~]# docker info
^[[A^[[AClient:
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:
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 1
Server Version: 20.10.11
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
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
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
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-257.el8.x86_64
Operating System: CentOS Stream 8
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.622GiB
Name: centos8-1
ID: HZL3:6QCV:HMOO:DE5R:L4IF:3OMC:A3IA:7JHE:OSM4:BREP:EQJR:WS5D
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://of7ut2lu.mirror.aliyuncs.com/
Live Restore Enabled: false
[root@centos8-1 ~]#
docker inspect
[root@centos8-1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ba4c399ff21 httpd "httpd-foreground" 9 minutes ago Up 2 minutes 80/tcp httpd
[root@centos8-1 ~]# docker inspect 4ba4c399ff21
[
{
"Id": "4ba4c399ff21a68857fa6b9dce32bd86db3793176a097f112b3cc9e573354a43",
"Created": "2021-12-01T10:55:40.2392817Z",
"Path": "httpd-foreground",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 15782,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-12-01T11:02:38.101805302Z",
"FinishedAt": "2021-12-01T11:01:42.602128357Z"
},
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "38b9e8218854247f08a196639387c64d4e39337796a5baa688debee06173eb9a",
"EndpointID": "da976b4895606df3e8a1ebac12fb90c4d8bd61c5d9f03a3a018e7e9378a6cf1e",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
[root@centos8-1 ~]#