您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么實現docker環境搭建,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
vagrant、virtualbox、centos7、xshell:鏈接:https://pan.baidu.com/s/1iilSiJgOQ8Dk0Ol2JcZNzw 密碼:kybq
官網下載:
Vagrant:https://www.vagrantup.com/
VirtualBox:https://www.virtualbox.org/
1、創建一個存放contos7的文件夾、并進入該目錄(目錄不要用中文)
2、在此目錄輸入cmd,運行:vagrant init centos/7,在此目錄會生成一個Vagrantfile。
修改Vagrantfile:
# -*- 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. # 共用網絡 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 config.vm.provider "virtualbox" do |vb| vb.memory = "3000" vb.name= "xj-centos7" 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
3、將virtualbox.box文件添加到vagrant管理的鏡像中
如文件virtualbox.box在E:\centOs\目錄下:
vagrant box add centos/7 E:\centOs\virtualbox.box
4、查看本地是否安裝成功:vagrant box list
5、啟動contos7:vagrant up
進入創建的contos7:vagrant ssh
查看centos7狀態:vagrant status
停止contos7:vagrant halt
刪除contos7:vagrant destroy
vagrantfile中也可以寫腳本命令,使得centos7更加豐富,但是要注意,修改了Vagrantfile,要想使正常運行的centos7生效,必須使用vagrant reload
1、查看contos7信息:vagrant ssh-config
關注IP:127.0.0.1、prot:2222、用戶名/密碼:vagrant/vagrant
文件:Identityfile指向的文件private-key
2、root設置密碼:
sudo -i
vi /etc/ssh/sshd_config
修改PasswordAuthentication yes并保存
passwd root
systemctl restart sshd
使用賬號root,密碼root進行登錄
1、退出虛擬機:vagrant halt
2、打包:vagrant package --output first-docker-centos7.box
3、得到first-docker-centos7.box
4、將first-docker-centos7.box添加到其他的vagrant環境中:vagrant box add first-docker-centos7 first-docker-centos7.box
5、得到Vagrantfile:vagrant init first-docker-centos7
6、根據Vagrantfile啟動虛擬機:vagrant up [此時可以得到和之前一模一樣的環境,但是網絡要重新配置]
1、如果非root用戶登陸需在命令前面加上sudo,或者將當前用戶添加到docker用戶組;
sudo groupadd docker #添加docker用戶組 sudo gpasswd -a $USER docker #將登陸用戶加入到docker用戶組中 newgrp docker #更新用戶組
2、卸載之前安裝的docker:
sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
3、安裝必要的依賴
sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
4、添加倉庫:
官網倉庫:
sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
阿里云倉庫:
sudo yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
設置阿里云加速器需注銷阿里云賬號:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
5、安裝docker:
sudo yum install -y docker-ce docker-ce-cli containerd.io
6、啟動docker:
sudo systemctl start docker
7、測試docker
sudo docker run hello-world
01 創建tomcat容器 下載鏡像:docker pull tomcat 運行tomcat把8080端口映射到contos7的9090端口:docker run -d --name my-tomcat -p 9090:8080 tomcat 訪問方式:contos7IP:9090(如果404檢查webapps文件是否空文件,webapps.dist有內容) rm -rf webapps mv webapps.dist webapps 03 進入到容器里面 docker exec -it containerid /bin/bash docker pull 拉取鏡像到本地 docker run 根據某個鏡像創建容器 -d 讓容器在后臺運行,其實就是一個進程 --name 給容器指定一個名字 -p 將容器的端口映射到宿主機的端口 docker exec -it 進入到某個容器中并交互式運行
上述就是小編為大家分享的怎么實現docker環境搭建了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。