網頁

2020/12/12

Docker 推送image到Docker Hub push build image to docker hub

Docker image push到Docker Hub的方法如下。

首先註冊一個Docker Hub帳戶並登入。在Docker Hub的Repositories中目前並沒有任何image。下面將把local build好的spring-boot-demo:0.0.1-SNAPSHOT image推送到Docker Hub。



例如在local有一個建置好的spring-boot-demo:0.0.1-SNAPSHOT image如下。

$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
spring-boot-demo                   0.0.1-SNAPSHOT      cc8fbb17a345        50 years ago        148MB

把image push到Docker Hub要先在命令列用docker login登入Docker Hub。

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: matthung0807
Password:
Login Succeeded

如果直接用docker push spring-boot-demo:0.0.1-SNAPSHOT會出現denied: requested access to the resource is denied錯誤訊息。

$ docker push spring-boot-demo:0.0.1-SNAPSHOT
The push refers to repository [docker.io/library/spring-boot-demo]
08ca6969bf61: Preparing
5cdfd70ab65d: Preparing
273175f8ad19: Preparing
012bb6144b38: Preparing
0a14b263fc87: Preparing
231bdbae9aea: Waiting
87c9f1582ca0: Waiting
7a5b9c0b4b14: Waiting
denied: requested access to the resource is denied

錯誤原因是推送的image名稱前要加上Docker Hub的帳號名稱(DOCKER ID),所以要用
docker tag <image_name[:tag]> <DOCKER_ID>/<image_name[:tag]>
在原本的image名稱前加上DOCKER_ID。

例如我的Docker Hub帳號是matthung0807,所以輸入以下把spring-boot-demo:0.0.1-SNAPSHOT修改為matthung0807/spring-boot-demo:0.0.1-SNAPSHOT
docker tag spring-boot-demo:0.0.1-SNAPSHOT matthung0807/spring-boot-demo:0.0.1-SNAPSHOT

$ docker tag spring-boot-demo:0.0.1-SNAPSHOT matthung0807/spring-boot-demo:0.0.1-SNAPSHOT

輸入後除了原來的spring-boot-demo 還會多一個修改名稱後的matthung0807/spring-boot-demo image。

$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
spring-boot-demo                   0.0.1-SNAPSHOT      cc8fbb17a345        50 years ago        148MB
matthung0807/spring-boot-demo      0.0.1-SNAPSHOT      cc8fbb17a345        50 years ago        148MB

接著就可以用docker push <DOCKER_ID>/<image_name>[:tag]把image推送到Docker Hub。所以這邊輸入:
$ docker push matthung0807/spring-boot-demo:0.0.1-SNAPSHOTmatthung0807/spring-boot-demo:0.0.1-SNAPSHOT push到Docker Hub。

$ docker push matthung0807/spring-boot-demo:0.0.1-SNAPSHOT
The push refers to repository [docker.io/matthung0807/spring-boot-demo]
08ca6969bf61: Pushed
5cdfd70ab65d: Pushed
273175f8ad19: Pushed
012bb6144b38: Pushed
0a14b263fc87: Pushed
231bdbae9aea: Pushed
87c9f1582ca0: Pushed
7a5b9c0b4b14: Pushed
0.0.1-SNAPSHOT: digest: sha256:e381f13d5db025554338f0ee9454a75f44792e984c721550710023fdb78bd6f9 size: 1999

到Docker Hub帳戶的Repositories重新整理可看到剛推送的matthung0807/spring-boot-demo



下面接著測試pull並運行剛推送的image。

輸入$ docker rmi matthung0807/spring-boot-demo:0.0.1-SNAPSHOT把local的matthung0807/spring-boot-demo:0.0.1-SNAPSHOT image刪除。

$ docker rmi matthung0807/spring-boot-demo:0.0.1-SNAPSHOT
Untagged: matthung0807/spring-boot-demo:0.0.1-SNAPSHOT
Untagged: matthung0807/spring-boot-demo@sha256:e381f13d5db025554338f0ee9454a75f44792e984c721550710023fdb78bd6f9

輸入docker run -p 8080:8080 --name demo matthung0807/spring-boot-demo:0.0.1-SNAPSHOT將Docker Hub的image pull下來並以容器執行。

$ docker run -p 8080:8080 --name demo matthung0807/spring-boot-demo:0.0.1-SNAPSHOT
Unable to find image 'matthung0807/spring-boot-demo:0.0.1-SNAPSHOT' locally
0.0.1-SNAPSHOT: Pulling from matthung0807/spring-boot-demo
Digest: sha256:e381f13d5db025554338f0ee9454a75f44792e984c721550710023fdb78bd6f9
Status: Downloaded newer image for matthung0807/spring-boot-demo:0.0.1-SNAPSHOT

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-12-12 16:14:08.417  INFO 1 --- [           main] c.a.d.DemoApplication                    : Starting DemoApplication on 415b9dc89cbf with PID 1 (/app/classes started by root in /)
2020-12-12 16:14:08.433  INFO 1 --- [           main] c.a.d.DemoApplication                    : No active profile set, falling back to default profiles: default
2020-12-12 16:14:09.784  INFO 1 --- [           main] o.s.b.w.e.t.TomcatWebServer              : Tomcat initialized with port(s): 8080 (http)
2020-12-12 16:14:09.826  INFO 1 --- [           main] o.a.c.c.StandardService                  : Starting service [Tomcat]
2020-12-12 16:14:09.826  INFO 1 --- [           main] o.a.c.c.StandardEngine                   : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-12-12 16:14:09.931  INFO 1 --- [           main] o.a.c.c.C.[.[.[/demo]                    : Initializing Spring embedded WebApplicationContext
2020-12-12 16:14:09.931  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1416 ms
2020-12-12 16:14:10.234  INFO 1 --- [           main] o.s.s.c.ThreadPoolTaskExecutor           : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-12 16:14:10.523  INFO 1 --- [           main] o.s.b.w.e.t.TomcatWebServer              : Tomcat started on port(s): 8080 (http) with context path '/demo'
2020-12-12 16:14:10.538  INFO 1 --- [           main] c.a.d.DemoApplication                    : Started DemoApplication in 2.897 seconds (JVM running for 4.089)


沒有留言:

張貼留言