阿里云云服务器Docker-Execrise

申请云服务器

  • 阿里云每个人可以免费申请三个月的使用的服务器可以用作学习使用
  • 建议申请规格2核4g的,2g的有点捉襟见肘了
  • 选择服务器建议alibaba-linux服务器,就是linux;选择windows可能由于2核4g的限制,docker不匹配系统起码我就是这样,ubantu网上教程较少,系统也不是很熟悉。linux系统熟悉,并且网上许多大佬分享的也是使用linux进行部署,有问题也好查漏补缺

Linux 安装docker

  1. 查询系统的内核版本

    root@iZbp1h7gyi10n901o1mnzdZ \~\]# uname -r 5.10.134-17.3.al8.x86_64

    使用阿里云的linux服务器默认应该是这个x86 64位系统,保证是在硬件层面没有问题

  2. yum 更新到最新版本

    [root@CodeGuide ~]# sudo yum update
    Last metadata expiration check: 1:15:10 ago on Sat 27 Nov 2021 04:22:53 PM CST.
    Dependencies resolved.
    Nothing to do.
    Complete!
    sudo yum update
    看到显示 Complete 就代表完成了,整个过程需要 5-10 分钟左右

  3. 安装Docker所需的依赖包

java 复制代码
[root@CodeGuide ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Last metadata expiration check: 1:16:16 ago on Sat 27 Nov 2021 04:22:53 PM CST.
Package yum-utils-4.0.21-3.el8.noarch is already installed.
Package device-mapper-persistent-data-0.9.0-4.el8.x86_64 is already installed.
Package lvm2-8:2.03.12-10.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
看到显示 Complete 就代表完成了,整个过程需要 1-3 分钟左右
  1. 设置Docker的yum的源

    使用阿里云的有官方的容器镜像服务->镜像工具->镜像加速器->CentOS

    安装/升级Docker客户端
    推荐安装1.10.0以上版本的Docker客户端,参考文档docker-ce

    1. 配置镜像加速器
      针对Docker客户端版本大于 1.10.0 的用户

    您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
    "registry-mirrors": ["https://q0om03qu.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker

  2. 启动Docker并添加开机自启动

java 复制代码
[root@CodeGuide ~]# sudo systemctl start docker

//启动docker
sudo systemctl start docker

//重启docker
sudo systemctl restart docker

//查询docker版本
docker --version

安装Portainer

这个软件是docker的可视化工具,跟swagger一样属于web程序的可视化

  1. 安装portainer
java 复制代码
[root@CodeGuide portainer]# docker pull portainer/portainer
Using default tag: latest
latest: Pulling from portainer/portainer
94cfa856b2b1: Pull complete 
49d59ee0881a: Pull complete 
a2300fd28637: Pull complete 
Digest: sha256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f
Status: Downloaded newer image for portainer/portainer:latest
docker.io/portainer/portainer:latest
  1. 安装和启动
java 复制代码
[root@CodeGuide]# docker run -d --restart=always --name portainer -p 9000:9000 -v
/var/run/docker.sock:/var/run/docker.sock portainer/portainer

这部分命令安装和启动了portainer这个服务,正如前面所说这个是一个web服务,所以在启动的时候会挂在在启动的9000端口上默认.此时我们使用的是阿里云linux服务器,因此我们想要在自己的浏览器上运行这个挂载在云服务上的portainer需要做到如下几点

  • 查看自己这个实例的公开地址,私有地址是作为云服务内部使用等同于localhost在云服务器linux中使用.公开的才是我们自身电脑访问云服务器的地址.这个在控制台->云服务器ECS->实例中有展示,所以我们在本地浏览器访问的就是'公开地址:9000'.但此时可能还是访问不到

  • 将9000端口暴露出来,控制台(工作台)->云服务器ECS->网络与安全->安全组->管理规则,设置端口9000:优先级1,协议类型:自定义TCP;端口范围9000,授权对象:所有IPv4(0.0.0.0/0) 添加描述:公开portainer端口

  • 查看linux中是否有防火墙,防火墙有两种我的是iptables,也有可能是firewalls,因为我的是iptables所以就展示iptables的写法了

    java 复制代码
    [root@iZbp1h7gyi10n901o1mnzdZ ~]# sudo iptables -A INPUT -p tcp --dport 9000 -j ACCEPT
    [root@iZbp1h7gyi10n901o1mnzdZ ~]# sudo iptables-save > /etc/sysconfig/iptables
    [root@iZbp1h7gyi10n901o1mnzdZ ~]# sudo iptables -L -n | grep 9000
    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:9000
    复制代码
      运行第三个Accept...... tcp dpt:9000就是已经开始监听9000端口了成功了.将9000端口号修改为其他的也是同样可以暴露监听其他端口
  1. 上面的准备工作做好了,就可以在自己的电脑上用浏览器直接访问portainer服务了
  • 例:公开地址 121.56.97.53,可以直接在浏览器搜索栏输入121.56.97.53:9000,就会进入portainer页面了,第一次进入会让你设置密码,账户默认admin,密码:自定义.然后因为我们是想要访问我们在服务器上的docker,有两个docker,一个是local本地的,是我们自己电脑上的docker显然不是这个,所以选择另一个;上面提示有两个信息,一个name就是你给这个docker的实例的命名可以随喜欢;另一个就是url地址,上面有一个实例2375端口,想必就是公开的地址加2375了,跟之前添加9000端口应该是一样的.我们将上面的步骤在做一次.
  1. 重新在安全规则中添加2375端口,在linux服务器中的防火墙暴露监听2375端口之后,发现仍然无法使用portainer访问我们在服务器上的docker,这是因为还少了一个步骤,我们需要在TCP中公开Docker API
  • 查看配置文件路径

    java 复制代码
    systemctl show --property=FragmentPath docker
    FragmentPath=/usr/lib/systemd/system/docker.service
  • 对这个配置文件进行编辑

    java 复制代码
    vim /usr/lib/systemd/system/docker.service
  • 文件内容大致如下

    java 复制代码
    [Unit]
    Description=Docker Application Container Engine
    Documentation=https://docs.docker.com
    After=network-online.target docker.socket firewalld.service containerd.service time-set.target
    Wants=network-online.target containerd.service
    Requires=docker.socket
    
    [Service]
    Type=notify
    # the default is not to use systemd for cgroups because the delegate issues still
    # exists and systemd currently does not support the cgroup feature set required
    # for containers run by docker
    # 将如下这行注释掉 ExecStart只能有一个命令
    # ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
    # 添加如下命令将2375端口公开,这样可以使用portainer访问服务器上的docker了
    ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H 0.0.0.0:2375
    ExecReload=/bin/kill -s HUP $MAINPID
    TimeoutStartSec=0
    RestartSec=2
    Restart=always
    
    # Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
    # Both the old, and new location are accepted by systemd 229 and up, so using the old location
    # to make them work for either version of systemd.
    StartLimitBurst=3
    
    # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
    # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
    # this option work for either version of systemd.
    StartLimitInterval=60s
    
    # Having non-zero Limit*s causes performance problems due to accounting overhead
    # in the kernel. We recommend using cgroups to do container-local accounting.
    LimitNPROC=infinity
    LimitCORE=infinity
    
    # Comment TasksMax if your systemd version does not support it.
    # Only systemd 226 and above support this option.
    TasksMax=infinity
    
    # set delegate yes so that systemd does not reset the cgroups of docker containers
    Delegate=yes
    
    # kill only the docker process, not all processes in the cgroup

    至此在阿里云服务器ECS上使用alibaba-linux 安装了docker和portainer并完成本地访问portainer可视化云服务器上的docker了

相关推荐
plmm烟酒僧3 小时前
使用 Tmux 在断开SSH连接后,保持会话的生命周期
运维·ssh·tmux·分离会话
opentrending5 小时前
Github 热点项目 awesome-mcp-servers MCP 服务器合集,3分钟实现AI模型自由操控万物!
服务器·人工智能·github
多多*5 小时前
Java设计模式 简单工厂模式 工厂方法模式 抽象工厂模式 模版工厂模式 模式对比
java·linux·运维·服务器·stm32·单片机·嵌入式硬件
白夜易寒6 小时前
Docker学习之私有仓库(day10)
学习·docker·容器
Guarding and trust7 小时前
python系统之综合案例:用python打造智能诗词生成助手
服务器·数据库·python
南鸳6107 小时前
Linux常见操作命令(2)
linux·运维·服务器
Kaede67 小时前
怎么安装JSON服务器?JSON服务器最新安装教程
运维·服务器·json
西北大程序猿8 小时前
linux进程信号 ─── linux第27课
linux·运维·服务器·信号处理
inxunoffice8 小时前
批量给 PDF 添加或删除密码保护,支持设置打开密码、只读密码、限制复制和打印
运维·服务器·pdf
宅小海9 小时前
14 配置Hadoop集群-配置历史和日志服务
linux·服务器·hadoop