docker基础
docker发展史
Docker最初是dotCloud公司创始人Solomon Hykes在法国期间发起的一个公司内部项目,它是基于dotCloud公司多年云服务技术的一次革新,并于2013 年3月以Apache 2.0授权协议开源,主要项目代码在GitHub上进行维护。Docker项目后来还加入了Linux基金会,并成立推动开放容器联盟(OCI)。
Docker自开源后受到广泛的关注和讨论,至今其GitHub项目已经超过4万6千个星标和一万多个fork。甚至由于Docker项目的火爆,在 2013年底,dotCloud公司决定改名为Docker。Docker最初是在Ubuntu 12.04上开发实现的;Red Hat则从RHEL 6.5开始对Docker进行支持;Google也在其PaaS产品中广泛应用Docker。
Docker使用Google公司推出的Go语言进行开发实现,基于Linux内核的cgroup,namespace,以及AUFS类的Union FS等技术,对进程进行封装隔离,属于操作系统层面的虚拟化技术。由于隔离的进程独立于宿主和其它的隔离的进程,因此也称其为容器。最初实现是基于LXC,从0.7版本以后开始去除LXC,转而使用自行开发的libcontainer,从1.11开始,则进一步演进为使用runC和containerd。
docker基本用法
命令 | 功能 |
---|---|
docker search | 在 Docker 中搜索镜像 |
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
[root@192 ~]# 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
100 1919 100 1919 0 0 2281 0 --:--:-- --:--:-- --:--:-- 2279
[root@192 ~]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@192 ~]# ls
anaconda-ks.cfg docker-ce.repo
[root@192 ~]# mv docker-ce.repo /etc/yum.repos.d/
[root@192 ~]# dnf -y install docker-ce
Failed to set locale, defaulting to C.UTF-8
Docker CE Stable - x86_64 7.4 kB/s | 26 kB 00:03
appstream 2.4 MB/s | 4.4 kB 00:00
BaseOS 2.8 MB/s | 3.9 kB 00:00
Dependencies resolved.
·······
Complete!
//启动docker并设为开机自启
[root@192 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@192 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor pres>
Active: active (running) since Fri 2022-08-05 15:26:34 CST; 6s ago
Docs: https://docs.docker.com
Main PID: 40172 (dockerd)
Tasks: 7
Memory: 31.7M
CGroup: /system.slice/docker.service
└─40172 /usr/bin/dockerd -H fd:// --cont
······
//配置docker镜像加速器
[root@192 ~]# vim /etc/docker/daemon.json
[root@192 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://8r73devx.mirror.aliyuncs.com"]
}
[root@192 ~]# systemctl restart docker
[root@192 ~]# docker info
······
Registry Mirrors:
https://8r73devx.mirror.aliyuncs.com/
Live Restore Enabled: false
//查看docker版本信息
[root@192 ~]# docker version
Client: Docker Engine - Community
Version: 20.10.17
API version: 1.41
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 23:03:11 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.17
API version: 1.41 (minimum version 1.12)
Go version: go1.17.11
Git commit: a89b842
Built: Mon Jun 6 23:01:29 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.6
GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc:
Version: 1.1.2
GitCommit: v1.1.2-0-ga916309
docker-init:
Version: 0.19.0
GitCommit: de40ad0
//拉取centos镜像
[root@192 ~]# docker pull centos
Using default tag: latest
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
//查看镜像
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 10 months ago 231MB
//搜索httpd
[root@192 ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 4107 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 44
centos/httpd 35 [OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 2
hypoport/httpd-cgi httpd-cgi 2 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
dockerpinata/httpd 1
nnasaki/httpd-ssi SSI enabled Apache 2.4 on Alpine Linux 1
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1 [OK]
publici/httpd httpd:latest 1 [OK]
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]
centos/httpd-24-centos8 1
manasip/httpd 0
amd64/httpd The Apache HTTP Server Project 0
patrickha/httpd-err 0
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
httpdss/archerysec ArcherySec repository 0 [OK]
paketobuildpacks/httpd 0
httpdocker/kubia 0
sandeep1988/httpd-new httpd-new 0
e2eteam/httpd 0
19022021/httpd-connection_test This httpd image will test the connectivity … 0
sherazahmedvaival/httpd-php-fpm74 0
[root@192 ~]#
//使用busybox镜像创建一个容器
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 7 months ago 1.24MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@192 ~]# docker create busybox
5536de865dd06bc3e31de7ce9be3695414f3e5fe812f8837281c1527fd71daf1
//显示所有容器
[root@192 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5536de865dd0 busybox "sh" 28 seconds ago Created confident_sammet
[root@192 ~]#
//启动创建的容器
[root@192 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5536de865dd0 busybox "sh" 28 seconds ago Created confident_sammet
[root@192 ~]# docker start 5536de865dd0
5536de865dd0
[root@192 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5536de865dd0 busybox "sh" 4 minutes ago Exited (0) 7 seconds ago confident_sammet
//获取web容器日志
[root@localhost ~]# docker logs web
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
[Fri Aug 05 08:13:59.798953 2022] [mpm_event:notice] [pid 1:tid 140464886181184] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Fri Aug 05 08:13:59.799074 2022] [core:notice] [pid 1:tid 140464886181184] AH00094: Command line: 'httpd -D FOREGROUND'
192.168.183.1 - - [05/Aug/2022:08:14:21 +0000] "GET / HTTP/1.1" 200 45
192.168.183.1 - - [05/Aug/2022:08:14:21 +0000] "GET /favicon.ico HTTP/1.1" 404 196
192.168.183.1 - - [05/Aug/2022:08:15:12 +0000] "-" 408 -
192.168.183.1 - - [05/Aug/2022:08:17:06 +0000] "-" 408 -
192.168.183.1 - - [05/Aug/2022:08:17:06 +0000] "-" 408 -
//docker重启、停止web容器
[root@localhost ~]# docker restart web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d1d2ca1380d httpd "httpd-foreground" 6 minutes ago Up 4 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
039326b658b3 busybox "sleep 6000" 15 minutes ago Up 14 minutes relaxed_mcclintock
[root@localhost ~]# docker stop web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
039326b658b3 busybox "sleep 6000" 15 minutes ago Up 14 minutes relaxed_mcclintock
[root@localhost ~]#
//杀死容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d1d2ca1380d httpd "httpd-foreground" 7 minutes ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
039326b658b3 busybox "sleep 6000" 16 minutes ago Up 15 minutes relaxed_mcclintock
[root@localhost ~]# docker kill 039326b658b3
039326b658b3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d1d2ca1380d httpd "httpd-foreground" 7 minutes ago Up 21 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
//强制删除容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d1d2ca1380d httpd "httpd-foreground" 7 minutes ago Up 21 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
[root@localhost ~]# docker rm -f web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker帮助文档
[root@localhost ~]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default
"/root/.docker")
-c, --context string Name of the context to use to connect to the
daemon (overrides DOCKER_HOST env var and
default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default
"/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default
"/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.8.2-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.17.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
[root@localhost ~]#
docker存储驱动
docker提供了多种存储驱动来实现不同的方式存储镜像,下面是常用的几种存储驱动:
- AUFS
- OverlayFS
- Devicemapper
- Btrfs
- VFS