随笔——Docker学习笔记

一、Docker 安装

yum install -y yum-utils device-mapper-persistent=data lvm2
# -y 自动确认安装
# yum-utils yum工具包,可以简化安装
# device-mapper-persistent=data,lvm2  是两个数据存储驱动包,docker中进行数据存储需要依赖这两个包
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# yum-config-manager yum-utils提供的简化工具包,用于修改安装源
# --add-repo   设置新的安装源
# http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  阿里云安装源
yum makecache fast
# 缓存下载速度最快的数据源
yum -y install docker-ce
# docker-ce 安装免费的社区版docker
# docker-ee 收费的企业版
# 安装过程中会自动安装客户端和服务器
service docker start #启动docker服务
docker version  #查看docker版本,验证是否启动,docker是C/S服务,有一个客户端一个服务器
docker pull hello-world #从国外远程中央仓库中拉取hello-world镜像,下载到本地
docker run hello-world #启动运行镜像

[root@VM-0-15-centos ~]# docker run hello-world

Hello from Docker!   #表示启动成功
This message shows that your installation appears to be working correctly.

二、Docker常用命令

hub.docker.com #docker 远程仓库网址

docker pull 镜像名<:tags>  #从远程仓库中抽取镜像 ,tags :版本号,如果不指定会拉取最新的版本
docker images  #查看本地镜像
docker run 镜像名<::ags>  #创建容器,启动应用
	docker run -p 8000:8080 tomcat  #启动tomcat容器,-p 添加端口映射 宿主机:容器
	docker run -p 8888:8080 -d tomcat # -d 后台启动
docker ps #查询正在运行中的容器
docker ps -a #查看所有的容器
docker rm <-f> 容器id #删除容器,-f 代表强制删除
docker rmi <-f> 镜像名:<tags> #删除镜像
netstat -tulpn  # 查看应用端口

1、删除容器

oot@VM-0-15-centos ~]# docker ps   # 查看运行中的容器
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                    NAMES
213193490a95   tomcat    "catalina.sh run"   9 seconds ago    Up 7 seconds    8080/tcp                 competent_brown
69099af40517   tomcat    "catalina.sh run"   12 minutes ago   Up 12 minutes   0.0.0.0:8888->8080/tcp   vigilant_lichterman
[root@VM-0-15-centos ~]# docker rm 213193490a95 #直接删除运行中的容器会报错
Error response from daemon: You cannot remove a running container 213193490a95b1e753bd34d44694a987058c57d668c27adf6a2daec34e9cebec. Stop the container before attempting removal or force remove
[root@VM-0-15-centos ~]# docker rm -f 213193490a95  #第一种 ,强制删除运行中的容器
213193490a95
[root@VM-0-15-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                    NAMES
69099af40517   tomcat    "catalina.sh run"   14 minutes ago   Up 14 minutes   0.0.0.0:8888->8080/tcp   vigilant_lichterman
[root@VM-0-15-centos ~]# docker stop 69099af40517   #第二种, 先停止容器,再进行删除
69099af40517
[root@VM-0-15-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-0-15-centos ~]# docker ps -a      #查看已停止的容器
CONTAINER ID   IMAGE         COMMAND             CREATED          STATUS                        PORTS     NAMES
69099af40517   tomcat        "catalina.sh run"   15 minutes ago   Exited (143) 14 seconds ago             vigilant_lichterman
c813e68f37cf   tomcat        "catalina.sh run"   31 minutes ago   Exited (130) 15 minutes ago             cranky_snyder
7a32b4724222   tomcat        "catalina.sh run"   33 minutes ago   Exited (130) 32 minutes ago             stupefied_agnesi
3d645031e0e2   hello-world   "/hello"            20 hours ago     Exited (0) 20 hours ago                 serene_wilson
[root@VM-0-15-centos ~]# docker rm 69099af40517#删除容器
69099af40517
[root@VM-0-15-centos ~]# 

2、删除镜像

[root@VM-0-15-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS      NAMES
b882bc1fb5bd   tomcat    "catalina.sh run"   19 seconds ago   Up 17 seconds   8080/tcp   thirsty_haslett
[root@VM-0-15-centos ~]# docker images  #查看镜像
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
tomcat        latest    1722f5c2ea4e   3 days ago      668MB
hello-world   latest    bf756fb1ae65   13 months ago   13.3kB
[root@VM-0-15-centos ~]# docker rmi tomcat:latest  #删除镜像,如果有容器在运行会报错
Error response from daemon: conflict: unable to remove repository reference "tomcat:latest" (must force) - container b882bc1fb5bd is using its referenced image 1722f5c2ea4e
[root@VM-0-15-centos ~]# docker rmi -f tomcat:latest  #强制删除镜像,会删除镜像的reposttory和tag,当前镜像依旧存在,容器依然在运行
Untagged: tomcat:latest
Untagged: tomcat@sha256:e7bc48bd0b0c18db84e718769ac27c87f90f9ecb20b761700be0a016a7f840d2
[root@VM-0-15-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
<none>        <none>    1722f5c2ea4e   3 days ago      668MB
hello-world   latest    bf756fb1ae65   13 months ago   13.3kB
[root@VM-0-15-centos ~]# docker rmi -f 1722f5c2ea4e   #通过镜像id删除镜像,如果有容器在运行则报错
Error response from daemon: conflict: unable to delete 1722f5c2ea4e (cannot be forced) - image is being used by running container b882bc1fb5bd
[root@VM-0-15-centos ~]# docker ps
CONTAINER ID   IMAGE          COMMAND             CREATED         STATUS         PORTS      NAMES
b882bc1fb5bd   1722f5c2ea4e   "catalina.sh run"   3 minutes ago   Up 3 minutes   8080/tcp   thirsty_haslett
[root@VM-0-15-centos ~]# docker rm -f b882bc1fb5bd
b882bc1fb5bd
[root@VM-0-15-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-0-15-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
<none>        <none>    1722f5c2ea4e   3 days ago      668MB
hello-world   latest    bf756fb1ae65   13 months ago   13.3kB
[root@VM-0-15-centos ~]# docker rmi 1722f5c2ea4e   #通过镜像id删除镜像,如果已经停止的容器未删除,则报错
Error response from daemon: conflict: unable to delete 1722f5c2ea4e (must be forced) - image is being used by stopped container 7a32b4724222
[root@VM-0-15-centos ~]# docker ps -a   #查看停止运行的容器
CONTAINER ID   IMAGE          COMMAND             CREATED        STATUS                     PORTS     NAMES
7a32b4724222   1722f5c2ea4e   "catalina.sh run"   2 hours ago    Exited (130) 2 hours ago             stupefied_agnesi
3d645031e0e2   hello-world    "/hello"            21 hours ago   Exited (0) 21 hours ago              serene_wilson
[root@VM-0-15-centos ~]# docker rm 7a32b4724222
7a32b4724222
[root@VM-0-15-centos ~]# docker rmi 1722f5c2ea4e    # 删除所有关联容器后,镜像删除成功
Deleted: sha256:1722f5c2ea4ee2149d08e48602cd9a85ead7f1ac59abdc7c78483b479af4458e
Deleted: sha256:d0a8b19dc2a2536c5df5c8e1cbbde2a57adcd478917b758ceab5b340f6e9065e
Deleted: sha256:77dbafc053decc4bd85be7eb743ff80203f6cfcfae3fd70ffad5c5553a7c7073
Deleted: sha256:9406881131610a34d957b835c3d33dfb5ea9d2e31425f773cfe55cfd43997948
Deleted: sha256:6216371d2ccbaf557de1f23966e285986a0e054121c6a45d6a48c46e24afc547
Deleted: sha256:d4b189eccefbe0342aeeda284f07182a214a44d3fe1f55afa9f51a8cb8baee4d
Deleted: sha256:c95970bd3292e930f4282018fea00f00c91312af66b95390b000b501f0150a92
Deleted: sha256:03b0292dbc6978fe1077f948ac07472a8cac23fb4c3cf92c76f5eb62891e3d5a
Deleted: sha256:19f2a825ed46bbf16a06d9d5185a0ac68196217f302e760abb4cbc3178b14cc1
Deleted: sha256:b4f0436e967b6b9b021d3ed900a5422da6d8fe8c17c51c7d7e4f9c105812f868
Deleted: sha256:4762552ad7d851a9901571428078281985074e5ddb806979dd7ad24748db4ca0
[root@VM-0-15-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   13 months ago   13.3kB
[root@VM-0-15-centos ~]# 

3、容器中命令

docker exec [it] 容器id 命令   #在容器中执行命令
	- exec 在对应容器中执行命令
	- it 采用交互方式执行命令
docker exec -it 容器id /bin/bash  #在容器中开启bash终端接收后续命令
cat /proc/version #查看docker中linux系统版本
java -version #查看docker中jdk版本
exit #退出docker
/var/lib/docker #docker默认安装目录,镜像image 容器containers

4、容器的生命周期

随笔——Docker学习笔记_第1张图片

你可能感兴趣的:(docker)