- windows下安裝docker
參考:
https://www.jianshu.com/p/502b4ac536ef
https://docs.docker.com/install/windows/docker-ee/
強烈 推薦 使用windows server 2019
____
1、安裝
安裝Hyper-V
>Install-WindowsFeature -Name Hyper-V
安裝容器功能
>Install-WindowsFeature -Name containers
安裝完成后需重啟服務器
>Restart-computer
>Install-Module DockerMSFTProvider
提示:不受信任的存儲庫
(選"Y"確認)
>Install-Package Docker -ProviderName DockerMSFTProvider -Force
(可能會提示包驗證錯誤,安裝失敗,多試幾次即可)
提示:程序來自未標記為"受信任"的程序包源
(選"Y"確認)
@@#
注:上述安裝很慢的話或者經常出錯,可以在C:\Users\Administrator\AppData\Local\Temp\2\DockerMsftProvider下找到DockerDefault_DockerSearchIndex.json,其中有各版本的下 載地址 ,如19.03.2為https://dockermsft.blob.core.windows.net/dockercontainer/docker-19-03-2.zip,手工下載這個文件,放回到DockerMsftProvider目錄下,然后執行指令,出現提示后選擇"全部(A)"
Install-Package Docker -ProviderName DockerMSFTProvider
@@#
2、切換linux內核
默認使用windows內核,docker的鏡像只能基於nano和servercore,而且 依賴於操作系統的內核,1803,1809,1903等,當然運行一些早期程序會比較適合,推薦的方案是windows 內核的docker搭配一個linux 虛擬 機,在虛擬 機內另外運行一套docker。或者兩台服務器各自運行一種內核
如果一定想使用LINUX內核,參考:https://docs.microsoft.com/zh-cn/virtualization/windowscontainers/quick-start/using-insider-container-images
下列代碼以管理員身份 運行powershell逐行復制執行,確保每個指令有明確的輸出,如提示被操作 的版本號 。
Stop-Service docker
Uninstall-Package docker
Uninstall-Module DockerMsftProvider
Uninstall-Module DockerProvider
#有必要的話上述 代碼多執行一遍
Install-Module -Name DockerProvider -Repository PSGallery
Install-Package -Name docker -ProviderName DockerProvider -RequiredVersion Preview
# 或Install-Package -Name docker -ProviderName DockerProvider -RequiredVersion 17.10.0-ee-preview-2
Restart-Computer
切換到Linux內核容器
>[Environment]::SetEnvironmentVariable("LCOW_SUPPORTED", "1", "Machine")
>Restart-Service Docker
切換到Windows內核容器
>[Environment]::SetEnvironmentVariable("LCOW_SUPPORTED", $null, "Machine")
>Restart-Service Docker
@@#
切換后重啟docker,使用docker info
PS C:\Users\Administrator> docker version
Client:
Version: 17.10.0-ee-preview-3
API version: 1.33
Go version: go1.8.4
Git commit: 1649af8
Built: Fri Oct 6 17:52:28 2017
OS/Arch: windows/amd64
Server:
Version: 17.10.0-ee-preview-3
API version: 1.34 (minimum version 1.24)
Go version: go1.8.4
Git commit: b8571fd
Built: Fri Oct 6 18:01:48 2017
OS/Arch: windows/amd64
Experimental: true
PS C:\Users\Administrator>
@@#
3、修改docker配置
啟動docker服務后,會生成docker目錄 ,文件為C:\ProgramData\docker\config\ daemon.json
{
"data-root":"d:\\Docker",
"registry-mirrors": [ "https://i1el1i0w.mirror.aliyuncs.com","https://registry.docker-cn.com"],
"experimental":true
}
@@#
4、windows內核模式下常用的鏡像
拉取非常慢
Windows 2016
docker pull mcr.microsoft.com/windows/servercore:10.0.14393.3204-amd64
Windows2019 提示版本不對就換TAG
docker pull mcr.microsoft.com/dotnet/framework/sdk:4.8
docker pull mcr.microsoft.com/dotnet/core/runtime:2.2-nanoserver-1809
docker pull mcr.microsoft.com/dotnet/core/runtime:2.2-nanoserver-1903
docker pull mcr.microsoft.com/dotnet/core/sdk:2.2.402-nanoserver-1903
docker pull mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-1903
docker pull mcr.microsoft.com/dotnet/core/aspnet:2.2-nanoserver-1809
docker pull mcr.microsoft.com/windows/servercore:ltsc2019-amd64
-
Linux下安裝docker
- 安裝centos7 mini
安裝 docker,參考 https://www.cnblogs.com/qgc1995/p/9553572.html
yum install docker-ce
#安裝 19.0.3
@@#
- 拉取測試鏡像
docker pull hello-world
[root@docker ~]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
@@#
拉取鏡像 https://hub.docker.com/publishers/microsoftowner
參考https://www.bbsmax.com/A/RnJW1E0Bdq/
docker pull mcr.microsoft.com/dotnet/core/sdk:2.2
docker images
docker run -itd --rm -p 80:80 --name core_test mcr.microsoft.com/dotnet/core/sdk:2.2
docker ps
docker attach core_test
# 刪除tag docker rm core_test 血和淚告訴我們,不要輕易刪除tag,這就是一個實例,其中所有文件變動都會刪除,如果隨后又同名run的話,可以跑路了
# 刪除文件 sudo docker rmi imageid
@@#
- 容器持久化與移除
- 進入docker執行命令
或使用
docker inspect -f {{.State.Pid}} 5a406185953b
nsenter --target 20190 --mount --uts --ipc --net --pid
@@#
- Docker互訪
參考
https://www.cnblogs.com/shenh/p/9714547.html
docker network create LanDocker
docker network ls
docker run -itd --rm --name redis --network LanDocker --network-alias redis redis
@@#
- 在vs2017中發布應用
https://www.cnblogs.com/rufus-hua/p/6742836.html
https://blog.csdn.net/sd7o95o/article/details/80809734
docker build -t test:1.0 . # 注意最后有一個小點.
docker run -itd -p 80:80 --name test test:1.0 #在瀏覽器中打開
@@#
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS base
WORKDIR /app
copy ./test /app
EXPOSE 80
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
@@#
可能發生的錯誤
防火牆操作 參考 https://blog.csdn.net/u013514928/article/details/80411110
firewall-cmd --zone=public --add-port=3410-3420/tcp --permanent
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=5880/udp --permanent
firewall-cmd --zone=public --add-port=5881/tcp --permanent
firewall-cmd --zone=public --add-port=5000-5005/tcp --permanent
firewall-cmd --zone=public --add-port=7200/tcp --permanent
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd –reload
firewall-cmd --permanent --list-port
@@#
附錄windows docker常用的管理命令
https://wenku.baidu.com/view/868c47c36e1aff00bed5b9f3f90f76c661374ce7.html (推薦看一哈通信原理)
1. 查看docker信息(version、info)
# 查看docker版本
docker version
# 顯示docker系統的信息
docker info
2. 對image的操作(search、pull、images、rmi、history)
# 檢索image
docker search image_name
# 下載image
docker pull image_name
#列出鏡像列表; -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs
docker images
# 刪除一個或者多個鏡像; -f, --force=false Force; --no-prune=false Do not delete untagged parents
docker rmi image_name
# 顯示一個鏡像的歷史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs docker his
3. 啟動容器(run)
docker容器可以理解為在沙盒中運行的進程。這個沙盒包 含了該進程運行所必須的資源,包括文件系統、系統類庫、shell 環境等等。但這個沙盒默認是不會運行任何程序的。你需要在沙盒中運行一個進程來啟動某一個容器。這個進程是該容器的唯一進程,所以當該進程結束的時候,容 器也會完全的停止。
# 在容器中運行"echo"命令,輸出"hello word"
docker run image_name echo "hello word"
# 交互式進入容器中
docker run -i -t image_name /bin/bash
# 在容器中安裝新的程序
docker run image_name apt-get install -y app_name
# 在一次進剛才進入的容器
docker exec -i -t [容器ID]
Note: 在執行apt-get 命令的時候,要帶上-y參數。如果不指定-y參數的話,apt-get命令會進入交互模式,需要用戶輸入命令來進行確認,但在docker環境中是無法響 應這種交互的。apt-get 命令執行完畢之后,容器就會停止,但對容器的改動不會丟失。
4. 查看容器(ps)
# 列出當前所有正在運行的container
docker ps
# 列出所有的container
docker ps -a
# 列出最近一次啟動的container
docker ps -l
5. 保存對容器的修改(commit)
當你對某一個容器做了修改之后(通過在容器中運行某一個命令),可以把對容器的修改保存下來,這樣下次可以從保存后的最新狀態運行該容器。
# 保存對容器的修改; -a, --author="" Author; -m, --message="" Commit message
docker commit ID new_image_name
6. 對容器的操作(rm、stop、start、kill、logs、diff、top、cp、restart、attach)
# 刪除所有容器
docker rm `docker ps -a -q`
# 刪除單個容器; -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container
docker rm Name/ID
# 停止、啟動、殺死一個容器
docker stop Name/ID
docker start Name/ID
docker kill Name/ID
# 從一個容器中取日志; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps
docker logs Name/ID
# 列出一個容器里面被改變的文件或者目錄,list列表會顯示出三種事件,A 增加的,D 刪除的,C 被改變的
docker diff Name/ID
# 顯示一個運行的容器里面的進程信息
docker top Name/ID
# 從容器里面拷貝文件/目錄到本地一個路徑
docker cp Name:/container_path to_path
docker cp ID:/container_path to_path
# 重啟一個正在運行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10
docker restart Name/ID
# 附加到一個運行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process
docker attach ID
Note: attach命令允許你查看或者影響一個運行的容器。你可以在同一時間attach同一個容器。你也可以從一個容器中脫離出來,是從CTRL-C。
7. 保存和加載鏡像(save、load)
當需要把一台機器上的鏡像遷移到另一台機器的時候,需要保存鏡像與加載鏡像。
# 保存鏡像到一個tar包; -o, --output="" Write to an file
docker save image_name -o file_path
# 加載一個tar包格式的鏡像; -i, --input="" Read from a tar archive file
docker load -i file_path
# 機器a
docker save image_name > /home/save.tar
# 使用scp將save.tar拷到機器b上,然后:
docker load < /home/save.tar
8、 登錄registry server(login)
# 登陸registry server; -e, --email="" Email; -p, --password="" Password; -u, --username="" Usernamedocker login
9. 發布image(push)
# 發布docker鏡像
docker push new_image_name
10. 根據Dockerfile 構建出一個容器
#build
--no-cache=false Do not use cache when building the image
-q, --quiet=false Suppress the verbose output generated by the containers
--rm=true Remove intermediate containers after a successful build
-t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success
docker build -t image_name Dockerfile_path
————————————————
版權聲明:本文為CSDN博主「天府雲創」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/enweitech/article/details/80689387
