docker 基本使用

centos7 安装docker

1.首先将系统自带的docker全部删除
sudo yum remove  docker \
								docker-client \
								docker-client-lastest \
								docker-common \
								docker-latest \
								docker-latest-logrotate \
								docker-selinux \
								docker-engine-selinux \
								docker-engine
2. 安装依赖包
sudo yum install -y yum-utils \
    device-mapper-persistent-data \
    lvm2
3.设置stable镜像仓库
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
4.查看ce版docker版本
sudo yum list docker-ce --showduplicates | sort -r
5.安装docker
	*不指定版本安装最新版
	sudo yum install docker-ce
	*安装指定版本
	sudo yum install  docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io 

6.重启docker
systemctl restart docker

配置docker加速

1.查看下docer版本
docker -v 或者 docker --version
2.参考链接
[link](http://guide.daocloud.io/dcs/daocloud-9153151.html).
3.镜像源
Docker国内镜像:
网易加速器:http://hub-mirror.c.163.com
官方中国加速器:https://registry.docker-cn.com
ustc的镜像:https://docker.mirrors.ustc.edu.cn
也可使用私人镜像加速器地址,如使用阿里云的镜像加速器:登录阿里云->产品->搜索"容器镜像服务"->镜像库->镜像加速器。
4.自己的配置
首先在/etc/docker/daemon.json
{ "registry-mirrors": [
 			 "https://docker.mirrors.ustc.edu.cn",
			  "https://hub-mirror.c.163.com",
  			"https://mirror.ccs.tencentyun.com",
 			 "https://registry.docker-cn.com"
 			  ]
  }
  5.重启
  sudo systemctl daemon-reload # 重启加速配置文件
  sudo systemctl restart docker # 重启docker

docker基本命令行

镜像命令

docker images == 》查看所有镜像

[root@VM-8-2-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   5 months ago   13.3kB

# 解释
REPOSITORY  #镜像名称
TAG         #镜像标签,类似于版本号,latest 代表最新版
IMAGE ID    #镜像标识
CREATED     # 创建日期
SIZE        # 镜像大小
# 可选参数
-a          #显示所有镜像
[root@VM-8-2-centos ~]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   5 months ago   13.3kB
-q          #仅显示镜像id
[root@VM-8-2-centos ~]# docker images -q
feb5d9fea6a5
-aq      # 显示所有镜像id
[root@VM-8-2-centos ~]# docker images -aq
feb5d9fea6a5

docker search 镜像名 ==》镜像搜索

[root@VM-8-2-centos ~]# docker search mysql
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql/mysql-server               Optimized MySQL Server Docker images. Create…   907                  [OK]
centos/mysql-57-centos7          MySQL 5.7 SQL database server                   92                   
mysql/mysql-cluster              Experimental MySQL Cluster Docker images. Cr…   92                   
bitnami/mysql                    Bitnami MySQL Docker Image                      65                   [OK]
circleci/mysql                   MySQL is a widely used, open-source relation…   25                   
#  如果搜索的东西不存在
[root@VM-8-2-centos ~]# docker search mysqlssasasasasa
NAME      DESCRIPTION   STARS     OFFICIAL   AUTOMATED
# 可选参数
[root@VM-8-2-centos ~]# docker search --help
Usage:  docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
# 加入搜索 stars超过900的 mysql:docker search --filter=starts=900
[root@VM-8-2-centos ~]# docker search --filter=stars=900 mysql
NAME                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql/mysql-server   Optimized MySQL Server Docker images. Create…   907                  [OK]

docker pull 镜像名 [:tag] ===》 镜像的获取/拉去

# 不指定版本拉取镜像
[root@VM-8-2-centos ~]# docker pull mysql
Using default tag: latest               # 如果不指定版本号,默认下载最新的	
latest: Pulling from library/mysql
72a69066d2fe: Pull complete             # 分层下载,docker images核心,联合文件系统
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709  # 签名
Status: Downloaded newer image for mysql:latest 
docker.io/library/mysql:latest        #真实的下载地址

#
docker pull mysql == docker pull   docker.io/library/mysql:latest

# 指定版本拉取镜像
[root@VM-8-2-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists   #  上边一步 拉mysql最新版时,已经存在的文件不再下载 
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi 镜像id ===>镜像的删除

# 删除mysql5.7(仅仅删除mysql5.7独有的文件,mysql最新版与mysql5.7的共有文件没有删除)
[root@VM-8-2-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   2 months ago   448MB
mysql         latest    3218b38490ce   2 months ago   516MB
hello-world   latest    feb5d9fea6a5   5 months ago   13.3kB
[root@VM-8-2-centos ~]# docker rmi c20987f18b13
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92

# 可选参数
[root@VM-8-2-centos ~]# docker rmi --help
Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents
[root@VM-8-2-centos ~]# 
# 具体例子
docker rmi  -f 镜像id                  #删除单个镜像  
docker rmi  -f 镜像id  镜像id          #删除多个镜像
docker rmi  -f $(docker images -aq)   #删除全部镜像

容器相关命令

docker run 容器的运行

docker run [可选参数] image
-- name = "Name"          # 给容器起个名字  用来区分容器
-d                        # 以后台运行的方式运行
-it                       # 使用交互方式运行,进入容器内查看内容
-p                        # 指定容器端口运行 -p 8080:8080 外部8080端口映射到内网8080端口
						  -p ip:主机端口:容器端口
					   	  -p  主机端口:容器端口
						  -p 容器端口 
-P                        # 随机指定端口 
#############################################################################
# 测试进入容器
[root@VM-8-2-centos ~]# docker run -it centos    /bin/bash  
[root@bb6af093b8a1 /]# ls                      #查看容器内的centos,基础版本,很多命令不完善 
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# 从容器中退回到主机
[root@bb6af093b8a1 /]# exit      
exit
[root@VM-8-2-centos ~]# ls
env  nginx-1.10.1.tar.gz  Python-3.7.4  Python-3.7.4.tar.xz  redis-5.0.7  redis-5.0.7.tar.gz
[root@VM-8-2-centos ~]# 

docker ps 查看正在运行的容器

# 参数解释
无参数                    # 列出正在运行的容器
-a                       # 列出正在运行的容器+历史运行过的容器
-n = ?                   # 显示最近创建的容器
-q                       # 只显示容器的编号
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
aeeb9ee9a8da   centos    "/bin/bash"   About a minute ago   Up About a minute             fervent_taussig
[root@VM-8-2-centos ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED              STATUS                          PORTS     NAMES
778032a21ba4   hello-world   "/hello"      About a minute ago   Exited (0) About a minute ago             jovial_snyder
aeeb9ee9a8da   centos        "/bin/bash"   About a minute ago   Up About a minute                         fervent_taussig
bcf797fdf60d   hello-world   "/hello"      2 minutes ago        Exited (0) 2 minutes ago                  suspicious_jennings
bb6af093b8a1   centos        "/bin/bash"   17 minutes ago       Exited (0) 17 minutes ago                 modest_heyrovsky
2a65272780bb   hello-world   "/hello"      23 minutes ago       Exited (0) 23 minutes ago                 vigorous_chatterjee
fe5d6df615ad   hello-world   "/hello"      2 hours ago          Exited (0) 2 hours ago                    naughty_snyder
9812abe29590   hello-world   "/hello"      8 hours ago          Exited (0) 8 hours ago                    funny_aryabhata
[root@VM-8-2-centos ~]# docker ps -q
aeeb9ee9a8da
[root@VM-8-2-centos ~]# docker ps -aq
778032a21ba4
aeeb9ee9a8da
bcf797fdf60d
bb6af093b8a1
2a65272780bb
fe5d6df615ad
9812abe29590
[root@VM-8-2-centos ~]# docker ps -n=1
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
778032a21ba4   hello-world   "/hello"   2 minutes ago   Exited (0) 2 minutes ago             jovial_snyder
[root@VM-8-2-centos ~]# docker ps -n=1 -q
778032a21ba4

容器的退出

# 容器停止并退出  exit
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos ~]# docker run -it centos /bin/bash
[root@59ab437d6042 /]# exit
exit
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos ~]# docker run -it centos /bin/bash
[root@1d530471fd8e /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# 容器不停止退出   ctrl+p+q
[root@1d530471fd8e /]
[root@VM-8-2-centos ~]# ls
env  nginx-1.10.1.tar.gz  Python-3.7.4  Python-3.7.4.tar.xz  redis-5.0.7  redis-5.0.7.tar.gz
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
1d530471fd8e   centos    "/bin/bash"   27 seconds ago   Up 25 seconds             unruffled_kalam

容器的删除

docker rm  容器id               # 删除指定容器,正在运行的不能删除,可以使用docker rm -f 容器id 强制删除
docker rm -f $(docker ps -aq)  # 删除所有容器
docker ps -a -q | xargs docker rm  # 删除所有容器  
####### 实际操作
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
1d530471fd8e   centos    "/bin/bash"   6 minutes ago   Up 6 minutes             unruffled_kalam
[root@VM-8-2-centos ~]# docker rm 1d530471fd8e
Error response from daemon: You cannot remove a running container 1d530471fd8efea808c60b3da5e2ac713af7a8f2f86c4de9cc91a8fc98edbc19. Stop the container before attempting removal or force remove
[root@VM-8-2-centos ~]# docker rm -f  1d530471fd8e
1d530471fd8e
[root@VM-8-2-centos ~]# 

容器的其他操作

docker start 容器id              # 容器的启动
docker stop 容器id               # 容器的停止
docker restart 容器id            # 容器的重启 
docker  kill  容器id             # 容器的杀死
################################################
[root@VM-8-2-centos ~]# clear
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos ~]# docker run -it centos /bin/bash
[root@f329f8203dd4 /]# exit
[root@VM-8-2-centos ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED              STATUS                          PORTS     NAMES
f329f8203dd4   centos        "/bin/bash"   10 seconds ago       Exited (0) 5 seconds ago                  charming_ramanujan
940f969423c5   centos        "/bin/bash"   About a minute ago   Exited (0) 34 seconds ago                 brave_mirzakhani
afb63129bb24   centos        "/bin/bash"   About a minute ago   Exited (0) About a minute ago             peaceful_hamilton
746e13fb68e7   centos        "/bin/bash"   2 minutes ago        Exited (0) 2 minutes ago                  serene_jepsen
59ab437d6042   centos        "/bin/bash"   12 minutes ago       Exited (0) 12 minutes ago                 trusting_kowalevski
778032a21ba4   hello-world   "/hello"      23 minutes ago       Exited (0) 23 minutes ago                 jovial_snyder
aeeb9ee9a8da   centos        "/bin/bash"   23 minutes ago       Exited (0) 13 minutes ago                 fervent_taussig
bcf797fdf60d   hello-world   "/hello"      24 minutes ago       Exited (0) 24 minutes ago                 suspicious_jennings
bb6af093b8a1   centos        "/bin/bash"   39 minutes ago       Exited (0) 39 minutes ago                 modest_heyrovsky
2a65272780bb   hello-world   "/hello"      44 minutes ago       Exited (0) 44 minutes ago                 vigorous_chatterjee
fe5d6df615ad   hello-world   "/hello"      3 hours ago          Exited (0) 3 hours ago                    naughty_snyder
9812abe29590   hello-world   "/hello"      8 hours ago          Exited (0) 8 hours ago                    funny_aryabhata
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos ~]# docker start f329f8203dd4
f329f8203dd4
[root@VM-8-2-centos ~]# 
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
f329f8203dd4   centos    "/bin/bash"   48 seconds ago   Up 4 seconds             charming_ramanujan
[root@VM-8-2-centos ~]# docker stop f329f8203dd4
f329f8203dd4
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

常用其他命令

后台运行容器

docker run -d 镜像名
#############################################
# 查看正在运行的容器
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos ~]# docker run centos
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
# 以后台方式运行docker
[root@VM-8-2-centos ~]# docker run -d centos
f10471cc2d912170b361245e49448f30b2dd16e6d422bcbca8e6511851b78d40
# 查看正在运行的容器
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
########################################################################
问题:docker以后台方式运行centos 发现centos 停止了
########################################################################
 常见的坑:docker 容器使用后台运行,就此需要有一个前台进程,否则docker会主动停止容器
 # nginx ,容器启动后,发现自己没有提供服务,就会立刻停止,不再提供服务

查看日志

docker logs 容器id
# 可选参数
-f           #持续跟踪日志输出
-t           #显示时间戳
--tail   数字  #显示最新的N条日志 	
###########################################################################################################
[root@VM-8-2-centos ~]# docker run -it centos  /bin/bash  -c "while true;do echo haoenyuan;sleep 10;done"
haoenyuan
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
752f6585a05e   centos    "/bin/bash -c 'while…"   8 seconds ago   Up 6 seconds             lucid_burnell
# 仅显示前10条
[root@VM-8-2-centos ~]# docker logs -t --tail 10 752f6585a05e
2022-02-24T14:40:38.257125999Z haoenyuan
2022-02-24T14:40:48.293835075Z haoenyuan
2022-02-24T14:40:58.302944621Z haoenyuan
2022-02-24T14:41:08.312763510Z haoenyuan
# 持续输出最新的10条日志
[root@VM-8-2-centos ~]# docker logs -tf --tail 10 752f6585a05e
2022-02-24T14:40:38.257125999Z haoenyuan
2022-02-24T14:40:48.293835075Z haoenyuan
2022-02-24T14:40:58.302944621Z haoenyuan
2022-02-24T14:41:08.312763510Z haoenyuan
2022-02-24T14:41:18.324085673Z haoenyuan
2022-02-24T14:41:28.333793952Z haoenyuan
2022-02-24T14:41:38.343830522Z haoenyuan
2022-02-24T14:41:48.347035664Z haoenyuan
2022-02-24T14:41:58.351824176Z haoenyuan

查看容器的进程信息

docker top  容器id
#########################################################################
#pid 进程id  ppid 父进程id
[root@VM-8-2-centos ~]# docker top ea77bf775e37
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                18397               18378               0                   22:45               pts/0               00:00:00            /bin/bash -c while true;do echo haoenyuan;sleep 10;done
root                18865               18397               0                   22:47               pts/0               00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 10

查看镜像的元数据

docker inspect 镜像id
[root@VM-8-2-centos ~]# docker inspect ea77bf775e37
[
    {
        "Id": "ea77bf775e37c56301db70e3b273be9012d953785e112ecf1b196a56e3d5ae6f",
        "Created": "2022-02-24T14:45:09.10062462Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo haoenyuan;sleep 10;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 18397,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-02-24T14:45:09.851358728Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/ea77bf775e37c56301db70e3b273be9012d953785e112ecf1b196a56e3d5ae6f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/ea77bf775e37c56301db70e3b273be9012d953785e112ecf1b196a56e3d5ae6f/hostname",
        "HostsPath": "/var/lib/docker/containers/ea77bf775e37c56301db70e3b273be9012d953785e112ecf1b196a56e3d5ae6f/hosts",
        "LogPath": "/var/lib/docker/containers/ea77bf775e37c56301db70e3b273be9012d953785e112ecf1b196a56e3d5ae6f/ea77bf775e37c56301db70e3b273be9012d953785e112ecf1b196a56e3d5ae6f-json.log",
        "Name": "/loving_maxwell",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "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",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "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"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/0847f4e148bf931188e86215fc02359347963e3e689693b520cd3d5de1d9ef8e-init/diff:/var/lib/docker/overlay2/2717dfb98971d3cc0c0f60d66dd945e55d837a1b928a7df0c298c51e2b1cbc76/diff",
                "MergedDir": "/var/lib/docker/overlay2/0847f4e148bf931188e86215fc02359347963e3e689693b520cd3d5de1d9ef8e/merged",
                "UpperDir": "/var/lib/docker/overlay2/0847f4e148bf931188e86215fc02359347963e3e689693b520cd3d5de1d9ef8e/diff",
                "WorkDir": "/var/lib/docker/overlay2/0847f4e148bf931188e86215fc02359347963e3e689693b520cd3d5de1d9ef8e/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "ea77bf775e37",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash",
                "-c",
                "while true;do echo haoenyuan;sleep 10;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "d5c2e8bd3586dd2fba72adf89dd260d908efc12bc17d8d1462c6d26182063679",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/d5c2e8bd3586",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "88c5fee63281f0b410a55c68a14aed0866e2f318b1dd4f9eae5a6724e3f1702e",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "7a56128af29fe654f2c917b2a32fd670cd4a2f1ae8089b1c0579deca4032c03a",
                    "EndpointID": "88c5fee63281f0b410a55c68a14aed0866e2f318b1dd4f9eae5a6724e3f1702e",
                    "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 exec -it 容器id   bashshell(/bin/bash)
######################################################################
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos ~]# docker run -it centos /bin/bash -c "while true;do echo haoenyuan;sleep 10'done"
/bin/bash: -c: line 0: unexpected EOF while looking for matching `''
/bin/bash: -c: line 1: syntax error: unexpected end of file
[root@VM-8-2-centos ~]# docker run -it centos /bin/bash -c "while true;do echo haoenyuan;sleep 10;done"
haoenyuan
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
d380548fb558   centos    "/bin/bash -c 'while…"   7 seconds ago   Up 6 seconds             relaxed_mendel
# 执行完此命令 会新生成一个终端,与原来运行的那个终端不会冲突
[root@VM-8-2-centos ~]# docker exec -it d380548fb558  /bin/bash
[root@d380548fb558 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# 此处执行exit,容器并不会停止,因为此处退出且停止的是刚刚新生成的终端
[root@d380548fb558 /]# exit
exit
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
d380548fb558   centos    "/bin/bash -c 'while…"   37 seconds ago   Up 36 seconds             relaxed_mendel
[root@VM-8-2-centos ~]# docker stop d380548fb558
d380548fb558
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos ~]# docker run -it centos /bin/bash
[root@4210b4b8359b /]# exit
exit
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
####################################################################
# 方式二 : docker attach  容器id
[root@VM-8-2-centos ~]# docker run -it centos /bin/bash -c "while true;do echo haoenyuan;sleep 10;done"
haoenyuan
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
cf1b740cf37c   centos    "/bin/bash -c 'while…"   7 seconds ago   Up 6 seconds             intelligent_hawking
# 会进入正在运行的容器中,不会新生成一个终端
[root@VM-8-2-centos ~]# docker attach cf1b740cf37c
haoenyuan
###############################################################
区分:
docker exec      # 进如容器后会新生成一个终端,可以在里边操作(常用)
docker attach    # 进入容器正在运行的终端,不会启动新进程!

从容器内拷贝文件到主机上

docker cp 容器id:容器目录  主机目录
##########################################################################
[root@VM-8-2-centos ~]# cd /home/
[root@VM-8-2-centos home]# ls
lighthouse
# 进入到
[root@VM-8-2-centos home]# docker run -it centos /bin/bash
[root@e134fd0cd5d9 /]# cd /home/
[root@e134fd0cd5d9 home]# ls
# 在容器内部创建一个文件
[root@e134fd0cd5d9 home]# touch test.py
[root@e134fd0cd5d9 home]# ls
test.py
[root@e134fd0cd5d9 home]# exit
exit
[root@VM-8-2-centos home]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos home]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
e134fd0cd5d9   centos    "/bin/bash"   50 seconds ago   Exited (0) 13 seconds ago             pedantic_brattain
# 将这个文件拷贝到主机上
[root@VM-8-2-centos home]# docker cp e134fd0cd5d9:/home/test.py /home/test.py
[root@VM-8-2-centos home]# ls
lighthouse  test.py
# 拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现自动同步
# 文件的拷贝于容器运行没运行没有关系,只要容器存在,即时停止了也可以进行文件的拷贝

一些安装部署示例

安装部署nginx

# 查看存在的镜像
[root@VM-8-2-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    605c77e624dd   8 weeks ago    141MB
tomcat        9.0       b8e65a4d736d   2 months ago   680MB
tomcat        latest    fb5657adc892   2 months ago   680MB
hello-world   latest    feb5d9fea6a5   5 months ago   13.3kB
centos        latest    5d0da3dc9764   5 months ago   231MB
# 运行nginx     -d 后台方式运行   --name 起别名 nginx01 -p 主机端口3344映射到nginx 80端口 
[root@VM-8-2-centos ~]# docker run -d --name nginx01  -p 3344:80 nginx 
7d796cae36f9c9a151046700812d7bb33e1de21e0c882255bc557e2178f8736a
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
7d796cae36f9   nginx     "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   0.0.0.0:3344->80/tcp, :::3344->80/tcp   nginx01

如果出现下图所示则表示运行成功(记得去控制台放开3344端口)

安装部署tomcat

# 首先获取tomcat
docker pull tomcat
[root@VM-8-2-centos ~]# docker run -d --name tomcat01 -p 3344:8080 tomcat
828ea9ea681e79e4edd2aeb32e84194c8c499e88e9cbad1a0fccb6a230088a31
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
828ea9ea681e   tomcat    "catalina.sh run"   19 seconds ago   Up 18 seconds   0.0.0.0:3344->8080/tcp, :::3344->8080/tcp   tomcat01

发现tomcat404
在这里插入图片描述
原因是:docker 获取的镜像都是最简版,都是减配的。
解决方法

[root@VM-8-2-centos ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS                                       NAMES
828ea9ea681e   tomcat    "catalina.sh run"   2 minutes ago   Up 2 minutes   0.0.0.0:3344->8080/tcp, :::3344->8080/tcp   tomcat01
[root@VM-8-2-centos ~]# docker exec -it 828ea9ea681e /bin/bash
root@828ea9ea681e:/usr/local/tomcat# ls
BUILDING.txt  CONTRIBUTING.md  LICENSE	NOTICE	README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
root@828ea9ea681e:/usr/local/tomcat# cd webapps
root@828ea9ea681e:/usr/local/tomcat/webapps# ls
root@828ea9ea681e:/usr/local/tomcat/webapps# cd ../webapps.dist/
root@828ea9ea681e:/usr/local/tomcat/webapps.dist# ls
ROOT  docs  examples  host-manager  manager
root@828ea9ea681e:/usr/local/tomcat/webapps.dist# cd ..
root@828ea9ea681e:/usr/local/tomcat# cp -r webapps.dist/* webapps/
root@828ea9ea681e:/usr/local/tomcat# exit
exit
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS                                       NAMES
828ea9ea681e   tomcat    "catalina.sh run"   4 minutes ago   Up 4 minutes   0.0.0.0:3344->8080/tcp, :::3344->8080/tcp   tomcat01

在这里插入图片描述

安装部署ES

# 拉一下最新的es
[root@VM-8-2-centos ~]# docker pull elasticsearch
Using default tag: latest
latest: Pulling from library/elasticsearch
05d1a5232b46: Pull complete 
5cee356eda6b: Pull complete 
89d3385f0fd3: Pull complete 
65dd87f6620b: Pull complete 
78a183a01190: Pull complete 
1a4499c85f97: Pull complete 
2c9d39b4bfc1: Pull complete 
1b1cec2222c9: Pull complete 
59ff4ce9df68: Pull complete 
1976bc3ee432: Pull complete 
5af49e8af381: Pull complete 
42c8b75ff7af: Pull complete 
7e6902915254: Pull complete 
99853874fa54: Pull complete 
596fbad6fcff: Pull complete 
Digest: sha256:a8081d995ef3443dc6d077093172a5931e02cdb8ffddbf05c67e01d348a9770e
Status: Downloaded newer image for elasticsearch:latest
docker.io/library/elasticsearch:latest
You have new mail in /var/spool/mail/root
# 运行es
[root@VM-8-2-centos ~]# docker run -d --name elasticsearch  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch
2f2691210632a5d2876a994ade43450d1d41e21741fee6e113ccf65dc2242ee5
# 发现没有运行起来
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-2-centos ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                     PORTS     NAMES
2f2691210632   elasticsearch   "/docker-entrypoint.…"   10 seconds ago   Exited (1) 6 seconds ago             elasticsearch
# 看一下日志
[root@VM-8-2-centos ~]# docker logs -tf 2f2691210632
2022-02-28T14:30:23.166600357Z OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
2022-02-28T14:30:23.166641036Z #
2022-02-28T14:30:23.166644889Z # There is insufficient memory for the Java Runtime Environment to continue.
2022-02-28T14:30:23.166647547Z # Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
2022-02-28T14:30:23.166650257Z # An error report file with more information is saved as:
2022-02-28T14:30:23.166652772Z # /tmp/hs_err_pid1.log
##########################################################
解决办法 限制内存
	[root@VM-8-2-centos ~]# docker run -d --name elasticsearch  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m"  elasticsearch
e5067cf92ec1a39166888097006b1a81f7e4628cf892b4b4e64c896e94f899f9
[root@VM-8-2-centos ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED         STATUS         PORTS                                                                               NAMES
e5067cf92ec1   elasticsearch   "/docker-entrypoint.…"   4 seconds ago   Up 3 seconds   9200/tcp, 0.0.0.0:9002-9003->9002-9003/tcp, :::9002-9003->9002-9003/tcp, 9300/tcp   elasticsearch
[root@VM-8-2-centos ~]# docker stats

CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O          BLOCK I/O        PIDS
e5067cf92ec1   elasticsearch   0.00%     225.9MiB / 1.795GiB   12.29%    2.72kB / 1.7kB   31.9MB / 156kB   33
################################################################

在

#安装部署es发现的问题
1.暴露的端口多,不安全
2.运行的时候常因为内存不够起不来服务,等价于es十分耗内存
3.  es的数据一般需要放置到安全目录! 挂载
问题:kibana 怎么连接es(会了就懂了es的网络)	

在这里插入图片描述

可视化

docker图形化界面管理工具!提供一个后台面板供我们操作

portainer

docker pull  portainer/portainer
docker run -d -p 8088:9000  --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

在这里插入图片描述

Rancher(CI/CD 再用)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值