Mục tiêu bài học:
- Đăng nhập và tạo tài khoản Docker Hub.
- Build image từ Dockerfile.
- Tag image theo commit hash và
latest. - Push image lên Docker Hub.
Quy trình các bước
1. Tạo tài khoản Docker Hub
- Truy cập: [https://hub.docker.com](https://hub.docker.com/)
- Đăng ký tài khoản và đăng nhập.
2. Tạo Dockerfile mới
```bash
cd docker_images/
mkdir dockerhub
cd dockerhub
vi Dockerfile
```
Paste nội dung Dockerfile từ bài Multi-Stage Builds.
3. Clone source code
```bash
git clone https://github.com/linuxacademy/weather-app.git
cd weather-app/src
git log -1 --pretty=%h # lấy commit hash
cd ../.. # về lại thư mục gốc
```
4. Build image
```bash
docker image build -t linuxacademy/weather-app:<commit-hash> --build-arg VERSION=1.5 .
```
5. Tag image chuẩn bị đẩy lên Docker Hub
```bash
docker image tag linuxacademy/weather-app:<commit-hash> <dockerhub-username>/weather-app:<commit-hash>
```
6. Push lên Docker Hub
```bash
docker login # nếu chưa đăng nhập
docker image push <dockerhub-username>/weather-app:<commit-hash>
```
7. Tạo thêm tag latest cho cùng image
```bash
docker image tag <dockerhub-username>/weather-app:<commit-hash> <dockerhub-username>/weather-app:latest
docker image push <dockerhub-username>/weather-app:latest
```
Kết quả trên Docker Hub
- Có 2 tag:
- Commit hash: phiên bản xác định source code tương ứng.
- Latest: để xác định đây là phiên bản mới nhất.
Tổng kết lệnh
| Lệnh | Mục đích |
|---|---|
docker login | Đăng nhập Docker Hub |
docker image build | Build image từ Dockerfile |
docker image tag | Gán thêm tag mới cho image |
docker image push | Push image lên Docker Hub |