Windows下Docker-desktop

1、在Windows下安装Docker-desktop

下载:

https://github.com/tech-shrimp/docker_installer/releases/download/latest/docker_desktop_installer_windows_x86_64.exe

2、安装:(先安装wsl)

start /w "" "docker_desktop_installer_windows_x86_64.exe" install --installation-dir=D:\Docker

3、安装完成后,重启启动没反应,这是因为需要安装wsl

4、安装wsl:cmd下操作

<!-- 下载 wsl 安装包 -->
curl https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi -O wsl_update_x64.msi 
<!-- 安装 wsl 安装包 -->
msiexec .\wsl_update_x64.msi
<!-- 将wsl2设置为默认版本 -->
wsl --set-default-version 2

如果启动docker有报错,执行如下:

C:\Users\admin>wsl --update
正在安装: 适用于 Linux 的 Windows 子系统
灾难性故障

C:\Users\admin>wsl --update
正在安装: 适用于 Linux 的 Windows 子系统
已安装 适用于 Linux 的 Windows 子系统。

C:\Users\admin>wsl --set-default-version 2
有关与 WSL 2 关键区别的信息,请访问 https://aka.ms/wsl2

操作成功完成。

 

5、然后Docker-desktop做一些相应配置:

 

 

{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "default-runtime": "nvidia",
  "experimental": false,
  "runtimes": {
    "nvidia": {
      "path": "nvidia-container-runtime",
      "runtimeArgs": []
    }
  }
}

 然后restart docker desktop,如果启动报错,可以执行wsl更新:C:\Users\Administrator\Downloads>wsl --update

 

C:\WINDOWS\system32>docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

C:\WINDOWS\system32>docker pull registry.cn-hangzhou.aliyuncs.com/jsonhc/nginx:1.15
1.15: Pulling from jsonhc/nginx
743f2d6c1f65: Pull complete
6bfc4ec4420a: Pull complete
688a776db95f: Pull complete
Digest: sha256:e770165fef9e36b990882a4083d8ccf5e29e469a8609bb6b2e3b47d9510e2c8d
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/jsonhc/nginx:1.15
registry.cn-hangzhou.aliyuncs.com/jsonhc/nginx:1.15

 启动镜像后,可以看见启动的镜像服务:

C:\WINDOWS\system32>docker ps
CONTAINER ID   IMAGE                                                 COMMAND                   CREATED          STATUS          PORTS     NAMES
dea6ea340ca6   registry.cn-hangzhou.aliyuncs.com/jsonhc/nginx:1.15   "nginx -g 'daemon of…"   36 seconds ago   Up 34 seconds   80/tcp    sharp_vaughan

 然后关闭防火墙:

 浏览器进行访问:

 

6、windows下docker安装gitlab-runner:

C:\WINDOWS\system32>docker pull registry.cn-hangzhou.aliyuncs.com/jsonhc/gitlab-runner:v15.6.0
v15.6.0: Pulling from jsonhc/gitlab-runner
eaead16dc43b: Pull complete
263b28da2553: Pull complete
fbd0cb936fa1: Pull complete
Digest: sha256:844a3d513f692832359a95643392d2a9907c1a126d52206a5a41beae20fdcfca
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/jsonhc/gitlab-runner:v15.6.0
registry.cn-hangzhou.aliyuncs.com/jsonhc/gitlab-runner:v15.6.0

 挂载情况:

 然后进行gitlab-runner cicd:

 

 查看挂载目录:

需要将docker.sock进行修改:

注意虽然在windows wsl2下没有/var/run/docker.sock:/var/run/docker.sock,但似乎它已自动确定为Windows环境中Docker Machine的路径

 

 继续执行job:

 这个报错表示Pulling docker image node:16.13.0-alpine失败,这里进行修改:

stages:
  - build
  - deploy

cache:
  paths:
    - ./dist

build:
  image: registry.cn-hangzhou.aliyuncs.com/jsonhc/node:16.13.0-alpine
  stage: build
  script:
    - npm config set registry https://registry.npmmirror.com
    - npm install
    - npm run build
  only:
    - main
  tags:
    - windows
  artifacts:
    when: on_success
    paths:
      - ./dist

deploy:
  image: docker:stable
  stage: deploy
  script:
    - ls -l && pwd
    - docker build -t test-web .
  only:
    - main
  tags:
    - windows

将官方镜像更换为阿里云镜像

 可见build部分成功

 deploy这个job执行,出现docker镜像pull失败,更换镜像

 

 

 两个stage执行成功,查看镜像:

 然后启动,验证下:

 访问正常,后续直接可以将docker run开启这个应用追加到yml文件中

 

针对gitlab项目下不同目录的更新触发不同目录的构建:

实现原理:可以在.gitlab-ci.yml文件中使用only关键字来指定需要监控的目录

 

stages:
  - build-frontend
  - build-web-test
 
build-frontend:
  image: registry.cn-hangzhou.aliyuncs.com/jsonhc/docker:stable
  stage: build-frontend
  script:
    - echo "Building frontend directory..."
    - cd frontend
    - docker build -t frontend .
  only:
    changes:
      - frontend/**/*         # 针对当前frontend目录进行监控变化
    refs:
      - main
  tags:
    - windows

build-web-test:
  image: registry.cn-hangzhou.aliyuncs.com/jsonhc/docker:stable
  stage: build-web-test
  script:
    - echo "Building web-test directory..."
    - cd web-test
    - docker build -t web-test:$CI_COMMIT_SHA .
  only:
    changes:
      - web-test/**/*         # 针对当前web-test目录进行监控变化
    refs:
      - main
  tags:
    - windows

 job执行过程:

 

 

在Windows环境下安装gitlab:

1、在Docker-desktop环境下下载gitlab镜像:

d:\soft>docker pull gitlab/gitlab-ce:latest
latest: Pulling from gitlab/gitlab-ce
3713021b0277: Pull complete
18fb9b360a6c: Pull complete
c7e4f5c8d5dd: Pull complete
1ae68faf8cbf: Pull complete
40c87fb60e9d: Pull complete
1329d3b69d77: Pull complete
58a911bce43c: Pull complete
1a7a4bf6f4d7: Pull complete
97f870522a99: Pull complete
Digest: sha256:9a5dff8bec16a32551b2069de505be134b30024e022ee72f30d0eddb9cccac95
Status: Downloaded newer image for gitlab/gitlab-ce:latest
docker.io/gitlab/gitlab-ce:latest

2、创建需要挂载的目录:D:\srv\gitlab

3、使用如下命令进行创建gitlab:

d:\soft>docker run -d --hostname 192.168.3.4 --env GITLAB_OMNIBUS_CONFIG="external_url 'http://192.168.3.4'" --publish 443:443 --publish 80:80 --publish 22:22 --name gitlab --restart always --volume D:\srv\gitlab\config:/etc/gitlab --volume D:\srv\gitlab\logs:/var/log/gitlab --volume D:\srv\gitlab\data:/var/opt/gitlab  gitlab/gitlab-ce:latest
0917cb4bf4e5eb533599ef4d2b3206fa1f0a16a833ab18529dec5b2929f6f82e

上面的ip是ipconfig查看本机的IP

4、通过Docker-desktop进行查看gitlab:

 

 大概7-8分钟才初始完成,需要等一下,然后通过http://192.168.3.4进行访问,用户root,密码获取:

 5、创建project,然后进行下载和push:

 6、需要将Windows生成的公钥配置到gitlab:

 7、Windows环境下下载git命令然后git clone代码:

D:\gitlab-win>git clone git@192.168.3.4:root/devops.git
Cloning into 'devops'...
The authenticity of host '192.168.3.4 (192.168.3.4)' can't be established.
ED25519 key fingerprint is SHA256:vDrGZJnosIDqHSnWkuK+mPzw04pytOatPVYi5EAW7RI.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.3.4' (ED25519) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (3/3), done.

8、随便创建一个文件进行push:

D:\gitlab-win\devops>dir
 驱动器 D 中的卷是 新加卷
 卷的序列号是 66D9-0C12

 D:\gitlab-win\devops 的目录

2024/08/04  19:33    <DIR>          .
2024/08/04  19:33    <DIR>          ..
2024/08/04  19:32             6,245 README.md
2024/08/04  19:33                 0 test.txt
               2 个文件          6,245 字节
               2 个目录 23,903,608,832 可用字节

D:\gitlab-win\devops>git add .

D:\gitlab-win\devops>git commit -m "update"
[main dbd2bbf] update
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

D:\gitlab-win\devops>git push origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 20 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 262 bytes | 262.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To 192.168.3.4:root/devops.git
   173331e..dbd2bbf  main -> main

 

posted on 2024-07-04 22:13  wadeson  阅读(205)  评论(0编辑  收藏  举报