Frecuentemente publicamos actualizaciones de nuestra documentación. Es posible que la traducción de esta página esté en curso. Para conocer la información más actual, visita la documentación en inglés. Si existe un problema con las traducciones en esta página, por favor infórmanos.
Versión del artículo: Free, Pro, and Team
Versión del artículo: Free, Pro, and Team

Publishing Docker images

You can publish Docker images to a registry, such as Docker Hub or Paquetes de GitHub, as part of your continuous integration (CI) workflow.

GitHub Actions se encuentra disponible con GitHub Free, GitHub Pro, GitHub Free para organizaciones, GitHub Team, GitHub Enterprise Cloud, y GitHub One. GitHub Actions no se encuentra disponible para repositorios privados que pertenezcan a cuentas que utilicen planes tradicionales por repositorio. Para obtener más información, consulta la sección "Productos de GitHub".

En este artículo

Introduction

This guide shows you how to create a workflow that performs a Docker build, and then publishes Docker images to Docker Hub or Paquetes de GitHub. With a single workflow, you can publish images to a single registry or to multiple registries.

Note: If you want to push to another third-party Docker registry, the example in the "Publishing images to Paquetes de GitHub" section can serve as a good template.

Prerequisites

We recommend that you have a basic understanding of workflow configuration options and how to create a workflow file. For more information, see "Learn GitHub Actions."

You might also find it helpful to have a basic understanding of the following:

About image configuration

This guide assumes that you have a complete definition for a Docker image stored in a GitHub repository. For example, your repository must contain a Dockerfile, and any other files needed to perform a Docker build to create an image.

In this guide, we will use the Docker build-push-action action to build the Docker image and push it to one or more Docker registries. For more information, see build-push-action.

Publishing images to Docker Hub

Cada vez que creas un nuevo lanzamiento en GitHub, puedes activar un flujo de trabajo para publicar tu imagen. El flujo de trabajo en el ejemplo siguiente se ejecuta cuando el se activa el evento release con el tipo de actividad created. Para obtener más información en el evento release, consulta la sección"Eventos que activan flujos de trabajo.

In the example workflow below, we use the Docker build-push-action action to build the Docker image and, if the build succeeds, push the built image to Docker Hub.

To push to Docker Hub, you will need to have a Docker Hub account, and have a Docker Hub repository created. For more information, see "Share images on Docker Hub" in the Docker documentation.

The build-push-action options required for Docker Hub are:

  • username and password: This is your Docker Hub username and password. We recommend storing your Docker Hub username and password as encrypted secrets in your GitHub repository so they aren't exposed in your workflow file. For more information, see "Creating and using encrypted secrets."
  • repository: Your Docker Hub repository in the format DOCKER-HUB-NAMESPACE/DOCKER-HUB-REPOSITORY.
name: Publish Docker image
on:
  release:
    types: [published]
jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v2
      - name: Push to Docker Hub
        uses: docker/build-push-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          repository: my-docker-hub-namespace/my-docker-hub-repository
          tag_with_ref: true

El flujo de trabajo anterior verifica el repositorio GitHub y utiliza la acción build-push-action para construir y subir la imagen de Docker. Éste configura la opción tag_with_ref de build-push-action para que etiquete automáticamente la imagen construida de Docker con la referencia de Git del evento del flujo de trabajo. Este flujo de trabajo se desencadena cuando se publica un lanzamiento de GitHub, para que la referencia sea la etiqueta de Git para el lanzamiento.

Publishing images to Paquetes de GitHub

Cada vez que creas un nuevo lanzamiento en GitHub, puedes activar un flujo de trabajo para publicar tu imagen. El flujo de trabajo en el ejemplo siguiente se ejecuta cuando el se activa el evento release con el tipo de actividad created. Para obtener más información en el evento release, consulta la sección"Eventos que activan flujos de trabajo.

In the example workflow below, we use the Docker build-push-action action to build the Docker image, and if the build succeeds, push the built image to Paquetes de GitHub.

The build-push-action options required for Paquetes de GitHub are:

  • username: You can use the ${{ github.actor }} context to automatically use the username of the user that triggered the workflow run. For more information, see "Context and expression syntax for GitHub Actions."
  • password: You can use the automatically-generated GITHUB_TOKEN secret for the password. For more information, see "Authenticating with the GITHUB_TOKEN."
  • registry: Must be set to docker.pkg.github.com.
  • repository: Must be set in the format OWNER/REPOSITORY/IMAGE_NAME. For example, for an image named octo-image stored on GitHub at http://github.com/octo-org/octo-repo, the repository option should be set to octo-org/octo-repo/octo-image.
name: Publish Docker image
on:
  release:
    types: [published]
jobs:
  push_to_registry:
    name: Push Docker image to GitHub Packages
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v2
      - name: Push to GitHub Packages
        uses: docker/build-push-action@v1
        with:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          repository: my-org/my-repo/my-image
          tag_with_ref: true

El flujo de trabajo anterior verifica el repositorio GitHub y utiliza la acción build-push-action para construir y subir la imagen de Docker. Éste configura la opción tag_with_ref de build-push-action para que etiquete automáticamente la imagen construida de Docker con la referencia de Git del evento del flujo de trabajo. Este flujo de trabajo se desencadena cuando se publica un lanzamiento de GitHub, para que la referencia sea la etiqueta de Git para el lanzamiento.

Publishing images to Docker Hub and Paquetes de GitHub

In a single workflow, you can publish your Docker image to multiple registries by using the build-push-action action for each registry.

The following example workflow uses the build-push-action steps from the previous sections ("Publishing images to Docker Hub" and "Publishing images to Paquetes de GitHub") to create a single workflow that pushes to both registries.

name: Publish Docker image
on:
  release:
    types: [published]
jobs:
  push_to_registries:
    name: Push Docker image to multiple registries
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v2
      - name: Push to Docker Hub
        uses: docker/build-push-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          repository: my-docker-hub-namespace/my-docker-hub-repository
          tag_with_ref: true
      - name: Push to GitHub Packages
        uses: docker/build-push-action@v1
        with:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          repository: my-org/my-repo/my-image
          tag_with_ref: true

The above workflow checks out the GitHub repository, and uses the build-push-action action twice to build and push the Docker image to Docker Hub and Paquetes de GitHub. For both steps, it sets the build-push-action option tag_with_ref to automatically tag the built Docker image with the Git reference of the workflow event. This workflow is triggered on publishing a GitHub release, so the reference for both registries will be the Git tag for the release.

¿Te ayudó este documento?

Privacy policy

Help us make these docs great!

All GitHub docs are open source. See something that's wrong or unclear? Submit a pull request.

Make a contribution

O, learn how to contribute.