docker-hello-world

构建自己镜像步骤

  1. 安装docker
  2. 编写项目
  3. 编写dockerfile
  4. 构建镜像
  5. 运行容器
  6. push镜像到hub

安装docker

编写项目

项目目录

1
2
3
4
5
6
7
8
9
10
➜  ehandbook ls -alh
total 38800
drwxr-xr-x 8 weimo staff 256B 7 22 17:22 .
drwx------@ 79 weimo staff 2.5K 7 22 12:42 ..
-rw-r--r--@ 1 weimo staff 6.0K 7 22 17:06 .DS_Store
-rw-r--r-- 1 weimo staff 19M 7 22 17:08 building-an-effective-dev-portfolio.pdf
-rw-r--r-- 1 weimo staff 60K 7 22 17:08 cat.png
-rw-r--r-- 1 weimo staff 149B 7 22 17:37 docker-compose.yaml
-rw-r--r-- 1 weimo staff 122B 7 22 17:24 dockerfile
-rw-r--r-- 1 weimo staff 4.4K 7 22 17:37 ehandbook.js

编写docker-compose.yaml

1
2
3
4
5
6
7
8
9
10
11
version: "0.0.1"

services:
app:
build: ./
ports:
- 8888:8888
volumes:
- ./:/app
environment:
- TZ=Asia/Shanghai

编写dockerfile

1
2
3
4
5
FROM node:16-alpine
ADD . ./app
WORKDIR /app
# RUN npm install
CMD pwd && ls && echo "启动服务~" && node ehandbook.js

构建镜像

1
➜  ehandbook docker build -t ehandbook:v0.0.1 .

运行容器

1
➜  ehandbook docker run -p 8888:8888 --name ehandbookcon ehandbook:v0.0.1

push镜像到hub

首先要登录(梯子

1
2
3
4
5
6
7
➜  ehandbook docker login
Log in with your Docker ID or email address 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.
You can log in with your password or a Personal Access Token (PAT). Using a limited-scope PAT grants better security and is required for organizations using SSO. Learn more at https://docs.docker.com/go/access-tokens/

Username: 你的用户名
Password: 密码直接回车
Login Succeeded
1
2
3
4
5
6
7
8
# 对本地镜像要打一个tag
➜ ehandbook docker tag ehandbook:v0.0.1 weimo9/ehandbook:v0.0.1
# 检查一下
➜ ehandbook docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
weimo9/ehandbook v0.0.1 df836bb667c9 19 minutes ago 136MB
ehandbook v0.0.1 df836bb667c9 19 minutes ago 136MB
weimo9/koa-test latest a87090af61aa 22 months ago 117MB

直接挂梯子push

1
2
3
4
5
6
7
8
➜  ehandbook docker push weimo9/ehandbook:v0.0.1 
The push refers to repository [docker.io/weimo9/ehandbook]
5f70bf18a086: Pushed
15111b888f32: Pushing 19.86MB
fabb6e7cc132: Retrying in 5 seconds
46e75cb9d01b: Pushed
525e899ff770: Pushing 69.58MB/101.1MB
b2191e2be29d: Retrying in 7 seconds

参考

https://github.com/miloweimo/koa-test.git

https://hub.docker.com/repository/docker/weimo9/ehandbook/general

作者

Wei Mo

发布于

2022-10-20

更新于

2024-07-22

许可协议

评论