Docker - 镜像 | 容器 日常开发常用指令 + 演示(一文通关)

发布时间:2023年12月23日

目录

Docker 开发常用指令汇总

辅助命令

docker version

docker info

docker --help

镜像命令

查看镜像信息

下载镜像

搜索镜像

删除镜像

容器命令

查看运行中的容器

运行容器

停止、启动、重启、暂停、恢复容器

杀死容器

删除容器

查看容器日志

进入容器内部

容器与宿主机之间的文件拷贝

如何部署一个 web 项目(war 包项目)

查看容器内运行的进程信息

查看容器内部细节

容器数据卷机制

作用

注意

使用

容器打包镜像

背景

将容器打包成一个新的镜像

镜像的备份和恢复


Docker 开发常用指令汇总


辅助命令

docker version

用来查看 docker 客户端引擎 和 server 端引擎版本信息

[root@VM-8-17-centos ~]# docker version
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:11:35 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:10:36 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.24
  GitCommit:        61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
 runc:
  Version:          1.1.9
  GitCommit:        v1.1.9-0-gccaecfc
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker info

用来查看 docker 引擎详细信息

[root@VM-8-17-centos ~]# docker info
Client: Docker Engine - Community
 Version:    24.0.7
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.11.2
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.21.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 4
  Running: 4
  Paused: 0
  Stopped: 0
 Images: 8
 Server Version: 24.0.7
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  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 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
 runc version: v1.1.9-0-gccaecfc
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
 Kernel Version: 3.10.0-1160.99.1.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.952GiB
 Name: VM-8-17-centos
 ID: 38c01492-c895-4bbc-8e2e-a575241b4201
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://q9n10oke.mirror.aliyuncs.com/
 Live Restore Enabled: false

docker --help

用来查看帮助文档,给出了对于不同指令的解释

[root@VM-8-17-centos ~]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.11.2)
  compose*    Docker Compose (Docker Inc., v2.21.0)
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  swarm       Manage Swarm

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  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
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  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
  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
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  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
  wait        Block until one or more containers stop, then print their exit codes

Global 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 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

Run 'docker COMMAND --help' for more information on a command.

For more help on how to use Docker, head to https://docs.docker.com/go/guides/

镜像命令

查看镜像信息

查看当前本地仓库中存在哪些镜像.

docker images -q | 镜像名
// -q 和 镜像名 是可选参数

a)docker images 就是 docker image ls 的缩写,用来镜像以及信息

[root@VM-8-17-centos ~]# docker images
REPOSITORY                  TAG              IMAGE ID       CREATED       SIZE
redis                       5                c5da061a611a   2 years ago   110MB
mysql                       5.7              c20987f18b13   2 years ago   448MB
rabbitmq                    3.8-management   699038cb2b96   2 years ago   250MB
nacos/nacos-server          1.4.2            938169b118c7   2 years ago   956MB
redis                       5.0.9            987b553c835f   3 years ago   98.3MB
bladex/sentinel-dashboard   1.8.0            61c10eaefbcf   3 years ago   147MB
nacos/nacos-server          1.2.0            763941e566bb   3 years ago   732MB
bladex/sentinel-dashboard   1.7.1            d12e18546bfd   3 years ago   148MB
  1. REPOSITORY:镜像名称
  2. TAG:版本
  3. IMAGE ID:镜像 id
  4. CREATED:镜像创建的时间(官方创建此镜像的时间)
  5. SIZE:镜像大小

b)docker images -q 是用来查看所有镜像 id 的

[root@VM-8-17-centos ~]# docker images -q
c5da061a611a
c20987f18b13
699038cb2b96
938169b118c7
987b553c835f
61c10eaefbcf
763941e566bb
d12e18546bfd

Ps:一个镜像的 id 是很长的,这里只会展示一个部分

c)docker images 镜像名 用来查看指定镜像信息(镜像名相同,版本不同)

[root@VM-8-17-centos ~]# docker images redis
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
redis        5         c5da061a611a   2 years ago   110MB
redis        5.0.9     987b553c835f   3 years ago   98.3MB

同理,docker images -q 镜像名 就是展示指定镜像名的镜像id.

[root@VM-8-17-centos ~]# docker images -q redis
c5da061a611a
987b553c835f

下载镜像

用来下载一个镜像

docker pull 镜像名:版本号

a)例如下载最新版本 redis:docker pull redis 等同于 docker pull redis:latest

b)例如下载指定版本 redis:docker pull redis:5.0.9

搜索镜像

搜索镜像,只能查看远端仓库是否存在该镜像,不能列出版本.

docker search 镜像名

a)例如查看 tomcat 镜像是否存在

[root@VM-8-17-centos ~]# docker search tomcat
NAME                                  DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat                                Apache Tomcat is an open source implementati…   3618      [OK]       
tomee                                 Apache TomEE is an all-Apache Java EE certif…   113       [OK]       
bitnami/tomcat                        Bitnami Tomcat Docker Image                     51                   [OK]
bitnamicharts/tomcat                                                                  0                    
secoresearch/tomcat-varnish           Tomcat and Varnish 5.0                          0                    [OK]
vulhub/tomcat                                                                         0                    
islandora/tomcat                                                                      0                    
wnprcehr/tomcat                                                                       0                    
jumpserver/tomcat                     Apache Tomcat is an open source implementati…   0                    
hivdb/tomcat-with-nucamino                                                            0                    
sismics/tomcat                        Apache Tomcat Servlet Container                 1                    
eclipse/rdf4j-workbench               Dockerfile for Eclipse RDF4J Server and Work…   6                    
eclipse/hadoop-dev                    Ubuntu 14.04, Maven 3.3.9, JDK8, Tomcat 8       0                    [OK]
gbif/ipt                              The GBIF Integrated Publishing Toolkit (IPT)…   2                    
dhis2/base-dev                        Images in this repository contains DHIS2 WAR…   0                    
eclipse/alpine_jdk8                   Based on Alpine 3.3. JDK 1.8, Maven 3.3.9, T…   1                    [OK]
dhis2/base                            Images in this repository contains DHIS2 WAR…   0                    
jelastic/tomcat                       An image of the Tomcat Java application serv…   4                    
cfje/tomcat-resource                  Tomcat Concourse Resource                       2                    
rightctrl/tomcat                      CentOS , Oracle Java, tomcat application ssl…   7                    [OK]
amd64/tomcat                          Apache Tomcat is an open source implementati…   8                    
arm64v8/tomcat                        Apache Tomcat is an open source implementati…   9                    
tomcat2111/papercut-mf                PaperCut MF Application Server                  0                    
softwareplant/tomcat                  Tomcat images for jira-cloud testing            0                    [OK]
tomcatengineering/pg_backup_rotated   Clone of martianrock/pg_backup_rotated but w…   0                    

删除镜像

删除指定镜像

docker image rm 镜像名(name:tag) | 镜像id

a)正常删除(要求:必须是没有正在运行的镜像),例如删除 tomcat:8.0 镜像

[root@VM-8-17-centos ~]# docker image rm tomcat:8.0
Untagged: tomcat:8.0
Untagged: tomcat@sha256:8ecb10948deb32c34aeadf7bf95d12a93fbd3527911fa629c1a3e7823b89ce6f
Deleted: sha256:ef6a7c98d192507d6066dcf24e44bec66d07ec9cf7c55d8d3d1ea0a24660bdef
Deleted: sha256:6dc5bbd9ede58ce52f6e9d34eaac4e3bf1fbb3014e9eae344677bf3c2f022a17
Deleted: sha256:6f22c12dcf0ac783b0b00471e38262c708761a677e6a847a57a5d7cce2cbc7f1
Deleted: sha256:dbefc7cd29b21d99c136d6234ca442cdb89fc91f3c91ef6c1d333d37424d2f44
Deleted: sha256:d65396dbf112285c9d395bb33c2eb2f776cd9b959c007152c10a2f25fbf1c198
Deleted: sha256:5b8a875f80bdd2e9f9cf2c0852243e0cb59ea7be5e92620b2c2089800bb18594
Deleted: sha256:d97b31b9ca51e024a895e2f6fa032224fb73ba2a070149499ce87306ed9d7af6
Deleted: sha256:3717c4f05493340d97d982bc6ed8cba98869833918de23c78c39414788dd08c1
Deleted: sha256:b9c326fb510188d2958888aee970ae93925c2a2ad8a78b2ec3a521747ee3ec4f
Deleted: sha256:8cc193ff8a5b4837eab54bee27418daadad8825f13b8fab55229b4954fee6f9f
Deleted: sha256:b1852f6f3f85863ed849af673e566ad8de76dac4d2c6526d791e965978c1ce62
Deleted: sha256:8c466bf4ca6ffdda8b7717b1cd6fe31109529ee64e626a003a224fb8bd2bc469

b)强制删除(可以删除正在运行的镜像):docker image rm -f 镜像名(name:tag) | 镜像 id

c)批量删除,例如删除所有 tomcat 镜像:docker image rm -f $(docker image tomcat -q)

容器命令

查看运行中的容器

查看运行中的容器信息

docker ps -a | -q

a)查看当前 docker 引擎中正在运行的容器

[root@VM-8-17-centos ~]# docker ps
CONTAINER ID   IMAGE                      COMMAND                  CREATED       STATUS       PORTS                                                                                                                                                 NAMES
abc6284fe34d   redis:5.0.9                "docker-entrypoint.s…"   5 weeks ago   Up 5 weeks   0.0.0.0:6380->6379/tcp, :::6380->6379/tcp                                                                                                             redis
d442ff7829df   nacos/nacos-server:1.4.2   "bin/docker-startup.…"   6 weeks ago   Up 5 weeks   0.0.0.0:8848->8848/tcp, :::8848->8848/tcp                                                                                                             nacos
fc16ae62dc0f   mysql:5.7                  "docker-entrypoint.s…"   7 weeks ago   Up 7 weeks   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp                                                                                                  mysql
a7dbf979b1e6   rabbitmq:3.8-management    "docker-entrypoint.s…"   7 weeks ago   Up 7 weeks   4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp   mq
  • CONTAINER ID:容器 id
  • IMAGE:基于那个镜像运行的
  • COMMAND:容器内启动服务的命令
  • CREATED:容器创建时间
  • STATUS:容器状态
  • PORTS:容器服务监听端口("->" 前表示宿主机端口,"->" 后表示映射的容器端口)
  • NAMES:容器名称(启动时,通过 --name 参数自定义的)

b)查看所有容器(运行 和 停止 的容器):docker ps -a

c)查看所有容器id:docker ps -aq

运行容器

docker run -d(后台运行) -p(端口映射) --name 自定义容器名 镜像名:tag

以下拿 tomcat:8.0 来举例?

a)简单运行一个容器(这种方式运行容器,会导致容器内无法访问,因为这里涉及到容器端口映射的概念,后面会讲)

[root@VM-8-17-centos ~]# docker run tomcat:8.0
23-Dec-2023 12:25:27.847 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.53
23-Dec-2023 12:25:27.849 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jun 29 2018 14:42:45 UTC
23-Dec-2023 12:25:27.849 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.53.0
23-Dec-2023 12:25:27.849 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
23-Dec-2023 12:25:27.849 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-1160.99.1.el7.x86_64
23-Dec-2023 12:25:27.849 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
23-Dec-2023 12:25:27.849 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-7-openjdk-amd64/jre
23-Dec-2023 12:25:27.849 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.7.0_181-b01
23-Dec-2023 12:25:27.850 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
23-Dec-2023 12:25:27.850 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
23-Dec-2023 12:25:27.850 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
23-Dec-2023 12:25:27.851 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
23-Dec-2023 12:25:27.851 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
23-Dec-2023 12:25:27.851 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
23-Dec-2023 12:25:27.851 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
23-Dec-2023 12:25:27.851 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
23-Dec-2023 12:25:27.851 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
23-Dec-2023 12:25:27.851 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
23-Dec-2023 12:25:27.851 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
23-Dec-2023 12:25:27.852 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.5.1.
23-Dec-2023 12:25:27.852 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
23-Dec-2023 12:25:27.855 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0f  25 May 2017)
23-Dec-2023 12:25:28.071 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
23-Dec-2023 12:25:28.083 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
23-Dec-2023 12:25:28.086 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 951 ms
23-Dec-2023 12:25:28.147 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
23-Dec-2023 12:25:28.148 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53
23-Dec-2023 12:25:28.163 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
23-Dec-2023 12:25:28.827 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 663 ms
23-Dec-2023 12:25:28.827 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
23-Dec-2023 12:25:29.319 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 492 ms
23-Dec-2023 12:25:29.319 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
23-Dec-2023 12:25:29.379 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 60 ms
23-Dec-2023 12:25:29.379 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
23-Dec-2023 12:25:29.439 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 60 ms
23-Dec-2023 12:25:29.439 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT
23-Dec-2023 12:25:29.468 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 29 ms
23-Dec-2023 12:25:29.486 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
23-Dec-2023 12:25:29.550 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
23-Dec-2023 12:25:29.554 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1467 ms

b)运行 tomcat 容器,设置容器与宿主机端口映射关系

docker run -p 8081:8080 tomcat:8.0

Ps:-p 8081(宿主机):8080(容器)

c)运行 tomcat 容器,设置设置容器与宿主机端口映射关系,后台启动服务

docker run -p 8081:8080 -d tomcat:8.0

d)运行 tomcat 容器,设置设置容器与宿主机端口映射关系,后台启动服务,自定义容器名称

docker run -p 8081:8080 -d --name tomcat_1 tomcat:8.0

停止、启动、重启、暂停、恢复容器

停止容器:docker stop 容器id | name(这里的 name 是启动时自定义的)

启动容器:docker start 容器 id | name

重启容器:docker restart 容器 id | name

暂停容器:docker pause?容器 id | name

恢复容器(从暂停恢复到启动):docker unpause?容器 id | name

杀死容器

docker kill id | name

例如删除名称为 tomcat_1 的容器?

[root@VM-8-17-centos ~]# docker kill tomcat_1
tomcat_1

删除容器

a)正常删除容器(只能删除已经停止的容器):docker rm id | name

b)强制删除容器(可以删除运行中的容器):docker rm -f id | name

c)批量删除容器:docker rm -f $(docker ps -qa)

查看容器日志

docker logs -f id | name

a)查看当前容器的日志信息,例如查看 tomcat_1 容器的日志

[root@VM-8-17-centos ~]# docker logs tomcat_1
23-Dec-2023 12:50:00.505 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.53
23-Dec-2023 12:50:00.508 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jun 29 2018 14:42:45 UTC
23-Dec-2023 12:50:00.508 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.53.0
23-Dec-2023 12:50:00.508 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
23-Dec-2023 12:50:00.508 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-1160.99.1.el7.x86_64
23-Dec-2023 12:50:00.509 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
23-Dec-2023 12:50:00.509 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-7-openjdk-amd64/jre
23-Dec-2023 12:50:00.509 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.7.0_181-b01
23-Dec-2023 12:50:00.509 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
23-Dec-2023 12:50:00.509 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
23-Dec-2023 12:50:00.509 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
23-Dec-2023 12:50:00.510 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
23-Dec-2023 12:50:00.510 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
23-Dec-2023 12:50:00.510 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
23-Dec-2023 12:50:00.510 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
23-Dec-2023 12:50:00.510 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
23-Dec-2023 12:50:00.510 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
23-Dec-2023 12:50:00.510 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
23-Dec-2023 12:50:00.510 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
23-Dec-2023 12:50:00.511 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.5.1.
23-Dec-2023 12:50:00.511 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
23-Dec-2023 12:50:00.515 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0f  25 May 2017)
23-Dec-2023 12:50:00.712 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
23-Dec-2023 12:50:00.724 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
23-Dec-2023 12:50:00.726 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 920 ms
23-Dec-2023 12:50:00.780 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
23-Dec-2023 12:50:00.780 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53
23-Dec-2023 12:50:00.795 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
23-Dec-2023 12:50:01.435 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 639 ms
23-Dec-2023 12:50:01.435 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
23-Dec-2023 12:50:01.931 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 496 ms
23-Dec-2023 12:50:01.931 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
23-Dec-2023 12:50:02.001 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 70 ms
23-Dec-2023 12:50:02.002 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
23-Dec-2023 12:50:02.075 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 73 ms
23-Dec-2023 12:50:02.075 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT
23-Dec-2023 12:50:02.108 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 33 ms
23-Dec-2023 12:50:02.118 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
23-Dec-2023 12:50:02.152 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
23-Dec-2023 12:50:02.155 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1429 ms

b)通过 -f 实时监控容器内服务的日志:docker logs -f id | name

进入容器内部

docker exec -it(交互模式) id | name bash

a)例如进入 tomcat_1 容器内部,然后查看容器内部的文件

[root@VM-8-17-centos ~]# docker exec -it tomcat_1 bash
root@e823da882acc:/usr/local/tomcat# ls
LICENSE  NOTICE  RELEASE-NOTES	RUNNING.txt  bin  conf	include  lib  logs  native-jni-lib  temp  webapps  work

b)退出容器:exit

root@e823da882acc:/usr/local/tomcat# exit
exit
[root@VM-8-17-centos ~]# 

容器与宿主机之间的文件拷贝

a)将容器中指定文件拷贝到宿主机:docker cp 容器id:容器中文件或目录 宿主机目录

例如将 tomcat 容器中的 /usr/local/tomcat/RUNNING.txt 文件拷贝到宿主机的?/root/cyk/?目录下

[root@VM-8-17-centos cyk]# docker ps
CONTAINER ID   IMAGE                      COMMAND                  CREATED          STATUS          PORTS                                                                                                                                                 NAMES
e823da882acc   tomcat:8.0                 "catalina.sh run"        11 minutes ago   Up 11 minutes   0.0.0.0:8082->8080/tcp, :::8082->8080/tcp                                                                                                             tomcat_1
abc6284fe34d   redis:5.0.9                "docker-entrypoint.s…"   5 weeks ago      Up 5 weeks      0.0.0.0:6380->6379/tcp, :::6380->6379/tcp                                                                                                             redis
d442ff7829df   nacos/nacos-server:1.4.2   "bin/docker-startup.…"   6 weeks ago      Up 5 weeks      0.0.0.0:8848->8848/tcp, :::8848->8848/tcp                                                                                                             nacos
fc16ae62dc0f   mysql:5.7                  "docker-entrypoint.s…"   7 weeks ago      Up 7 weeks      33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp                                                                                                  mysql
a7dbf979b1e6   rabbitmq:3.8-management    "docker-entrypoint.s…"   7 weeks ago      Up 7 weeks      4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp   mq
[root@VM-8-17-centos cyk]# 
[root@VM-8-17-centos cyk]# docker cp e8:/usr/local/tomcat/RUNNING.txt /root/cyk/
Successfully copied 17.9kB to /root/cyk/
[root@VM-8-17-centos cyk]# ls
RUNNING.txt

Ps:这里的 e8 是 tomcat_1?容器 id 的简写,只要能区分开,怎么简写都可以

b)将宿主机中指定文件拷贝到容器内:docker cp 宿主机目录?容器id:容器中文件或目录

如何部署一个 web 项目(war 包项目)

a)将 xxx.war 上传到 linux 服务器

b)通过 docker cp 命令将 xxx.war 赋值到 tomcat 的 webapp 目录中即可.

c)使用 ip 端口进行测试访问即可.

查看容器内运行的进程信息

docker top 容器id | name

a)查看 tomcat_1 容器内运行的进程信息

[root@VM-8-17-centos cyk]# docker top tomcat_1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                32112               32092               0                   20:49               ?                   00:00:05            /docker-java-home/jre/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 -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

查看容器内部细节

docker inspect 容器id | name

a)例如查看 tomcat_1 容器内部细节?

[root@VM-8-17-centos cyk]# docker inspect tomcat_1
[
    {
        "Id": "e823da882accc99496e297da6cbf0004e3b1033304f65d6a2d3e538a36d53665",
        "Created": "2023-12-23T12:49:58.845311366Z",
        "Path": "catalina.sh",
        "Args": [
            "run"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 32112,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-12-23T12:49:59.234928823Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ef6a7c98d192507d6066dcf24e44bec66d07ec9cf7c55d8d3d1ea0a24660bdef",
        "ResolvConfPath": "/var/lib/docker/containers/e823da882accc99496e297da6cbf0004e3b1033304f65d6a2d3e538a36d53665/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/e823da882accc99496e297da6cbf0004e3b1033304f65d6a2d3e538a36d53665/hostname",
        "HostsPath": "/var/lib/docker/containers/e823da882accc99496e297da6cbf0004e3b1033304f65d6a2d3e538a36d53665/hosts",
        "LogPath": "/var/lib/docker/containers/e823da882accc99496e297da6cbf0004e3b1033304f65d6a2d3e538a36d53665/e823da882accc99496e297da6cbf0004e3b1033304f65d6a2d3e538a36d53665-json.log",
        "Name": "/tomcat_1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "8080/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8082"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                36,
                171
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware",
                "/sys/devices/virtual/powercap"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/1807f8589086eb51f861c4938ddbd1da9395c48ef3b1bc40ded35195707c07db-init/diff:/var/lib/docker/overlay2/4272a408897873c3e991cc410cc9f3191dc2ebb5a8f940e386746a8b62e45b77/diff:/var/lib/docker/overlay2/aefe736cf7883ec152540cf245eebf8d1daee7ebd7752db0f2aed440db9c436e/diff:/var/lib/docker/overlay2/eab8218d1796d5eaa07da26ded1c78c01b0d7dfaf96654c7223f94fa022804ca/diff:/var/lib/docker/overlay2/6a1ae79728973f2f3dde69c44b8bf36c7e3e8dee18a67777f2c18298c27b72e3/diff:/var/lib/docker/overlay2/4284addc9df80d1ca1f05dddd850dd749c9cc55c34f28b0876d5eaacc2d576e5/diff:/var/lib/docker/overlay2/ed77212064855a9fee7e0403c15a23667de8c08fe7d59635f328c1b91e8e0af1/diff:/var/lib/docker/overlay2/1e0a0cebf48bf83030ebecda040939bc9eb11094883fa716bde5789cfc8d5ed1/diff:/var/lib/docker/overlay2/648a8998071f0296333f194e322e6785fb01b763e684543c6d665eb74965567b/diff:/var/lib/docker/overlay2/96aaa73c0076e21994ad88907d5f425ebf630fab7e091eb301073b4429c2f609/diff:/var/lib/docker/overlay2/6315239813db62569e9e729bbf20f7d6220df8a170fbd4fae2e1eba015e4f51d/diff:/var/lib/docker/overlay2/6951fbc42d9b579b84c403fdb31421d105aac328ad81e2e3e1d55a11556afcf7/diff",
                "MergedDir": "/var/lib/docker/overlay2/1807f8589086eb51f861c4938ddbd1da9395c48ef3b1bc40ded35195707c07db/merged",
                "UpperDir": "/var/lib/docker/overlay2/1807f8589086eb51f861c4938ddbd1da9395c48ef3b1bc40ded35195707c07db/diff",
                "WorkDir": "/var/lib/docker/overlay2/1807f8589086eb51f861c4938ddbd1da9395c48ef3b1bc40ded35195707c07db/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "e823da882acc",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8080/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "JAVA_HOME=/docker-java-home/jre",
                "JAVA_VERSION=7u181",
                "JAVA_DEBIAN_VERSION=7u181-2.6.14-1~deb8u1",
                "CATALINA_HOME=/usr/local/tomcat",
                "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
                "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
                "OPENSSL_VERSION=1.1.0f-3+deb9u2",
                "GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23",
                "TOMCAT_MAJOR=8",
                "TOMCAT_VERSION=8.0.53",
                "TOMCAT_SHA512=cd8a4e48a629a2f2bb4ce6b101ebcce41da52b506064396ec1b2915c0b0d8d82123091242f2929a649bcd8b65ecf6cd1ab9c7d90ac0e261821097ab6fbe22df9",
                "TOMCAT_TGZ_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz \thttps://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz \thttps://www.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz \thttps://archive.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz",
                "TOMCAT_ASC_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz.asc \thttps://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz.asc \thttps://www.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz.asc \thttps://archive.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz.asc"
            ],
            "Cmd": [
                "catalina.sh",
                "run"
            ],
            "Image": "tomcat:8.0",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "7c5c3740d5acbd7748c07d0203d0833f72ebb617c7b3d0e76a3e6d8a2510e062",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8080/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8082"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "8082"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/7c5c3740d5ac",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "2294463d89e7a91ee32cbd717403ceb9d1079fc23fca9cd251c003fa019bff0c",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.6",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:06",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "3be1ce6eea6f4f139c964c6afa94dd46fa60576e291edbc56d730c3c977b018c",
                    "EndpointID": "2294463d89e7a91ee32cbd717403ceb9d1079fc23fca9cd251c003fa019bff0c",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.6",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:06",
                    "DriverOpts": null
                }
            }
        }
    }
]

容器数据卷机制

作用

a)主要作用:

用来实现容器中数据和宿主机中数据进行映射的(这里可以类比 Vue 中的双向绑定,修改任意一方,另一方都会跟着修改)

b)使用场景:

  1. 易修改:即使我们不进入容器的内部,也可以通过修改宿主机文件系统的 conf 文件,来修改容器中的 conf 文件;
  2. 数据可复用:当创建了新的容器并且需要复用之前的 conf 文件时,可以直接再创建一个数据卷或者修改之前数据卷对容器目录的指向即可,来进行关联,这样就可以将之前的配置映射到新容器的配置中;
  3. 升级维护简单:如果来了新的容器,需要删除旧容器,也不用担心数据会被删除掉,因为数据在宿主机上还保存了一份。
    ?

注意

数据卷只能在容器首次启动时设置.

使用

容器首次启动时指定 -v 参数

a)使用绝对路径设置数据卷

docker run -v 宿主机绝对路径:容器内绝对路径:ro .....(其他设置)

ro:表示 ready only,也就是只读的意思,如果在设置数据卷时指定 ro,代表日后从容器内访问该路径时是只读的.

Ps:这种方式会将容器该路径下的原始内容全部清空,始终以宿主机路径为主.

b)使用别名方式设置数据卷

docker run -v aa:/usr/local/tomcat/webapps ......(其他配置)

aa:

  1. aa 代表 docker 数据卷中别名. (如果存在就直接使用,不存在就创建)
  2. 使用别名方式保留容器路径原始内容,前提是别名对应路径不能存在内容.

Ps:docker 根据别名创建目录默认在 /var/lib/docker/volumes/ 中.

通过 find 命令也可以查找到该路径

?

容器打包镜像

背景

a)回顾三个核心概念

仓库 repository:docker 官方维护的一个仓库,用来集中存储镜像.

镜像 image:一个镜像代表一个软件服务,特点就是 "只读".

容器 container:镜像每运行一次形成一个容器,运行的容器代表一个正在运行的软件服务.

b)为什么要将容器打包成一个新的镜像?

容器可读可写,那么基于这个特性就可以对容器进行自己的深度定制.

将容器打包成一个新的镜像,日后运行的这个容器就存在原始定制的特性.

c)原理图

将容器打包成一个新的镜像

docker commit -m "对镜像的描述" -a "作者名称" 容器id | name  镜像名:版本

如下案例

[root@VM-8-17-centos cyk]# docker ps -a
CONTAINER ID   IMAGE                      COMMAND                  CREATED             STATUS                           PORTS                                                                                                                                                 NAMES
e823da882acc   tomcat:8.0                 "catalina.sh run"        About an hour ago   Up About an hour                 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp                                                                                                             tomcat_1
07a1f69b4c4c   tomcat:8.0                 "catalina.sh run"        About an hour ago   Exited (137) About an hour ago                                                                                                                                                         objective_taussig
abc6284fe34d   redis:5.0.9                "docker-entrypoint.s…"   5 weeks ago         Up 5 weeks                       0.0.0.0:6380->6379/tcp, :::6380->6379/tcp                                                                                                             redis
d442ff7829df   nacos/nacos-server:1.4.2   "bin/docker-startup.…"   6 weeks ago         Up 5 weeks                       0.0.0.0:8848->8848/tcp, :::8848->8848/tcp                                                                                                             nacos
fc16ae62dc0f   mysql:5.7                  "docker-entrypoint.s…"   7 weeks ago         Up 7 weeks                       33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp                                                                                                  mysql
a7dbf979b1e6   rabbitmq:3.8-management    "docker-entrypoint.s…"   7 weeks ago         Up 7 weeks                       4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp   mq
[root@VM-8-17-centos cyk]# 
[root@VM-8-17-centos cyk]# 
[root@VM-8-17-centos cyk]# 
[root@VM-8-17-centos cyk]# docker commit -m "new test tomcat" -a "cyk" e8 tomcat_cyk:8.1
sha256:9b9add65a1f1b2a9331bac5f0c047af6a921e0850b247638256843a770b4ed6d
[root@VM-8-17-centos cyk]# docker images
REPOSITORY                  TAG              IMAGE ID       CREATED          SIZE
tomcat_cyk                  8.1              9b9add65a1f1   10 seconds ago   356MB
redis                       5                c5da061a611a   2 years ago      110MB
mysql                       5.7              c20987f18b13   2 years ago      448MB
rabbitmq                    3.8-management   699038cb2b96   2 years ago      250MB
nacos/nacos-server          1.4.2            938169b118c7   2 years ago      956MB
redis                       5.0.9            987b553c835f   3 years ago      98.3MB
bladex/sentinel-dashboard   1.8.0            61c10eaefbcf   3 years ago      147MB
nacos/nacos-server          1.2.0            763941e566bb   3 years ago      732MB
bladex/sentinel-dashboard   1.7.1            d12e18546bfd   3 years ago      148MB
tomcat                      8.0              ef6a7c98d192   5 years ago      356MB

镜像的备份和恢复

a)备份镜像: docker save 镜像名:tag -o (镜像名-tag).tar

b)恢复镜像:docker load -i xxx.tar

例如 docker load -i tomcat-8.0.tar?

文章来源:https://blog.csdn.net/CYK_byte/article/details/135172406
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。