Linux~Docker容器入门学习

一、Docker简单介绍

背景:物理机时代,由于部署非常慢,成本高,资源浪费,并且难于扩展与迁移。更快的部署,有效利用内存和磁盘资源,更容易的横向扩展。

容器浅谈

容器化技术的应用场景

阿里与腾讯云,容器化技术的视线

阿里巴巴虚拟机根据应用场景的不同安装容器,虚拟机内部需要使用容器化技术进行部署

启动Docker:systemctl start docker.service
查看Docker运行状态:systemctl statusdocker.service

二、离线~容器的安装与部署

(一)准备环境

虚拟机配置环境

|------------|-------------------|-------|-------|------|---------|
| 地址 | 主机名称 | CPU | 内存 | 磁盘配置 | 角色说明 |
| 10.0.0.201 | Docker-10.0.0.201 | 2Core | >=4G | 50G | ES NODE |

启动虚拟机和配置环境

关闭虚拟机Selinux

关闭firewalld

#可以看前面的内容主机优化

(二)上传Docker安装包

1.百度网盘下载对应的安装包

Docker网盘地址:

NGINX网盘地址:

2.上传到虚拟机安装

bash 复制代码
[root@docker ~]# rz -E
rz waiting to receive.
[root@docker ~]# ll
总用量 225156
-rw-r--r-- 1 root root 230555846 12月  4 2023 docker.tar.gz

(三)解压安装包

bash 复制代码
[root@docker ~]# unzip docker.zip
[root@docker ~]# ll
总用量 225160
-rw-r--r-- 1 root root 230555846 12月  4 2023 docker.zip
drwxr-xr-x 2 root root       200 6月  11 2023 download
-rwxr-xr-x 1 root root      3363 12月  4 2023 install-docker.sh

(四)正式安装Docker与Docker Compose

bash 复制代码
[root@docker ~]# ./install-docker.sh install

(五)验证是否安装成功

bash 复制代码
[root@oldboy tmpdata]# ps -axu | grep docker
root       7047  0.0  1.0 1426584 43856 ?       Ssl  15:36   0:01 /usr/bin/dockerd
root       7054  1.4  0.7 1341944 29004 ?       Ssl  15:36   0:50 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
root       7213  0.0  0.1 711516  7148 ?        Sl   15:36   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 92 -container-ip 172.17.0.2 -container-port 80

(六)配置内核转发

为后面实验端口映射做准备

bash 复制代码
[root@docker ~]# echo "net.ipv4.ip_forward = 1 " >> /etc/sysctl.conf 
[root@docker ~]# sysctl -p
net.ipv4.ip_forward = 1

三、镜像image

(一)在线拉取镜像(和离线一样,步骤忽略)

bash 复制代码
[root@docker ~]# docker pull nginx:1.24-alpine
[root@docker ~]# docker pull nginx:latest
##############
nginx:lastest      #最新版本的nginx镜像
ngixn:stable       #最新的稳定版
nginx:1.20.1      #下载指定版本
nginx:1.24-alpine  #下载指定版本+系统

(二)导入本地镜像

bash 复制代码
[root@docker ~]# docker image load < nginx.tar 
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>]     62MB/62MB
b8d6e692a25e: Loading layer [==================================================>]  3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>]  4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>]  3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest

(三)导出本地镜像到本地

bash 复制代码
[root@docker ~]# docker image save nginx:latest > nginx-latest.tar
[root@docker ~]# ll
总用量 142488
-rw-r--r-- 1 root root 145905152 8月  23 10:30 nginx-latest.tar

(四)查看系统镜像列表

查看系统镜像列表方式一:

bash 复制代码
[root@docker tmpdata]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
game                 v1        1942c42269a3   5 hours ago   162MB
nginx                latest    605c77e624dd   2 years ago   141MB
sagikazarmark/dvwa   latest    e901498e651a   7 years ago   359MB
[root@docker tmpdata]# 

查看系统镜像列表方式二:

bash 复制代码
[root@docker tmpdata]# docker image ls
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
game                 v1        1942c42269a3   5 hours ago   162MB
nginx                latest    605c77e624dd   2 years ago   141MB
sagikazarmark/dvwa   latest    e901498e651a   7 years ago   359MB
[root@docker tmpdata]# 

查看系统镜像列表方式三:

bash 复制代码
[root@docker tmpdata]# docker image ls -a
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
game                 v1        1942c42269a3   5 hours ago   162MB
nginx                latest    605c77e624dd   2 years ago   141MB
sagikazarmark/dvwa   latest    e901498e651a   7 years ago   359MB
[root@docker tmpdata]# 

(五)删除镜像

bash 复制代码
[root@tmpdata]# docker image rm game:v1
Untagged: game:v1
Deleted: sha256:1942c42269a39ae611f2b342cf231a0e43c5560c9ad3c5074c550bfe177ffcf9
Deleted: sha256:e2f5d48a0b8cd22cb11a24aa6b05251b72b85c11cfd58b3361bfcab16db87687
[root@tmpdata]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
nginx                latest    605c77e624dd   2 years ago   141MB
sagikazarmark/dvwa   latest    e901498e651a   7 years ago   359MB
[root@tmpdata]# 

(六)image镜像打标签

bash 复制代码
[root@tmpdata]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
nginx                latest    605c77e624dd   2 years ago   141MB
sagikazarmark/dvwa   latest    e901498e651a   7 years ago   359MB
[root@tmpdata]# docker tag nginx:latest nginx:new
[root@tmpdata]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
nginx                latest    605c77e624dd   2 years ago   141MB
nginx                new       605c77e624dd   2 years ago   141MB
sagikazarmark/dvwa   latest    e901498e651a   7 years ago   359MB
[root@tmpdata]# 

(七)查询仓库中的镜像

bash 复制代码
[root@tmpdata]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        20102     [OK]       
linuxserver/nginx                 An Nginx container, brought to you by LinuxS...   217                  
bitnami/nginx                     Bitnami container image for NGINX               193                  [OK]
ubuntu/nginx                      Nginx, a high-performance reverse proxy & we...   116                  
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo...   93                   
nginx/unit                        This repository is retired, use the Docker o...   63                   
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN...   43                   
rapidfort/nginx                   RapidFort optimized, hardened image for NGINX   15                   
kasmweb/nginx                     An Nginx image based off nginx:alpine and in...   8                    
dockette/nginx                    Nginx SSL / HSTS / HTTP2                        3                    [OK]
circleci/nginx                    This image is for internal use                  2                    
nginx/nginx-ingress-operator      NGINX Ingress Operator for NGINX and NGINX P...   2                    

(八)推送镜像到仓库

bash 复制代码
[root@docker ~]# docker push nginx:latest

仓库地址(类似GIT、SVN):

https://tuv7rqqq.mirror.aliyuncs.com

bash 复制代码
daemon.json  key.json     
[root@oldboy tmpdata]# cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://tuv7rqqq.mirror.aliyuncs.com"]
}
[root@oldboy tmpdata]# 

四、容器container

(一)运行容器RUN命令

分两步:

docker container create 【】 #创建

docker container start 【】 #启动
#docker run -d -p 96:80 nginx:latest

-d #后台运行

-p 80:99 #端口映射【宿主机对应容器的端口】访问书主机的80端口,就会转发到docker容器99端口;

--name #自定义容器的名称

-v #指定存储卷【宿主机的路径挂载到容器的路径下】

-i #进入交互模式

-t #分配一个终端(-it一起使用);

bash 复制代码
[root@oldboy tmpdata]# docker run -d -p 96:80 nginx:latest
d4126a017c12c771e55752d7b3285dbfb5ee7499709101d7ebe8d83ec7fccfaf
[root@oldboy tmpdata]# 

(二)查看容器PS命令

查看容器语法:Docker ps

查看所有容器(没有运行的也显示出来):docker ps -a

查看所有容器,只显示容器的ID:docker ps -a -q

bash 复制代码
[root@~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS         PORTS                               NAMES
8745fff81c98   nginx:latest   "/docker-entrypoint...."   46 hours ago   Up 8 seconds   0.0.0.0:92->80/tcp, :::92->80/tcp   v3
[root@~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS          PORTS                               NAMES
8745fff81c98   nginx:latest   "/docker-entrypoint...."   46 hours ago   Up 16 seconds   0.0.0.0:92->80/tcp, :::92->80/tcp   v3
[root@oldboy ~]# 

(三)启动和关闭容器

bash 复制代码
[root@docker ~]# docker stop 2308136cfc12
[root@docker ~]# docker start 2308136cfc12

(四)删除容器

bash 复制代码
#删除容器
[root@docker ~]# docker rm 2308136cfc12

#强制删除容器
[root@docker ~]# docker rm -f 2308136cfc12

#删除所有容器【``】
[root@docker ~]# docker rm -f `docker ps -a -q`
4bdf929033cc

(五)进入容器

bash 复制代码
[root@docker ~]# docker ps -q
8745fff81c98
[root@oldboy ~]# 


#进入容器(运行中容器,进入)
[root@~]#  docker exec -it 8745fff81c98 /bin/bash
root@8745fff81c98:/# 



#创建容器,并直接进入
[root@docker ~]# docker run -it --name nginx -p 80:80 nginx:latest /bin/bash
root@179f521b7495:/# exit

(六)宿主机与容器文件传输CP

bash 复制代码
#1,宿主机==》容器
[root@docker ~]# docker cp ./html nginx:/usr/share/nginx/

#2,容器到宿主机
[root@docker ~]# docker cp nginx:/usr/share/nginx/html   /opt/

(七)保存容器为镜像

bash 复制代码
[root@docker ~]# docker commit nginx game:v1

(八)其他命令

bash 复制代码
docker kill 容器名称  #干掉干不掉容器
docker top 容器名    #查看容器进程

docker stats  容器名   #查看容器状态

#查看容器的资源使用情况
[root@docker ~]# docker stats game 

1.docker run 跑容器的时候可以进行资源限制

bash 复制代码
[root@docker ~]# docker run -d --name game -p 80:80 -m 50MB  game:v1 

或者

[root@docker ~]# docker run -d --name game -p 90:80 -m 50MB  game:v1 

五、数据持久化

bash 复制代码
# docker run -d -p 88:80 --name v1 -v /data/html:/usr/share/nginx/html nginx:latest

六、容器的重启策略

--restart #当容器挂掉,是否重启的设置?什么时候重启?

bash 复制代码
--restart=always
############################################
always    #自动重启,只要容器关闭了,就自动重启;
unless-stopped  #只有容器正常关闭的时候,才重启;
on-failure   #只有不正常关闭的时候,才重启;
#############################################
[root@docker ~]# docker run -d -p 90:80 --name v2 -v /data/html:/usr/share/nginx/html  --restart=unless-stopped  nginx:latest
相关推荐
qq_254674411 小时前
ssl 协议工作过程
网络·网络协议·ssl
白总Server1 小时前
SSL认证解说
运维·服务器·网络·网络协议·云原生·kubernetes·ssl
节点小宝1 小时前
远程访问NAS速度慢??那是因为你没用对。。。
运维·服务器·网络
WangMing_X1 小时前
linux入门到实操-4 linux系统网络配置、连接测试、网络连接模式、修改静态IP、配置主机名
linux·运维·服务器
golove6661 小时前
网络运维故障处理案例
运维·网络
晓生谈跨境1 小时前
什么是外贸专用路由器?
网络·智能路由器
布说在见1 小时前
负载均衡:从理论到实践 ---day04
java·运维·负载均衡
happy_king_zi1 小时前
ansible常用模块
运维·ansible·devops
weixin_510695551 小时前
如何用静态住宅代理实现分布式代理网络
网络·分布式·php
蓝胖子不是胖子2 小时前
docker 多服务只暴露一个客户端
运维·docker·容器