Docker For Windows 安装配置及常用维护

时间:2022-12-07 10:58:08

1.简介

容器技术最早是从Linux 环境产生并持续发展,Windows 是在新的 CEO 上台后才开始转向开源并拥抱Linux。容器相对虚拟化更轻量,资源占用更少。

虽然Linux 环境有 Mono 组件可以实现 .net 架构的资源配置与使用,但是兼容性和复杂性也让快速搭建开发和测试环境的想法难以实现。

Windows 系统原生支撑容器是最简便的方法,可以在不更改 .net 开发和配置文件的前提下快速实现 IIS 组件的部署配置。

2.Docker安装与卸载

2.1.图形界面安装

下载Docker,一直下一步,安装好如下图: ​​https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe​

Docker For Windows 安装配置及常用维护

配置国内镜像加速站点(需要注册一个账号)

Docker For Windows 安装配置及常用维护

配置容器的网络,按实际需求来配置调整,这里用默认

Docker For Windows 安装配置及常用维护

查看版本输入:docker --version

Docker For Windows 安装配置及常用维护

2.2.PowerShell安装

参考微软官网
​​​https://docs.microsoft.com/en-us/virtualization/windowscontainers/quick-start/quick-start-windows-server​

Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Install-Package -Name docker -ProviderName DockerMsftProvider

提示出错了

Docker For Windows 安装配置及常用维护

手动下载docker-18-09-8.zip放到C:\Users\Administrator\AppData\Local\Temp\DockerMsftProvider,重新运行

Docker For Windows 安装配置及常用维护

重启服务器

Restart-Computer -Force

Docker For Windows 安装配置及常用维护

2.3.卸载Docker

Uninstall-Package -Name docker -ProviderName DockerMsftProvider 
Uninstall-Module -Name DockerMsftProvider

清理数据和组件

清除网络

Get-HNSNetwork | Remove-HNSNetwork

清除数据

Remove-Item "C:\ProgramData\Docker" -Recurse

清除容器和Hyper-V功能

Remove-WindowsFeature Containers 
Remove-WindowsFeature Hyper-V

重启系统

重启系统完成卸载和清理

Restart-Computer -Force

3.Docker 命令

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
--config string Location of client config files (default
"C:\\Users\\01Administrator\\.docker")
-c, --context string Name of the context to use to connect to the
daemon (overrides DOCKER_HOST env var and
default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"C:\\Users\\01Administrator\\.docker\\ca.pem")
--tlscert string Path to TLS certificate file (default
"C:\\Users\\01Administrator\\.docker\\cert.pem")
--tlskey string Path to TLS key file (default
"C:\\Users\\01Administrator\\.docker\\key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

3.1.搜索镜像

docker search iis

Docker For Windows 安装配置及常用维护

3.2.下载镜像

docker pull microsoft/aspnet

3.3.查看下载到镜像

docker images

Docker For Windows 安装配置及常用维护

3.4.启用容器

docker run -ti -d --restart=always --name web_test_mpostools -p 58043:8832 -v E:\Docker-IIS\web_test_mpostools:C:\inetpub\wwwroot -v  E:\Docker-IIS-Logs\web_test_mpostools:C:\inetpub\logs\LogFiles  microsoft/iis

注:把容器目录C:\inetpub\wwwroot映射到宿主机D:\Docker-IIS\My-IIS2\www这两个目录的内容是同步的

Docker For Windows 安装配置及常用维护

Docker For Windows 安装配置及常用维护

3.5.进入容器

docker exec -it cf0b277dbfde cmd

Docker For Windows 安装配置及常用维护

更改站点端口

进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd set site "Default Web Site" /bindings:http/*:8282:

新建站点

进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd.exe add site /name:"myjtestsite" /bindings:http/*:80: /physicalpath:"C:\inetpub\wwwroot"

新建应用池

进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd add apppool /name:myjtestpool /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated 
appcmd add apppool /name:api_web /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated

设置应用池管道托管模式

进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd set apppool "myjtestpool" /managedPipelineMode:Classic
appcmd set apppool "myjtestpool" /managedPipelineMode:Integrated

设置站点应用池

进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd set app "myjtestsite/" /applicationPool:"myjtestpool"

设置虚拟目录应用池

进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd set app "myjtestsite/api_web" /applicationPool:"api_web"

新建虚拟目录
进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd.exe add app /site.name:"Default Web Site" /path:"/api_web" /applicationPool:"api_web" /physicalpath:"C:\inetpub\wwwroot\API_Web" 
appcmd.exe add app /site.name:"Default Web Site" /path:"/newbm" /applicationPool:"newbm" /physicalpath:"C:\inetpub\wwwroot\newbm"

删除站点

进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd.exe delete site /site.name:"myjtestsite"

删除虚拟目录

进入容器,进入C:\Windows\System32\inetsrv>目录

appcmd.exe  delete app /app.name:"Default Web Site/newbm"

Docker For Windows 安装配置及常用维护

Docker For Windows 安装配置及常用维护

3.6.查看正在运行的容器

docker ps

Docker For Windows 安装配置及常用维护

3.7.查看所有容器

docker ps -a

Docker For Windows 安装配置及常用维护

3.8.停止容器运行

Docker stop 容器ID 容器ID2 ……

Docker For Windows 安装配置及常用维护

3.9.运行容器

Docker start ID ID2 

Docker For Windows 安装配置及常用维护

3.10.重启容器

Docker restart ID ID2 

Docker For Windows 安装配置及常用维护

3.11.从容器创建镜像

docker commit -a "WingZhang" -m "myjtestsite" 115b5e8fc0b7 myjtestsiteimages:v1

Docker For Windows 安装配置及常用维护

3.12.用创建的镜像启动容器

docker run -ti -d --name web_test_wms -p 58001:80 -v E:\Docker-IIS\web_test_wms:C:\inetpub\wwwroot -v E:\Docker-IIS-Logs\web_test_wms:C:\inetpub\logs\LogFiles myjtestsiteimages:v1

3.13.导出镜像

docker save -o d:\DockerImages\microsoftiis.tar microsoft/iis:latest

Docker For Windows 安装配置及常用维护

Docker For Windows 安装配置及常用维护

Docker For Windows 安装配置及常用维护

3.14.导入镜像

docker load --input D:\dockerimages\microsoftiis.tar

Docker For Windows 安装配置及常用维护

3.15.查看容器日志

docker logs --timestamps web_test_wms9097

Docker For Windows 安装配置及常用维护

3.16.查看容器CPU内存使用情况

docker stats

Docker For Windows 安装配置及常用维护

4.参考内容

容器指南之第一个容器    ​https://blog.51cto.com/waringid/5904849​

容器指南之容器架构及常用指令  ​​https://blog.51cto.com/waringid/5913145​

容器指南之日常运维 ​​https://blog.51cto.com/waringid/5914178​