一、容器化技术-docker初识

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用



前言

    去了新公司线上应用部署环境由传统服务器直接部署变为使用docker,并且感觉以前部署应用方式不够优雅,面对微服务体系资源利用率不够高,所以学习docker。

    所有百度云盘提取码都为1111

一、docker是什么?

      Modernize your applications, accelerate innovation Securely build, share and run modern applications anywhere

     翻译过来就是帮助程序程序构建、共享(可移植)、运行的的平台,思想上非常接近java一次编译,到处运行,在以后学习中可以类比java中相关知识。

二、环境约定

环境非常重要避免未知问题产生!!!!!!!!!!!!!!

  1. win10 64位
  2. virtualBox-6.0.12-133076-Win.exe(1111)
  3. vagrant_2.2.6_x86_64.msi(1111)
  4. centos7

三、环境安装

傻瓜式安装2、3程序,如下图所示。

四、安装centos7

  1. 安装完成virtualbox后更改全局设置,修改虚拟机文件存储位置。
  2. 创建centos7文件夹,并进入其中[目录全路径不要有中文字符]
  3. 在此目录下打开cmd,运行vagrant init centos/7。此时会在当前目录下生成Vagrantfile,同时指定使用的镜像为centos/7,关键是这个镜像在哪里,我已经提前准备好了,名称是virtualbox.box文件
  4. 修改Vagrantfile文件内容
  5. 将virtualbox.box文件添加到vagrant管理的镜像中,vagrant box add centos/7 E:\docker\virtualbox.box(按照本地存储位置)
  6. vagrant box list  查看本地的box校验是否安装成功
  7. 根据Vagrantfile文件启动创建虚拟机,vagrant up。

  8. 通过Xshell连接centos7

  9. vagrant ssh 进入刚才创建的centos7中
  10. sudo -i
  11. vi /etc/ssh/sshd_config
  12. 打开 PasswordAuthentication yes 注释 PasswordAuthentication no
  13. 打开 PermitRootLogin yes
  14. 保存文件修改
  15. 退出 输入passwd
  16. 输入两次新密码
  17. systemctl restart sshd
  18. 使用xshell登录账号为root

五、操作实例

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network. centos和本地机器网络连接方式
  config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end 官方规定最小cpu2核、内存看自己机器配置
    config.vm.provider "virtualbox" do |vb|
        vb.memory = "4000"
        vb.name= "demo"
        vb.cpus= 2
    end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

 

 

 

 

 

 

 

 六、安装docker

  1. 卸载之前的docker	
    sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
  2. 安装必要的依赖
    sudo yum install -y yum-utils \
        device-mapper-persistent-data \
        lvm2
  3. 设置docker仓库

sudo yum-config-manager \
      --add-repo \
      https://download.docker.com/linux/centos/docker-ce.repo

安装docker
    sudo yum install -y docker-ce docker-ce-cli containerd.io
启动docker
    sudo systemctl start docker
测试docker安装是否成功
    sudo docker run hello-world

常用命令

vagrant常用命令
	(1)vagrant ssh    
    	进入刚才创建的centos7中
    (2)vagrant status
    	查看centos7的状态
    (3)vagrant halt
    	停止/关闭centos7
    (4)vagrant destroy
    	删除centos7
    (5)vagrant status
    	查看当前vagrant创建的虚拟机
    (6)Vagrantfile中也可以写脚本命令,使得centos7更加丰富
    	但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload
box的打包分发
01 退出虚拟机
	vagrant halt

02 打包
	vagrant package --output first-docker-centos7.box
	
03 得到first-docker-centos7.box
	
04 将first-docker-centos7.box添加到其他的vagrant环境中
	vagrant box add first-docker-centos7 first-docker-centos7.box
	
05 得到Vagrantfile
	vagrant init first-docker-centos7

06 根据Vagrantfile启动虚拟机
	vagrant up [此时可以得到和之前一模一样的环境,但是网络要重新配置]
docker
docker pull        拉取镜像到本地
docker run         根据某个镜像创建容器
-d                 让容器在后台运行,其实就是一个进程
--name             给容器指定一个名字
-p                 将容器的端口映射到宿主机的端口
docker exec -it    进入到某个容器中并交互式运行
images             查询所有镜像
ps                 查看容器列表

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值