docker基础用法
文章目录
什么是docker
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.
- 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安装
[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
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1919 100 1919 0 0 3655 0 --:--:-- --:--:-- --:--:-- 3648
[root@localhost yum.repos.d]# ls
163.repo CentOS-Stream-HighAvailability.repo
CentOS-Stream-AppStream.repo CentOS-Stream-Media.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-PowerTools.repo
CentOS-Stream-Debuginfo.repo CentOS-Stream-RealTime.repo
CentOS-Stream-Extras.repo 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
[root@localhost yum.repos.d]# yum -y install docker-ce
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
EOF
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# 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
[root@localhost ~]# docker info
Client:
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: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
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.runtime.v1.linux runc io.containerd.runc.v2
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: 1.746GiB
Name: localhost.localdomain
ID: LJF6:WPLD:72NS:3BCY:Q6WY:LNHH:HBKD:325R:72UB:WIS4:7USD:6CP2
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://docker.mirrors.ustc.edu.cn/
Live Restore Enabled: false
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 |
| 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 search(搜索镜像)
用法:docker search 镜像名称
#搜索httpd镜像
[root@localhost ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3783 [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 8
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]
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1 [OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 1
jonathanheilmann/httpd-alpine-rewrite httpd:alpine with enabled mod_rewrite 1 [OK]
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
dockerpinata/httpd 1
manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
dariko/httpd-rproxy-ldap Apache httpd reverse proxy with LDAP authent… 1 [OK]
publici/httpd httpd:latest 1 [OK]
centos/httpd-24-centos8 1
appertly/httpd Customized Apache HTTPD that uses a PHP-FPM … 0 [OK]
amd64/httpd The Apache HTTP Server Project 0
interlutions/httpd httpd docker image with debian-based config … 0 [OK]
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
manasip/httpd 0
ysli/httpd Httpd for DeepWeb 0 [OK]
itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK]
trollin/httpd 0
e2eteam/httpd 0
docker pull(在镜像仓库拉取镜像)
用法: docker pull 镜像名称
[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
eff15d958d66: Pull complete
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
docker info(查看镜像信息)
用法:docker info
[root@localhost ~]# docker info
Client:
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: 0
Running: 0
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: 1.746GiB
Name: localhost.localdomain
ID: LJF6:WPLD:72NS:3BCY:Q6WY:LNHH:HBKD:325R:72UB:WIS4:7USD:6CP2
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://docker.mirrors.ustc.edu.cn/
Live Restore Enabled: false
docker images(列出镜像)
dockers image(对某一个镜像操作)
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest ad17c88403e2 12 days ago 143MB
docker create(创建容器)
[root@localhost ~]# docker create busybox #创建busybox容器,如果不存在该容器的镜像会自动去拉取该镜像
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
3aab638df1a9: Pull complete
Digest: sha256:52817dece4cfe26f581c834d27a8e1bcc82194f914afe6d50afad5a101234ef1
Status: Downloaded newer image for busybox:latest
67948fa119a3539605802519e1756b9ba291c96d1e7692657373e7fe361ad386
[root@localhost ~]# docker create httpd #创建httpd容器
92d1b5884dfd1390d8d5c2c415a8b882adc61b87227b591376fc02ff88f1ac24
docker start(启动镜像)
[root@localhost ~]# docker start 92d1b5884dfd
92d1b5884dfd
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92d1b5884dfd httpd "httpd-foreground" 2 minutes ago Up 21 seconds 80/tcp nifty_booth
##有的容器不可以启动,只会启动后立刻停止,可用sleep来启动
[root@localhost ~]# docker create busybox sleep 3600
a6f29ba2eac34ec62051bc5257f15014cfd04bfd439de48edf7499d53afc88d4
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6f29ba2eac3 busybox "sleep 3600" 14 seconds ago Created festive_pike
92d1b5884dfd httpd "httpd-foreground" 8 minutes ago Up 5 minutes 80/tcp nifty_booth
[root@localhost ~]# docker start a6f29ba2eac3
a6f29ba2eac3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6f29ba2eac3 busybox "sleep 3600" 37 seconds ago Up 3 seconds festive_pike
92d1b5884dfd httpd "httpd-foreground" 8 minutes ago Up 6 minutes 80/tcp nifty_booth
docker run(运行命令)
解释: -i 交互式模式 -t 指定终端 -d 运行一个容器在后台运行
[root@localhost ~]# docker run -it httpd /bin/sh
# ls
bin build cgi-bin conf error htdocs icons include logs modules
[root@localhost ~]# docker run -it busybox /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
22: eth0@if23: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@localhost ~]# docker run -d httpd
bba9c427ea8d1a151288d2a4fae8afec995d709b752c0c1aad3f4894b0127622
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
docker attach(记录容器)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9c62b22d3bd busybox "sleep 60000" 3 minutes ago Exited (137) 37 seconds ago xenodochial_pike
74641d77eac3 httpd "httpd-foreground" 16 minutes ago Up 16 minutes 80/tcp stupefied_vaughan
[root@localhost ~]# docker start b9c62b22d3bd
b9c62b22d3bd
[root@localhost ~]# docker attach b9c62b22d3bd
docker ps(列出容器)
解释:-a 查看所有容器,包括没启动的,默认查看启动了的
-aq 列出所有容器id
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74641d77eac3 httpd "httpd-foreground" 9 minutes ago Up 9 minutes 80/tcp stupefied_vaughan
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1fabb1161db busybox "sh" 12 seconds ago Created clever_einstein
f49883b437db busybox "sh" 13 seconds ago Created silly_galois
85f3f48b6230 busybox "sh" 14 seconds ago Created quizzical_tesla
e10fcae3d4ed busybox "sh" 15 seconds ago Created intelligent_tharp
74641d77eac3 httpd "httpd-foreground" 9 minutes ago Up 9 minutes 80/tcp stupefied_vaughan
6f813be13cf9 httpd "httpd-foreground" 9 minutes ago Exited (0) 9 minutes ago elastic_chebyshev
bba9c427ea8d httpd "httpd-foreground" 12 minutes ago Exited (0) 10 minutes ago clever_cerf
docker logs(获取日志)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74641d77eac3 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 80/tcp stupefied_vaughan
[root@localhost ~]# docker logs 74641d77eac3
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 Dec 01 08:54:24.853961 2021] [mpm_event:notice] [pid 1:tid 139686278569280] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 08:54:24.854186 2021] [core:notice] [pid 1:tid 139686278569280] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [01/Dec/2021:08:57:11 +0000] "GET / HTTP/1.1" 200 45
socker restart(重启镜像)
[root@localhost ~]# docker restart 92d1b5884dfd
92d1b5884dfd
docker stop(停止镜像)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6f29ba2eac3 busybox "sleep 3600" 4 minutes ago Up 3 minutes festive_pike
92d1b5884dfd httpd "httpd-foreground" 11 minutes ago Up About a minute 80/tcp nifty_booth
[root@localhost ~]# docker stop 92d1b5884dfd
92d1b5884dfd
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6f29ba2eac3 busybox "sleep 3600" 4 minutes ago Up 3 minutes festive_pike
docker kill(杀死镜像的进程)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6f29ba2eac3 busybox "sleep 3600" 4 minutes ago Up 3 minutes festive_pike
[root@localhost ~]# docker kill a6f29ba2eac3
a6f29ba2eac3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
docker rm(删除镜像)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6f29ba2eac3 busybox "sleep 3600" 5 minutes ago Exited (137) 24 seconds ago festive_pike
92d1b5884dfd httpd "httpd-foreground" 13 minutes ago Exited (0) 50 seconds ago nifty_booth
[root@localhost ~]# docker rm a6f29ba2eac3
a6f29ba2eac3
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92d1b5884dfd httpd "httpd-foreground" 13 minutes ago Exited (0) About a minute ago nifty_booth
##删除没有在运行的容器
[root@localhost ~]# docker rm $(docker ps -aq)
d1fabb1161db
f49883b437db
85f3f48b6230
e10fcae3d4ed
6f813be13cf9
bba9c427ea8d
cd081f1aa9d9
b2eecb480be3
2a43ecb8957b
afea5e2981d4
01eb9d01ed85
b7767c3f73a0
92d1b5884dfd
Error response from daemon: You cannot remove a running container 74641d77eac3b994aa77d39a3aa405c39cf0c0cc807ddceaf4007bc9dd126756. Stop the container before attempting removal or force remove
docker exec(记录容器)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74641d77eac3 httpd "httpd-foreground" 18 minutes ago Up 2 seconds 80/tcp stupefied_vaughan
[root@localhost ~]# docker exec -it 74641d77eac3 /bin/bash
root@74641d77eac3:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@74641d77eac3:/usr/local/apache2# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
docker inspect(查看容器的详细信息)
[root@localhost ~]# docker inspect 74641d77eac3
[
{
"Id": "74641d77eac3b994aa77d39a3aa405c39cf0c0cc807ddceaf4007bc9dd126756",
"Created": "2021-12-01T08:54:24.167485117Z",
"Path": "httpd-foreground",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 162014,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-12-01T08:54:24.824108691Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:ad17c88403e2cedd27963b98be7f04bd3f903dfa7490586de397d0404424936d",
"ResolvConfPath": "/var/lib/docker/containers/74641d77eac3b994aa77d39a3aa405c39cf0c0cc807ddceaf4007bc9dd126756/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/74641d77eac3b994aa77d39a3aa405c39cf0c0cc807ddceaf4007bc9dd126756/hostname",
"HostsPath": "/var/lib/docker/containers/74641d77eac3b994aa77d39a3aa405c39cf0c0cc807ddceaf4007bc9dd126756/hosts",
"LogPath":
····省略部分·····
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4ea01950334b47fadc5c85ac7f3f2b6d0e1b63c5ac5d08aa4e2023e19777db3a",
"EndpointID": "3bdce4709b5a37b49f571342625ac6ade8773d6668f35ecd1c1668b6a117eb15",
"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
}
}
}
}
]
docker event state

本文介绍了Docker的基础概念,包括oci和ocf标准,详细讲解了docker的架构和镜像仓库。展示了如何在CentOS上安装Docker并设置加速器,以及常用的操作如搜索、拉取、查看和管理镜像,创建、启动、停止和删除容器。此外,还涵盖了容器的日志、状态检查等实用命令。
584

被折叠的 条评论
为什么被折叠?



