|
2 | 2 |
|
3 | 3 | ### Pushing Docker Images on GitHub Packages
|
4 | 4 |
|
5 |
| -* Authorize your local machine with the GitHub Package registry |
6 |
| - |
7 |
| -``` |
8 |
| -$ echo "GitHub Token" | docker login docker.pkg.github.com --username [GitHub Username] --password-stdin |
| 5 | +* `Dockerfile` |
| 6 | + |
| 7 | +```dockerfile |
| 8 | +FROM node:14-alpine |
| 9 | +WORKDIR /usr/src/app |
| 10 | +COPY . . |
| 11 | +RUN npm install |
| 12 | +EXPOSE 3000 |
| 13 | +CMD [ "npm", "start" ] |
9 | 14 | ```
|
10 | 15 |
|
11 |
| -* Tag the local image with the GitHub Repository name |
| 16 | +- `.dockerignore` |
12 | 17 |
|
13 | 18 | ```
|
14 |
| -$ docker tag [local image name] docker.pkg.github.com/[GitHub username]/[Repository name]/[Image name]:[Version] |
| 19 | +node_modules/ |
15 | 20 | ```
|
16 | 21 |
|
17 |
| -* Push the local image to remote GitHub registry |
18 |
| - |
19 |
| -``` |
20 |
| -$ docker push docker.pkg.github.com/[GitHub username]/[Repository name]/[Image name]:[Version] |
21 |
| -``` |
22 |
| - |
23 |
| -### Using the Docker Images locally |
24 |
| - |
25 |
| -* Pull the remote docker image locally |
26 |
| - |
27 |
| -``` |
28 |
| -$ docker pull docker.pkg.github.com/[Username]/[Repository Name]/[Image name]:[version] |
29 |
| -``` |
30 |
| - |
31 |
| -* Check the Docker Image ID |
32 |
| - |
33 |
| -``` |
34 |
| -$ docker images |
35 |
| -``` |
36 |
| - |
37 |
| -* Launch a docker container using the pulled docker image |
38 |
| - |
39 |
| -``` |
40 |
| -$ docker run -it [Image ID] |
| 22 | +- `Docker Build And Push Actions` |
| 23 | + |
| 24 | +```yaml |
| 25 | +name: Create and publish a Docker image |
| 26 | + |
| 27 | +on: push |
| 28 | + |
| 29 | +env: |
| 30 | + REGISTRY: ghcr.io |
| 31 | + IMAGE_NAME: ${{ github.repository }} |
| 32 | + |
| 33 | +jobs: |
| 34 | + build-and-push-image: |
| 35 | + runs-on: ubuntu-latest |
| 36 | + permissions: |
| 37 | + contents: read |
| 38 | + packages: write |
| 39 | + |
| 40 | + steps: |
| 41 | + - name: Checkout repository |
| 42 | + uses: actions/checkout@v3 |
| 43 | + |
| 44 | + - name: Log in to the Container registry |
| 45 | + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 |
| 46 | + with: |
| 47 | + registry: ${{ env.REGISTRY }} |
| 48 | + username: ${{ github.actor }} |
| 49 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 50 | + |
| 51 | + - name: Extract metadata (tags, labels) for Docker |
| 52 | + id: meta |
| 53 | + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 |
| 54 | + with: |
| 55 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 56 | + |
| 57 | + - name: Build and push Docker image |
| 58 | + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 |
| 59 | + with: |
| 60 | + context: . |
| 61 | + push: true |
| 62 | + tags: ${{ steps.meta.outputs.tags }} |
| 63 | + labels: ${{ steps.meta.outputs.labels }} |
41 | 64 | ```
|
42 | 65 |
|
43 | 66 | ## Important Links and References
|
|
0 commit comments