Docker 基础
[root@controller ~]
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@controller ~]
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: 13
Running: 2
Paused: 0
Stopped: 11
Images: 2
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
……
Image
[root@controller ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 2b35e80091ed 2 days ago 680MB
centos latest 5d0da3dc9764 6 months ago 231MB
[root@controller ~]
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec… 7254 [OK]
continuumio/anaconda3 Powerful and flexible python distribution 597 [OK]
pypy PyPy is a fast, compliant alternative implem… 316 [OK]
continuumio/anaconda Powerful and flexible python distribution 219 [OK]
circleci/python Python is an interpreted, interactive, objec… 48
hylang Hy is a Lisp dialect that translates express… 44 [OK]
amazon/aws-lambda-python AWS Lambda base images for Python 41
bitnami/python Bitnami Python Docker Image 17 [OK]
cimg/python 4
google/guestbook-python-redis A simple guestbook example written in Python… 4
mirantis/python-operations-api https://mirantis.jira.com/browse/IT-40189 0 [OK]
okteto/python-fastapi 0
appdynamics/python-agent-init AppDynamics Repository for Python agent inst… 0
ibmcom/python-semver-ppc64le Docker image for python-semver-ppc64leDocker… 0
ibmcom/python-ceilometerclient-ppc64le Docker image for python-ceilometerclient-ppc… 0
ibmcom/python-memcached-ppc64le Docker image for python-memcached-ppc64le 0
ibmcom/python-dropbox-ppc64le Docker image for python-dropbox-ppc64leDocke… 0
ibmcom/python-glanceclient-ppc64le Docker image for python-glanceclient-ppc64le 0
pachyderm/python-build 0
ibmcom/pythonfutures-ppc64le Docker image for pythonfutures-ppc64le 0
bitnami/python-snapshot 0
pachyderm/python-evaluate 0
okteto/python 0
okteto/python-job-launcher 0
ibmcom/python-sybase-ppc64le Docker image for python-sybase-ppc64le 0
[root@controller ~]
Using default tag: latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
vim /etc/docker/daemon.json
{
"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]
}
:wq
systemctl restart docker.service
docker rmi -f 删除镜像
docker rmi -f 镜像ID 删除单个镜像
docker rmi -f 镜像名1:TAG 镜像名2:TAG 删除多个
docker rmi -f $(docker images -qa) 删除全部
Container
docker run [option] image [command]
--name="容器新名字": 为容器指定一个名称;
-d: 后台运行容器,并返回容器ID,也即启动守护式容器;
-i:以交互模式运行容器,通常与 -t 同时使用;
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-P: 随机端口映射;
-p: 指定端口映射
[root@controller ~] docker run --name testco1 -dit -P centos /bin/bash
dad93cafdcf2e29f56703c06058814e09373c82da2108050d2892686d33f2f67
[root@controller ~]
-a:列出当前所有正在运行的容器+历史上运行过的
-l:显示最近创建的容器
-n:显示最近n个创建的容器
-q:静默模式,只显示容器编号
--no-trunc:不截断输出
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dad93cafdcf2 centos "/bin/bash" 40 seconds ago Up 39 seconds testco1
[root@controller ~] docker attach testco
[root@dee0506983a6 /]
[root@dee0506983a6 /]
or ctrl+q+p 容器不停止退出
启动容器:docker start 容器ID或者容器名
重启容器:docker restart 容器ID或者容器名
停止容器:docker stop 容器ID或者容器名
强制停止容器:docker kill 容器ID或者容器名
删除单个容器:docker rm 容器ID
删除多个容器:docker rm -f $(docker ps -a -q)
重要指令
docker logs 容器ID
-t:是加入时间戳
-f:跟随最新的日志打印
--tail:数字显示最后多少条
[root@controller ~]
2022-04-02T03:32:46.665556554Z 02-Apr-2022 03:32:46.665 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
2022-04-02T03:32:46.793829826Z 02-Apr-2022 03:32:46.793 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
2022-04-02T03:32:46.793862170Z 02-Apr-2022 03:32:46.793 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
2022-04-02T03:32:46.802781816Z 02-Apr-2022 03:32:46.802 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1n 15 Mar 2022]
2022-04-02T03:32:47.374406938Z 02-Apr-2022 03:32:47.371 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
2022-04-02T03:32:47.416283648Z 02-Apr-2022 03:32:47.414 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [1404] milliseconds
2022-04-02T03:32:47.496488539Z 02-Apr-2022 03:32:47.496 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
2022-04-02T03:32:47.496562918Z 02-Apr-2022 03:32:47.496 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.18]
2022-04-02T03:32:47.518440878Z 02-Apr-2022 03:32:47.516 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
2022-04-02T03:32:47.535351228Z 02-Apr-2022 03:32:47.535 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [120] milliseconds
root@controller ~]
UID PID PPID C STIME TTY TIME CMD
root 6832 6813 2 11:32 ? 00:00:03 /usr/local/openjdk-11/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
[root@controller ~]
[
{
"Id": "3a4d6964f0e7ff04bfa435546c67ff9180a3cd50081bab01a3f76a83a4ac7e2c",
"Created": "2022-03-31T09:56:58.714058461Z",
"Path": "catalina.sh",
"Args": [
"run"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 6832,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-04-02T03:32:44.170435831Z",
"FinishedAt": "2022-04-02T02:24:22.572335853Z"
},
"Image": "sha256:2b35e80091ed529ecb0287e9d515314cfb007f3ccf29b5f7e9a3b8e50174ff8b",
"ResolvConfPath": "/var/lib/docker/containers/3a4d6964f0e7ff04bfa435546c67ff9180a3cd50081bab01a3f76a83a4ac7e2c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3a4d6964f0e7ff04bfa435546c67ff9180a3cd50081bab01a3f76a83a4ac7e2c/hostname",
"HostsPath": "/var/lib/docker/containers/3a4d6964f0e7ff04bfa435546c67ff9180a3cd50081bab01a3f76a83a4ac7e2c/hosts",
"LogPath": "/var/lib/docker/containers/3a4d6964f0e7ff04bfa435546c67ff9180a3cd50081bab01a3f76a83a4ac7e2c/3a4d6964f0e7ff04bfa435546c67ff9180a3cd50081bab01a3f76a83a4ac7e2c-json.log",
"Name": "/testport1",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
docker cp 容器ID:容器内路径 目标主机路径
[root@controller ~]
此时可用exec
[root@controller ~]
root@3a4d6964f0e7:/usr/local/tomcat
![命令](https://i-blog.csdnimg.cn/blog_migrate/c24236fbe54c21fa357b00cb84b23c95.png)