johnhao
johnhao
发布于 2025-12-18 / 11 阅读
0

Docker-自定义镜像发布到DockerHub

1、现在开始构建Docker镜像,使用指定的镜像名称和标签

# 先进入到DockerFile项目目录文件下
cd ~/../Documents/trae_projects/test

# 指定的镜像名称和标签
docker build -t test:1.0.0 .

# 指定在特定架构平台能运行的镜像
docker buildx build --platform linux/amd64 -t test:1.0.0 -t coderjohnhao/test:1.0.0 .

完成后终端会打印日志,如下

[+] Building 0.3s (12/12) FINISHED      docker:desktop-linux
 => [internal] load build definition from Dockerfile    0.0s
 => => transferring dockerfile: 840B                    0.0s
 => [internal] load metadata for m.daocloud.io/docker.  0.3s
 => [internal] load .dockerignore                       0.0s
 => => transferring context: 400B                       0.0s
 => [1/7] FROM m.daocloud.io/docker.io/library/node:18  0.0s
 => [internal] load build context                       0.0s
 => => transferring context: 824B                       0.0s
 => CACHED [2/7] WORKDIR /app                           0.0s
 => CACHED [3/7] COPY package*.json ./                  0.0s
 => CACHED [4/7] RUN npm ci --only=production && npm c  0.0s
 => CACHED [5/7] COPY . .                               0.0s
 => CACHED [6/7] RUN addgroup -g 1001 -S nodejs &&      0.0s
 => CACHED [7/7] RUN chown -R fileRocket:nodejs /app    0.0s
 => exporting to image                                  0.0s
 => => exporting layers                                 0.0s
 => => writing image sha256:c80a008f60a97277885aebed73  0.0s
 => => naming to docker.io/library/jh-file-rocket:1.0.  0.0s

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/4netdxlkyka8ob7nl8w0omdvf

What's next:
    View a summary of image vulnerabilities and recommendations → docker scout quickview

2、构建成功,现在为镜像添加docker hub仓库标签。

docker tag test:1.0.0 coderjohnhao/test:1.0.0 

3、现在尝试将镜像推送到Docker Hub。

docker push coderjohnhao/test:1.0.0 

终端打印日志,如下

The push refers to repository [docker.io/coderjohnhao/test]
ab6e32f647e8: Pushed 
5cdf62be22c6: Pushed 
1382fb8756b6: Pushed 
789037214cd1: Pushed 
694b75c4bb93: Pushed 
50a120c80d2a: Pushed 
50cd4dbd375f: Pushed 
37e456418fa3: Pushed 
105c493da353: Pushed 
a16e98724c05: Pushed 
1.0.0: digest: sha256:f4162a1a342b3c12e89042edcf38587ec190ad95665b1958cfeccd40210336ba size: 2411

4、使用

  • 终端直接运行

docker pull coderjohnhao/test:1.0.0
docker run -d -p 3000:3000 coderjohnhao/test:1.0.0
  • 使用docker-compose.yml

version: '3.9'
services:
    docker-test:
        image: 'coderjohnhao/test:1.0.3'
        ports:
            - '3000:3000'

在docker-compose.yml文件夹目录下,执行

docker compose up -d --build