{"id":584,"date":"2025-08-02T09:25:25","date_gmt":"2025-08-02T07:25:25","guid":{"rendered":"https:\/\/www.oferto.io\/?p=584"},"modified":"2025-08-02T09:28:07","modified_gmt":"2025-08-02T07:28:07","slug":"desplegando-una-api-rest-con-spring-boot-y-docker","status":"publish","type":"post","link":"https:\/\/www.oferto.io\/en\/desplegando-una-api-rest-con-spring-boot-y-docker\/","title":{"rendered":"Deploying a REST API with Spring Boot and Docker"},"content":{"rendered":"<p data-pm-slice=\"1 1 []\">El desarrollo de APIs REST es una pr\u00e1ctica habitual en proyectos backend modernos. Spring Boot facilita enormemente esta tarea, ofreciendo una estructura clara, integraci\u00f3n con librer\u00edas populares y soporte para perfiles de entorno. En este art\u00edculo aprender\u00e1s a crear una API REST sencilla con Spring Boot y desplegarla usando Docker. Este enfoque garantiza portabilidad, repetibilidad y facilidad de despliegue en cualquier entorno.<\/p>\n<h4>1. Crear el proyecto con Spring Boot<\/h4>\n<p>Puedes usar <a>Spring Initializr<\/a> para generar la estructura b\u00e1sica. Selecciona:<\/p>\n<ul data-spread=\"false\">\n<li>Project: Maven<\/li>\n<li>Language: Java<\/li>\n<li>Dependencies: Spring Web, Spring Data JPA, PostgreSQL Driver<\/li>\n<\/ul>\n<p>Una vez generado el proyecto, a\u00f1ade una entidad y un controlador REST b\u00e1sico:<\/p>\n<pre><code>@Entity\r\npublic class Task {\r\n    @Id @GeneratedValue\r\n    private Long id;\r\n    private String title;\r\n    private boolean completed;\r\n    \/\/ getters y setters\r\n}\r\n\r\n@RestController\r\n@RequestMapping(\"\/tasks\")\r\npublic class TaskController {\r\n    @Autowired\r\n    private TaskRepository repository;\r\n\r\n    @GetMapping\r\n    public List&lt;Task&gt; getAll() {\r\n        return repository.findAll();\r\n    }\r\n}<\/code><\/pre>\n<h4>2. Configurar Docker<\/h4>\n<p>Para contenerizar la aplicaci\u00f3n, crea un <code>Dockerfile<\/code>:<\/p>\n<pre><code>FROM openjdk:17-jdk-slim\r\nARG JAR_FILE=target\/*.jar\r\nCOPY ${JAR_FILE} app.jar\r\nENTRYPOINT [\"java\",\"-jar\",\"\/app.jar\"]<\/code><\/pre>\n<p>Y un <code>docker-compose.yml<\/code> para levantar la app y PostgreSQL:<\/p>\n<pre><code>version: '3.8'\r\nservices:\r\n  db:\r\n    image: postgres:15\r\n    environment:\r\n      POSTGRES_DB: tasks\r\n      POSTGRES_USER: user\r\n      POSTGRES_PASSWORD: pass\r\n    ports:\r\n      - \"5432:5432\"\r\n  app:\r\n    build: .\r\n    ports:\r\n      - \"8080:8080\"\r\n    depends_on:\r\n      - db\r\n    environment:\r\n      SPRING_DATASOURCE_URL: jdbc:postgresql:\/\/db:5432\/tasks\r\n      SPRING_DATASOURCE_USERNAME: user\r\n      SPRING_DATASOURCE_PASSWORD: pass<\/code><\/pre>\n<h4>3. Probar el entorno local<\/h4>\n<p>Ejecuta <code>docker-compose up --build<\/code>. Accede a <code>http:\/\/localhost:8080\/tasks<\/code> para probar el endpoint. Puedes usar <code>Postman<\/code> o <code>curl<\/code> para hacer peticiones <code>GET<\/code>, <code>POST<\/code>, etc.<\/p>\n<h4>4. Consideraciones para producci\u00f3n<\/h4>\n<ul data-spread=\"false\">\n<li>Usa perfiles <code>application-dev.yml<\/code> y <code>application-prod.yml<\/code><\/li>\n<li>Configura <code>Flyway<\/code> para la gesti\u00f3n de esquemas<\/li>\n<li>Agrega logging estructurado y gesti\u00f3n de errores global<\/li>\n<li>Usa im\u00e1genes base m\u00e1s ligeras (Alpine)<\/li>\n<\/ul>\n<p>Con esta configuraci\u00f3n tienes un entorno de desarrollo y despliegue b\u00e1sico listo para crecer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this quickstart, I explain how to create a simple REST API with Spring Boot and package it with Docker for deployment. It includes a basic Dockerfile and a docker-compose.yml file to deploy the database and app simultaneously.<\/p>","protected":false},"author":1,"featured_media":586,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backend"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Desplegando una API REST con Spring Boot y Docker - Oferto<\/title>\n<meta name=\"description\" content=\"En esta gu\u00eda r\u00e1pida explico c\u00f3mo crear una API REST sencilla con Spring Boot y empaquetarla con Docker para su despliegue. Incluye un Dockerfile b\u00e1sico y un docker-compose.yml para levantar la base de datos y la app de forma simult\u00e1nea.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.oferto.io\/en\/desplegando-una-api-rest-con-spring-boot-y-docker\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Desplegando una API REST con Spring Boot y Docker - Oferto\" \/>\n<meta property=\"og:description\" content=\"En esta gu\u00eda r\u00e1pida explico c\u00f3mo crear una API REST sencilla con Spring Boot y empaquetarla con Docker para su despliegue. Incluye un Dockerfile b\u00e1sico y un docker-compose.yml para levantar la base de datos y la app de forma simult\u00e1nea.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.oferto.io\/en\/desplegando-una-api-rest-con-spring-boot-y-docker\/\" \/>\n<meta property=\"og:site_name\" content=\"Oferto\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-02T07:25:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-02T07:28:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.oferto.io\/wp-content\/uploads\/2025\/08\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"791\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"doominio\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/\"},\"author\":{\"name\":\"doominio\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#\\\/schema\\\/person\\\/6ee60897fbb7fb4ad6634c73f4be87fa\"},\"headline\":\"Desplegando una API REST con Spring Boot y Docker\",\"datePublished\":\"2025-08-02T07:25:25+00:00\",\"dateModified\":\"2025-08-02T07:28:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/\"},\"wordCount\":202,\"publisher\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.oferto.io\\\/wp-content\\\/uploads\\\/2025\\\/08\\\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg\",\"articleSection\":[\"Backend\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/\",\"url\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/\",\"name\":\"Desplegando una API REST con Spring Boot y Docker - Oferto\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.oferto.io\\\/wp-content\\\/uploads\\\/2025\\\/08\\\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg\",\"datePublished\":\"2025-08-02T07:25:25+00:00\",\"dateModified\":\"2025-08-02T07:28:07+00:00\",\"description\":\"En esta gu\u00eda r\u00e1pida explico c\u00f3mo crear una API REST sencilla con Spring Boot y empaquetarla con Docker para su despliegue. Incluye un Dockerfile b\u00e1sico y un docker-compose.yml para levantar la base de datos y la app de forma simult\u00e1nea.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.oferto.io\\\/wp-content\\\/uploads\\\/2025\\\/08\\\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg\",\"contentUrl\":\"https:\\\/\\\/www.oferto.io\\\/wp-content\\\/uploads\\\/2025\\\/08\\\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg\",\"width\":1500,\"height\":791,\"caption\":\"Software developing\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/desplegando-una-api-rest-con-spring-boot-y-docker\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Inicio\",\"item\":\"https:\\\/\\\/www.oferto.io\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Desplegando una API REST con Spring Boot y Docker\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#website\",\"url\":\"https:\\\/\\\/www.oferto.io\\\/\",\"name\":\"Oferto\",\"description\":\"Software Toolbox\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.oferto.io\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#organization\",\"name\":\"Oferto\",\"url\":\"https:\\\/\\\/www.oferto.io\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.oferto.io\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/image.png\",\"contentUrl\":\"https:\\\/\\\/www.oferto.io\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/image.png\",\"width\":1385,\"height\":437,\"caption\":\"Oferto\"},\"image\":{\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"http:\\\/\\\/www.linkedin.com\\\/in\\\/masalinas\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.oferto.io\\\/#\\\/schema\\\/person\\\/6ee60897fbb7fb4ad6634c73f4be87fa\",\"name\":\"doominio\",\"sameAs\":[\"https:\\\/\\\/www.oferto.io\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Deploying a REST API with Spring Boot and Docker - Oferto","description":"In this quickstart, I explain how to create a simple REST API with Spring Boot and package it with Docker for deployment. It includes a basic Dockerfile and a docker-compose.yml file to deploy the database and app simultaneously.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.oferto.io\/en\/desplegando-una-api-rest-con-spring-boot-y-docker\/","og_locale":"en_US","og_type":"article","og_title":"Desplegando una API REST con Spring Boot y Docker - Oferto","og_description":"En esta gu\u00eda r\u00e1pida explico c\u00f3mo crear una API REST sencilla con Spring Boot y empaquetarla con Docker para su despliegue. Incluye un Dockerfile b\u00e1sico y un docker-compose.yml para levantar la base de datos y la app de forma simult\u00e1nea.","og_url":"https:\/\/www.oferto.io\/en\/desplegando-una-api-rest-con-spring-boot-y-docker\/","og_site_name":"Oferto","article_published_time":"2025-08-02T07:25:25+00:00","article_modified_time":"2025-08-02T07:28:07+00:00","og_image":[{"width":1500,"height":791,"url":"https:\/\/www.oferto.io\/wp-content\/uploads\/2025\/08\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg","type":"image\/jpeg"}],"author":"doominio","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/#article","isPartOf":{"@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/"},"author":{"name":"doominio","@id":"https:\/\/www.oferto.io\/#\/schema\/person\/6ee60897fbb7fb4ad6634c73f4be87fa"},"headline":"Desplegando una API REST con Spring Boot y Docker","datePublished":"2025-08-02T07:25:25+00:00","dateModified":"2025-08-02T07:28:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/"},"wordCount":202,"publisher":{"@id":"https:\/\/www.oferto.io\/#organization"},"image":{"@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/#primaryimage"},"thumbnailUrl":"https:\/\/www.oferto.io\/wp-content\/uploads\/2025\/08\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg","articleSection":["Backend"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/","url":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/","name":"Deploying a REST API with Spring Boot and Docker - Oferto","isPartOf":{"@id":"https:\/\/www.oferto.io\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/#primaryimage"},"image":{"@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/#primaryimage"},"thumbnailUrl":"https:\/\/www.oferto.io\/wp-content\/uploads\/2025\/08\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg","datePublished":"2025-08-02T07:25:25+00:00","dateModified":"2025-08-02T07:28:07+00:00","description":"In this quickstart, I explain how to create a simple REST API with Spring Boot and package it with Docker for deployment. It includes a basic Dockerfile and a docker-compose.yml file to deploy the database and app simultaneously.","breadcrumb":{"@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/#primaryimage","url":"https:\/\/www.oferto.io\/wp-content\/uploads\/2025\/08\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg","contentUrl":"https:\/\/www.oferto.io\/wp-content\/uploads\/2025\/08\/closeup-adult-asia-male-female-freelance-typing-w-2024-11-02-12-26-47-utc.jpg","width":1500,"height":791,"caption":"Software developing"},{"@type":"BreadcrumbList","@id":"https:\/\/www.oferto.io\/desplegando-una-api-rest-con-spring-boot-y-docker\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Inicio","item":"https:\/\/www.oferto.io\/"},{"@type":"ListItem","position":2,"name":"Desplegando una API REST con Spring Boot y Docker"}]},{"@type":"WebSite","@id":"https:\/\/www.oferto.io\/#website","url":"https:\/\/www.oferto.io\/","name":"Oferto","description":"Software Toolbox","publisher":{"@id":"https:\/\/www.oferto.io\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.oferto.io\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.oferto.io\/#organization","name":"Oferto","url":"https:\/\/www.oferto.io\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oferto.io\/#\/schema\/logo\/image\/","url":"https:\/\/www.oferto.io\/wp-content\/uploads\/2025\/07\/image.png","contentUrl":"https:\/\/www.oferto.io\/wp-content\/uploads\/2025\/07\/image.png","width":1385,"height":437,"caption":"Oferto"},"image":{"@id":"https:\/\/www.oferto.io\/#\/schema\/logo\/image\/"},"sameAs":["http:\/\/www.linkedin.com\/in\/masalinas\/"]},{"@type":"Person","@id":"https:\/\/www.oferto.io\/#\/schema\/person\/6ee60897fbb7fb4ad6634c73f4be87fa","name":"domain","sameAs":["https:\/\/www.oferto.io"]}]}},"_links":{"self":[{"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":1,"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":585,"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/posts\/584\/revisions\/585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/media\/586"}],"wp:attachment":[{"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oferto.io\/en\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}