Home > Backend Development > PHP Tutorial > Docker for Windows installation and Linux+PHP development environment construction (1)

Docker for Windows installation and Linux+PHP development environment construction (1)

WBOY
Release: 2016-07-30 13:30:13
Original
2245 people have browsed it

What is Docker?

Simply speaking, Docker is a "container" (Linux container, LXCs) run by a program written in the GO language; the cornerstone of current cloud services is operating system level isolation, on the same physical machine Multiple hosts are virtualized on the server. Docker implements an application-level isolation; it changes our basic development and operation unit from directly operating the virtual host (VM) to a "container" where the operating program runs.

Benefits of Docker

Advantages of Docker: fast running, agile deployment, flexibility, lightweight, open source and free.

Boot2Docker is a lightweight Linux distribution package specially designed for Docker, which solves the problem that Windows or OS X users cannot install Docker. Boot2Docker runs completely in memory, is 24M in size, and only takes 5-6 seconds to start. Boot2Docker needs to run in VirtualBox.

Docker for Windows installation and usage steps

Check the system version and hardware virtualization

First, check the Windows operating system version number

The operation of Boot2Docker requires the operating system to be at least Windows 7.1, 8/8.1. Tested by Windows 7 Ultimate, it is also supported.

Secondly, you need to ensure that your system supports hardware virtualization and virtualization is enabled.

Enabling virtualization technology support requires several aspects of support: chipset support, BIOS support, processor support, and operating system support.

In terms of operating systems, all mainstream operating systems support VMM management, so there is no need to consider it. As for the chipset, it has been supported since the Intel 945 (except netbooks) era, so there is no need to consider it. In terms of CPU, you can query it through Intel's official website or use third-party testing software to judge. Therefore, it is more about checking whether the BIOS supports it.

For Windows 8/8.1 systems, open the Task Manager-"Performance" tab-CPU and you can see that virtualization is turned on. As shown below:

Docker for Windows installation and Linux+PHP development environment construction (1)

If it is "not enabled", you need to go to the motherboard BIOS to enable it.

How to enter the BIOS: When the computer is starting up, when the "Press DEL to enter SETUP" prompt appears at the bottom of the screen, immediately press the "Del" key to enter the BIOS setup program. For some types of BIOS, you need to press the F2 or F10 key to enter the startup logo screen, and just follow the on-screen prompts.

For example, find the Intel Virtual Technology setting item and change Disabled to Enabled. Then save and exit to enable the VT function. As shown below:

Enable Intel Virtual Technology

For Windows 7 systems, you need to install Microsoft Hardware-Assisted Virtualization Detection Tool to determine whether the processor supports virtualization and whether virtualization is turned on. Download and install the HAV detection tool, follow the on-screen prompts for detection results, and then refer to the HAV Detection Tool User Guide for the next step.

Download address:

HAV Detection Tool
http://download.microsoft.com/download/1/9/F/19FD407F-A7E9-4393-A845-D0B1F539678E/havdetectiontool.exe

HAV Detection Tool User Guide
http://download.microsoft.com/download/1/9/F/19FD407F-A7E9-4393-A845-D0B1F539678E/HAV%20Detection%20Tool%20-%20User%20Guide.mht

If the screen prompts as shown below, It indicates that the current computer processor supports hardware virtualization and has been enabled.

Hardware-Assisted Virtualization Detection

Install Boot2Docker

Next, you need to install Boot2Docker and some auxiliary software. Specifically include:

Docker Client for Windows
Boot2Docker management tool and ISO
Oracle VM VirtualBox
Git MSYS-git UNIX tools

First click to download Boot2Docker for Windows.

Download address:

https://s3.amazonaws.com/github-cloud/releases/18047765/825908bc-15b8-11e5-96cb-9a42a043cc39.exe?response-content-disposition=attachment%3B%20filename%3Ddocker -install.exe&response-content-type=application/octet-stream&AWSAccessKeyId=AKIAISTNZFOVBIJMK3TQ&Expires=1437386585&Signature=wM%2BTTYN4yqc8fhh7WAEeZw7A5cE%3D

Then double-click the downloaded docker-install.exe installation file to start the installation. It is best to turn off the computer's relevant security software in advance during the installation process.

The official download speed is relatively slow, you can try to download from domestic DaoCloud http://get.daocloud.io/

Verify whether the installation is successful

Start Boot2Docker after the installation is completed.

Desktop Iconset

After opening the Boot2Docker terminal, if the system prompts for User Account Control, please select "Yes" to ensure that the virtual machine VirtualBox completes some computer configuration changes. If there is no prompt, generally no settings are required.

Note: User Account Control (UAC) is a new security feature of Windows Vista. It prevents malware from gaining privileges, even if the user is logged in with an administrator account. UAC can be modified in the control panel. Taking Win7 Ultimate as an example: Control Panel → All Control Panel Items → User Account → Change User Account Control setting).

UAC Setting

The terminal needs a certain initialization time to start Boot2Docker and execute the environment variable bash script required for Docker to run. If it starts successfully, a $ prompt will appear.

Boot2docker Start

Use the boot2docker ssh command to enter the VM. You will need to use this command later to perform some basic configuration.

boot2docker ssh

You can now use the docker command directly.

Execute the command docker run hello-world in the terminal and press Enter. If it runs normally, the following content will be output:

<code>$ docker run hello-world
Unable <span>to</span> find image <span>'hello-world:latest'</span> locally
Pulling repository hello-world
<span>91</span>c95931e552: Download complete 
a8219747be10: Download complete 
Status: Downloaded newer image <span>for</span> hello-world:latest
Hello <span>from</span> Docker.
This message shows that your installation appears <span>to</span> be working correctly.


To generate this message, Docker took <span>the</span> following steps:
 <span>1.</span> The Docker client contacted <span>the</span> Docker daemon.
 <span>2.</span> The Docker daemon pulled <span>the</span><span>"hello-world"</span> image <span>from</span><span>the</span> Docker Hub.
    (Assuming <span>it</span> was <span>not</span> already locally available.)
 <span>3.</span> The Docker daemon created <span>a</span><span>new</span> container <span>from</span> that image which runs <span>the</span>
    executable that produces <span>the</span> output you are currently reading.
 <span>4.</span> The Docker daemon streamed that output <span>to</span><span>the</span> Docker client, which sent <span>it</span><span>to</span> your terminal.


To <span>try</span> something more ambitious, you can run <span>an</span> Ubuntu container <span>with</span>:
 $ docker run -<span>it</span> ubuntu bash


For more examples <span>and</span> ideas, visit:
 <span>http</span>://docs.docker.com/userguide/

</code>
Copy after login

Understand containers and images

Take the command docker run hello-world as an example:

docker run container

A container is a simplified version of the Linux operating system, and the image is loaded into the container Software collection, after this command is executed, Docker will take the following actions in order:

a. Check whether the hello-world image has been installed
b. If it is not installed, download the image from the Docker remote repository (Docker Hub)
c. Load the image into the container and run it.

Create a Doker PHP container and enable the PHP development environment

Docker Hub has images provided by individual developers, as well as official images uploaded by companies and organizational structures. You can use Docker Hub images or your own local private images. (eg local.registry.com:5000/php). Taking the establishment of a PHP development environment as an example, we will continue to explain how to use Docker commands.

What is used here is a self-built private image (Linux+PHP+Apache has been integrated). The private image address is: 172.16.100.71:5000/php

1. Set up the Windows system and the host of the boot2docker VM

a. Set up Windows System local host

Setting the Windows system local host is mainly to facilitate operations in the CMD command prompt. If you are operating in Git Bash, you can choose to skip the local host settings of the Windows system.

Location: C:WindowsSystem32driversetchosts

Add a line 172.16.100.71 local.registry.com

b. Set the host of the boot2docker virtual machine

Enter the virtual machine through the boot2docker ssh command, and then execute the command:

sudo vi /etc/hosts
Add a line to the file: 172.16.100.71 local.registry.com
exit

You can also use the combined command:

<code>boot2docker ssh <span>"echo $'172.16.100.71 local.registry.com' | sudo tee -a /etc/hosts"</span></code>
Copy after login

2. Check to set the environment variables

Enter in Git Bash:

<code><span>export</span></code>
Copy after login

Check whether the environment variables have been automatically set: DOCKER_HOST, DOCKER_CERT_PATH, DOCKER_TLS_VERIFY

boot2docker shellinit can display the Docker client environment variables on the end, so you can use the following command to automatically set the environment variables

<code><span>eval</span><span>"<span>$(boot2docker shellinit)</span>"</span></code>
Copy after login

If this command cannot be set successfully, you need to enter the following command to add it manually:

<code><span>export</span> DOCKER_HOST=tcp:<span>//192.168.59.103:2376</span><span>export</span> DOCKER_CERT_PATH=/c/Users/Administrator/.boot2docker/certs/boot2docker-vm
<span>export</span> DOCKER_TLS_VERIFY=<span>1</span></code>
Copy after login

Enter export after execution to check whether the writing is successful.

To enable Docker commands to be operated in Windows' own CMD, type the following command in cmd:

<code><span>set</span> DOCKER_HOST<span>=</span>tcp:<span>//192.168.59.103:2376</span><span>set</span> DOCKER_CERT_PATH<span>=</span>C:/Users/Administrator<span>/</span><span>.</span>boot2docker/certs/boot2docker<span>-vm</span><span>set</span> DOCKER_TLS_VERIFY<span>=</span><span>1</span></code>
Copy after login

Enter set after execution to check whether the writing is successful.

However, the environment variables set through the set command are temporarily valid and will no longer take effect when the cmd command line window is closed.

The setting method of permanent environment variables is: "Computer-Properties-Advanced System Settings-Advanced-Environment Variables", create the above three system variables and fill in the corresponding values. After the setting is completed, reopen CMD to take effect.

In addition, if you want to run commands such as boot2docker ssh in CMD, you also need to set Git-related environment variables. The method is:

<code>set PATH=<span>%PATH</span><span>%;</span><span>"C:\Program Files (x86)\Git\bin"</span></code>
Copy after login

The permanent environment variable setting method is the same as above, add the following directory to the system variable PATH field (the specific directory is added according to your own Git installation directory)

;C:Program Files (x86)Gitbin;

Then restart cmd It takes effect immediately.

3. Set the –insecure-registry option

Starting from docker 1.3.1, connecting to insecure private images is not allowed by default. Now we want to download the self-built private image, which needs to be set in the following way:

<code>boot2docker ssh
<span>sudo</span> vi /var/lib/boot2docker/profile</code>
Copy after login

Add a line

<code><span>EXTRA_ARGS=<span><span>"--insecure-registry local.registry.com:5000"</span></span></span></code>
Copy after login

Save, and then restart the docker service:

<code><span>sudo</span> /etc/init.d/docker restart</code>
Copy after login

You can also merge the operation in Git Bash:

<code>boot2docker ssh <span>"echo $'EXTRA_ARGS=\"</span>--insecure-registry local.registry.com:<span>5000</span>\<span>"' | sudo tee -a /var/lib/boot2docker/profile && sudo /etc/init.d/docker restart"</span></code>
Copy after login

Instructions: local.registry Private mirror addresses such as .com:5000 can also be replaced by IP addresses.
The tee command parameter -a is to append content to the file, use it as appropriate.

Docker for Windows installation and Linux+PHP development environment construction (1)

The sudo /etc/init.d/docker restart command sometimes fails to restart, so we can observe the information of the docker process through the following command:

<code>ps -ef <span>|grep docker</span></code>
Copy after login

若docker主进程(/usr/local/bin/docker)PID比较小,且运行时间比较久,那么说明没有重启成功。需要强制杀死该进程后再重启。

<code><span>sudo</span> kill -<span>9</span> pid
<span>sudo</span> /etc/init.d/docker restart</code>
Copy after login

最后通过ps -ef |grep docker命令再检查一遍进程号和运行时间,保证docker重启成功。

<code>ps -ef |<span>grep</span> docker
<span>exit</span></code>
Copy after login

4、将私有镜像下载到本地

<code>docker pull local<span>.registry</span><span>.com</span>:<span>5000</span>/php</code>
Copy after login

在镜像运行前,先将其下载到本地,如果报错,参照之前步骤或错误处理进行解决,解决后继续执行后续步骤

5、检查、设置虚拟机共享文件夹

VirtualBox默认将宿主机目录C:\Users作为共享文件夹,并自动挂载到虚拟机c/Users路径。所以一般情况下我们通常选择将数据放在宿主机C:\Users目录,然后通过docker命令挂载到容器。但作为Windows用户,大家的C盘常作为系统盘,关键数据放在C盘是比较危险的,一旦Windows系统损坏无法启动有可能造成数据丢失。

所以,我们想到:能不能自己添加其他盘符下的共享文件夹,挂载到容器?

当然可以!!!步骤有些繁琐,继续往下看。

经过尝试,在C:\Users目录创建指向其他盘符共享文件夹的快捷方式这个方法是行不通的。

下面是正确的操作步骤:

打开boot2docker虚拟机GUI,点击“设置”-“共享文件夹”,在“固定分配”菜单右键“添加共享文件夹”,如图:

VitualBox ShareFolder

a. 执行boot2docker ssh 进入虚拟机,先确定要挂载到的目录,若目录不存在需要手动创建:

<code>boot2docker ssh
<span>sudo</span> mkdir -p /f/projects/phpdev</code>
Copy after login

b. 创建要挂载到的目录后,执行以下命令进行共享文件夹挂载。

<code><span>sudo</span> mount -t vboxsf f/projects/phpdev  /f/projects/phpdev</code>
Copy after login

<code>sudo mount <span>-t</span> vboxsf <span>-o</span> uid<span>=</span><span>1000</span>,gid<span>=</span><span>50</span> f/projects/phpdev  /f/projects/phpdev</code>
Copy after login

如下图:

Docker for Windows installation and Linux+PHP development environment construction (1)

mount完成后退出, boot2docker ssh重新进入虚拟机,查看宿主机共享文件夹是否同步到挂载目录。

从图中可以看到,宿主机本地目录F:\projects\phpdev下的文件/文件夹已经同步显示到当前虚拟机挂载目录下,即/f/projects/phpdev路径下。

由于下文步骤六的docker容器是将/f/projects/phpdev挂载到容器Linux环境的/var/www/html路径,这一步操作就是为了接下来步骤做准备。

我们一般情况下,为了保证数据安全性和完整性,不会在docker创建的容器环境中永久保存数据。常见的方法是将本地宿主机的文件系统映射到容器环境的文件系统,这样能确保容器环境中生成的数据直接保存在用户主机的磁盘,而不会由于容器损坏或未及时将容器持久化而造成数据丢失。

根据boot2docker官方(https://github.com/boot2docker/boot2docker)的描述,boot2docker内置了一个专门用于虚拟机文件夹共享的客户端增强包(VirtualBox Guest Additions)。

若以下共享文件夹名称存在,则该共享文件夹则自动挂载到以下位置:

Users 挂载到 /Users
/Users挂载到 /Users
c/Users挂载到 /c/Users
/c/Users挂载到 /c/Users
c:/Users挂载到 /c/Users

如果需要共享其他文件路径,则需要执行以下命令:

<code>boot2docker ssh
sudo mount <span>-t</span> vboxsf your<span>-other</span><span>-share</span><span>-name</span> /some/mount/location</code>
Copy after login

<code>boot2docker ssh
mount <span>-t</span> vboxsf <span>-o</span> uid<span>=</span><span>1000</span>,gid<span>=</span><span>50</span> your<span>-other</span><span>-share</span><span>-name</span> /some/mount/location</code>
Copy after login

注:Linux命令id可查看用户的UID(对应/etc/passwd)和GID(对应/etc/group)

不过,如果重启Windows,这些设置会被清除,需要再次进行挂载操作(当然,直接重启boot2docker-vm同样会清除所有配置数据)。如果你不想每一次都手动挂载,可以在/etc/fstab中添加一项

<code>your<span>-other</span><span>-share</span><span>-name</span> /some/mount/location vboxsf rw,gid<span>=</span><span>100</span>,uid<span>=</span><span>1000</span>,auto <span>0</span><span>0</span></code>
Copy after login

<code>your<span>-other</span><span>-share</span><span>-name</span> /some/mount/location vboxsf defaults <span>0</span><span>0</span></code>
Copy after login

这样就能够自动挂载了(需要安装客户端增强包VirtualBox Guest Additions)。

执行完手动挂载命令,正常情况下,/some/mount/location目录下已经可以显示共享文件夹的内容了。

由于boot2docker本身是虚拟机,docker也作为虚拟机环境,要使用docker run -v path1:path2成功挂载,实际需要进行两步映射:共享文件夹先由宿主机挂载到boot2docker虚拟机,再由boot2docker虚拟机挂载到docker容器环境。

挂载前先建立挂载目录:

<code><span>sudo</span> mkdir -p /some/mount/location</code>
Copy after login

然后再执行上述mount挂载命令。

卸载共享文件夹,使用命令sudo umount -f /f/projects/phpdev

注意:

卸载共享文件夹需要先退出挂载目录,再卸载,否则会报错:
umount: can’t forcibly umount /f/projects/phpdev: No such file or directory

同时要注意卸载目录一定要写对,注意盘符F前的斜线/,如果要卸载的目录没有挂载过,则会报错:
umount: can’t forcibly umount /f/projects/phpdev: Invalid argument

6、根据所提供镜像,创建并运行一个容器

根据拉到本地的私有镜像(Linux+Apache+PHP集成镜像),创建并运行一个容器:

<code>docker run <span>-d</span><span>-p</span><span>8000</span>:<span>80</span><span>-p</span><span>2222</span>:<span>22</span><span>--</span>name php <span>-v</span> /f/projects/phpdev:/<span>var</span>/www/html <span>local</span><span>.</span>registry<span>.</span>com:<span>5000</span>/php</code>
Copy after login

其中80端口是HTTP/Nginx默认端口,22端口是Linux SSH默认端口。以后台运行方式,将宿主机本地目录/f/projects/phpdev挂载到容器。

注意:

若在Windows系统的Git bash下执行会挂载失败报错,因为msysgit会将/c/Users这样的目录自动转为c:\Users,所以需要使用双斜线,Git bash中输入命令为:

<code>docker run <span>-d</span><span>-p</span><span>8000</span>:<span>80</span><span>-p</span><span>2222</span>:<span>22</span><span>--</span>name php <span>-v</span><span>//f//projects//phpdev:/var/www/html local.registry.com:5000/php</span></code>
Copy after login

然后,通过docker ps 命令,可以查看当前运行中的容器。

<code><span>$ </span>boot2docker.exe ip
<span>192.168</span>.<span>59.103</span></code>
Copy after login

我们可以通过ssh进入到当前php容器,也可使用XShell等SSH工具连接。

SSH登录命令为:

<code>ssh -p <span>2222</span> root<span>@192</span>.<span>168.59</span>.<span>103</span></code>
Copy after login

注:boot2docker ssh的账号密码为user/docker ,私有镜像为root/hellonihao

SSH登录后就可以看到我们熟悉的Linux目录结构了。

如果本地没有phpdev文件夹将会自动创建,在该目录下新建个phpinfo.php测试文件,用于输出php配置信息。

Docker的宿主机是boot2docker的虚拟机,因此需要使用虚拟机的ip进行访问,获取ip方式为boot2docker ip。

所以上述phpinfo.php文件访问地址:192.168.59.103:8000/phpinfo.php

Docker for Windows installation and Linux+PHP development environment construction (1)

举例,仅根据centos基础镜像建立一个容器,并建立一个数据卷/data,命令如下

<code><span>$</span><span>docker</span><span>run</span><span>-</span><span>it</span><span>-</span><span>-</span><span>rm</span><span>-</span><span>-</span><span>name</span><span>centos</span><span>-</span><span>-</span><span>volume=/data</span><span>local</span><span>.</span><span>registry</span><span>.</span><span>com:5000/centos</span><span>/bin/bash</span></code>
Copy after login

注意:在Git Bash需要用双斜线,执行

<code><span>$</span><span>docker</span><span>run</span><span>-</span><span>it</span><span>-</span><span>-</span><span>rm</span><span>-</span><span>-</span><span>name</span><span>centos</span><span>-</span><span>-</span><span>volume=//data</span><span>local</span><span>.</span><span>registry</span><span>.</span><span>com:5000/ce</span><span>ntos</span><span>//bin//bash</span></code>
Copy after login

将宿主机目录挂载到数据卷,命令如下:

<code>$ docker run <span>-it</span><span>--</span>rm  <span>--</span>name centos <span>-v</span> /c/Users/phpdev:/<span>data</span><span>local</span><span>.</span>registry<span>.</span>com:<span>5000</span>/centos /bin/bash </code>
Copy after login

注意:在Git Bash下需要用双斜线,即:

<code><span>$ </span>docker run -it --rm  --name centos -v /<span>/c/</span><span>/Users/</span><span>/phpdev:/data</span> local.registry.<span>com:</span><span>5000</span>/centos /<span>/bin/</span><span>/bash </span></code>
Copy after login

附其他常用命令:

docker rm -f php
docker ps -l
docker images -a
docker rmi
docker stop
docker run
docker kill

以上是docker for Windows版本的安装方法和docker使用步骤的介绍。如果有错误疏漏,欢迎留言指正,一起探讨学习docker。

下一篇会分享介绍docker安装使用中常见错误的处理方法。


参考文章:

http://docs.docker.com/userguide/ “docker官方文档-用户指南”
https://github.com/boot2docker/boot2docker “Boot2docker官方文档”
http://blog.pavelsklenar.com/5-useful-docker-tip-and-tricks-on-windows/

本文来自于CSDN博客,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了Docker for Windows安装与Linux+PHP开发环境搭建(一),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template