镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系
容器是可以删除的,容器被删除后其镜像是不会被删除的。
[root@SYL4 ~]# cd /etc/yum.repos.d/
[root@SYL4 yum.repos.d]# ls
CentOS-Base.repo epel-testing-modular.repo epel.repo
epel-modular.repo epel-testing.repo redhat.repo
[root@SYL4 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@SYL4 ~]# yum list all|grep docker
[root@SYL4 ~]# yum -y install docker-ce
[root@SYL4 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@SYL4 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; en>
Active: active (running) since Sun 2022-04-24 15:57:13 CST>
Docs: https://docs.docker.com
Main PID: 198086 (dockerd)
Tasks: 9
Memory: 29.0M
CGroup: /system.slice/docker.service
└─198086 /usr/bin/dockerd -H fd:// --containerd=/r>
[root@SYL4 ~]# ls /etc/docker/
key.json
[root@SYL4 ~]#
[root@SYL4 ~]# cd /etc/docker/
[root@SYL4 docker]# ls
key.json
[root@SYL4 docker]# vim daemon.json
[root@SYL4 docker]# cat daemon.json
{
"registry-mirrors": ["https://szudx43q.mirror.aliyuncs.com"]
}
[root@SYL4 docker]# systemctl daemon-reload
[root@SYL4 docker]# systemctl restart docker
[root@SYL4 docker]#
docker info 显示整个系统的信息
docker version 查看版本号
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 inspect 返回Docker对象的低级信息/详细信息
[root@SYL4 ~]# docker info
Client:
Context: default //调试模式
Debug Mode: false//插件
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.8.1-docker)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 0//容器
Running: 0//运行
Paused: 0//暂停
Stopped: 0//停止
Images: 0//镜像
Server Version: 20.10.14
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: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc version: v1.0.3-0-gf46b6ba
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.18.0-348.el8.x86_64
Operating System: Red Hat Enterprise Linux 8.5 (Ootpa)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 1.748GiB
Name: SYL4
ID: VHGE:CXZW:RYB7:GKM2:X5V6:F5ZP:6EGP:EBEK:JCZY:5XIK:CXNF:TKOG
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://szudx43q.mirror.aliyuncs.com/
Live Restore Enabled: false
[root@SYL4 ~]#
[root@SYL4 ~]# docker version
Client: Docker Engine - Community
Version: 20.10.14
API version: 1.41
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:47:44 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.14
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 87a90dc
Built: Thu Mar 24 01:46:10 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.5.11
GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc:
Version: 1.0.3
GitCommit: v1.0.3-0-gf46b6ba
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@SYL4 ~]#
[root@SYL4 ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3975 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 44
centos/httpd 35 [OK]
hypoport/httpd-cgi httpd-cgi 2 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
dockerpinata/httpd 1
publici/httpd httpd:latest 1 [OK]
dariko/httpd-rproxy-ldap Apache httpd reverse proxy with LDAP authent… 1 [OK]
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1 [OK]
centos/httpd-24-centos8 1
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 1
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
manasip/httpd 0
e2eteam/httpd 0
paketobuildpacks/httpd 0
httpdocker/kubia 0
sandeep1988/httpd-new httpd-new 0
httpdocker/kubia-unhealthy 0
amd64/httpd The Apache HTTP Server Project 0
patrickha/httpd-err 0
httpdss/archerysec ArcherySec repository 0 [OK]
19022021/httpd-connection_test This httpd image will test the connectivity … 0
itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK]
[root@SYL4 ~]#
[root@SYL4 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@SYL4 ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete
dcc4698797c8: Pull complete
41c22baa66ec: Pull complete
67283bbdd4a0: Pull complete
d982c879c57e: Pull complete
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
[root@SYL4 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest dabbfbe0c57b 4 months ago 144MB
[root@SYL4 ~]#
[root@SYL4 ~]# docker pull httpd:2.4.53
2.4.53: Pulling from library/httpd
1fe172e4850f: Already exists
e2fa1fe9b1ec: Already exists
60dd7398e74e: Already exists
ea2ca81c6d4c: Already exists
f646c69a26ec: Already exists
Digest: sha256:e02a2ef36151905c790efb0a8472f690010150f062639bd8c0760e7b1e884c07
Status: Downloaded newer image for httpd:2.4.53
docker.io/library/httpd:2.4.53
[root@SYL4 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4.53 c30a46771695 4 days ago 144MB
httpd latest dabbfbe0c57b 4 months ago 144MB
[root@SYL4 ~]#
docker create --name+名字 -p容器端口映射真机 +服务
docker ps 列出本地容器 -a列出未启动容器
[root@SYL4 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd 2.4.53 c30a46771695 4 days ago 144MB
httpd latest dabbfbe0c57b 4 months ago 144MB
[root@SYL4 ~]# docker create --name web -p 80:80 httpd:2.4.53
0146899d0c7aec70c6b61c6e3fd60a85c8094d9b1d15d30a59fe36d9ab017223
[root@SYL4 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@SYL4 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0146899d0c7a httpd:2.4.53 "httpd-foreground" 26 seconds ago Created web
[root@SYL4 ~]#
启动
[root@SYL4 ~]# docker start web
web
[root@SYL4 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0146899d0c7a httpd:2.4.53 "httpd-foreground" 5 minutes ago Up 22 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
[root@SYL4 ~]#
[root@SYL4 ~]# systemctl disable --now firewalld
[root@SYL4 ~]# setenforce 0
[root@SYL4 ~]# vi /etc/selinux/config
停止
[root@SYL4 ~]# docker stop web
web
[root@SYL4 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
重启
[root@SYL4 ~]# docker restart web
web
[root@SYL4 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0146899d0c7a httpd:2.4.53 "httpd-foreground" 11 minutes ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
[root@SYL4 ~]#
[root@SYL4 ~]# docker rm -f web//删除容器
web
[root@SYL4 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@SYL4 ~]#
[root@SYL4 ~]# docker kill web
web
[root@SYL4 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
716728e19427 httpd "httpd-foreground" 6 minutes ago Exited (137) 8 seconds ago web
[root@SYL4 ~]#
[root@SYL4 ~]# docker logs web
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
[Sun Apr 24 10:26:21.809014 2022] [mpm_event:notice] [pid 1:tid 139621542448448] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sun Apr 24 10:26:21.833449 2022] [core:notice] [pid 1:tid 139621542448448] AH00094: Command line: 'httpd -D FOREGROUND'
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
[Sun Apr 24 10:30:25.983348 2022] [mpm_event:notice] [pid 1:tid 139987630406976] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sun Apr 24 10:30:25.983485 2022] [core:notice] [pid 1:tid 139987630406976] AH00094: Command line: 'httpd -D FOREGROUND'
192.168.232.1 - - [24/Apr/2022:10:33:45 +0000] "GET / HTTP/1.1" 304 -
192.168.232.1 - - [24/Apr/2022:10:33:46 +0000] "GET / HTTP/1.1" 304 -
192.168.232.1 - - [24/Apr/2022:10:33:48 +0000] "GET / HTTP/1.1" 304 -
192.168.232.1 - - [24/Apr/2022:10:33:53 +0000] "GET / HTTP/1.1" 304 -
192.168.232.1 - - [24/Apr/2022:10:33:55 +0000] "GET / HTTP/1.1" 304 -
192.168.232.1 - - [24/Apr/2022:10:34:11 +0000] "GET /bbdb%20n HTTP/1.1" 404 196
[root@SYL4 ~]#
-it 交互模式 在新容器执行命令,退出后容器就停掉了
[root@SYL4 ~]# docker run -it --name mu busybox /bin/sh
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
/ # 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
36: eth0@if37: <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@SYL4 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1dd0826017e httpd "httpd-foreground" 6 minutes ago Up 6 minutes 80/tcp shuang
6252bbff7a3c busybox "/bin/sh" 21 hours ago Exited (255) 3 hours ago mu
716728e19427 httpd "httpd-foreground" 21 hours ago Exited (255) 3 hours ago 0.0.0.0:80->80/tcp, :::80->80/tcp web
[root@SYL4 ~]#
-d 在后台运行,不指定端口号只能在虚拟机上访问
[root@SYL4 ~]# docker run -d --name shuang httpd
b1dd0826017eec7727e0b7c7fd20442ab952318fad1aba82e84ba55b1a747894
[root@SYL4 ~]# docker inspect shuang
"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@SYL4 ~]# curl 172.17.0.2
<html><body><h1>It works!