<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Diego Hordi on Medium]]></title>
        <description><![CDATA[Stories by Diego Hordi on Medium]]></description>
        <link>https://medium.com/@diego.hordi?source=rss-3956e0d07089------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*E0qZ8xL45v4360NWiyEPfg.jpeg</url>
            <title>Stories by Diego Hordi on Medium</title>
            <link>https://medium.com/@diego.hordi?source=rss-3956e0d07089------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 02:09:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@diego.hordi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Writing and deploying your first AWS Lambda with Go]]></title>
            <link>https://levelup.gitconnected.com/writing-and-deploying-your-first-aws-lambda-7a6d5800b443?source=rss-3956e0d07089------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a6d5800b443</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Diego Hordi]]></dc:creator>
            <pubDate>Wed, 29 Jan 2020 22:16:37 GMT</pubDate>
            <atom:updated>2020-01-30T03:59:04.903Z</atom:updated>
            <content:encoded><![CDATA[<p>This post shows how to write and deploy an AWS Lambda using Go.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ShS3U6f7ox0Lig_-" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>First things first</h3><p>The idea behind writing this post is to show you how can we start to develop small things as cloud-native. In order to achieve this result, I’ve chosen Go as the language, AWS as our cloud provider (S2), and the following problem:<em> you are new in town and you are going to a square, so you search for a restaurant on the internet and find a good one. Now, you want to know if you (point A) are next to the restaurant’s (point B) radius. How could you solve it, given point A (latitude and longitude), point B (latitude and longitude) and its radius in meters?</em></p><p>Don’t run away if you don’t know Go. It’s very simple to understand and as I said, the main goal is to show you how can we write, deploy, and access a small cloud-native solution.</p><p>To solve the problem, we’ll use the Spherical Law of Cosines. I’ve found a <a href="http://www.movable-type.co.uk/scripts/latlong.html">very useful page</a> that explains its formula and how to calculate it, far better than I could.</p><p>So, firstly I’ll show you the lambda code, line-by-line, introducing you to the AWS Lambda environment, and finally, we’ll see how can you deploy and test it using the management console.</p><p><strong>If you’ve some knowledge on AWS CLI, I strongly recommend you check out the </strong><a href="https://docs.aws.amazon.com/cli/latest/reference/lambda/create-function.html"><strong>lambda create-function</strong></a><strong> command reference to see how could you deploy our lambda in a CLI way too.</strong></p><p>At last but not the least, I’ve used Go 1.13.4 (<a href="https://golang.org/doc/install">how to get and install it</a>), and the project’s code could be accessed from my <a href="https://github.com/diegohordi/awslambda-georadius-checker">GitHub</a>.</p><p>So, let’s get our hands dirty!</p><h4>Environment</h4><p>Assuming you have already installed <a href="https://golang.org/doc/install">Go</a>, in order to write other AWS Lambdas you’ll need to add the following dependencies:</p><p>#1 — <a href="http://github.com/aws/aws-lambda-go">AWS Lambda Go package</a></p><pre>go get github.com/aws/aws-lambda-go/lambda</pre><p>#2 — AWS Lambda Deployment Package Tool (only Windows)</p><pre>go get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip</pre><h4><strong>Creating the project</strong></h4><p>To create a new project in Go, just type in your prompt tool:</p><pre>go mod init github.com/&lt;your-username&gt;/georadius-checker</pre><p>After creating the project, just create a<strong> georadius-checker.go</strong> with the lines explained below.</p><h4>Our lambda</h4><p>In the first lines of our file, we declare the package and imports we’ll use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80705d920767f2a665bd4c2c7574882b/href">https://medium.com/media/80705d920767f2a665bd4c2c7574882b/href</a></iframe><p>At line #3, we create a constant to hold the Spherical Law of Cosine’s radius constant. In order to solve our problem, we need to receive 5 parameters, so, from line #6 to #12 we create our <em>data transfer object</em> (DTO)<em> </em>to hold these parameters. Notice that for all attributes that you want to receive, its names must be capitalized.</p><p>At line #14 we create our DTO to hold the response model. As our request DTO, all attributes that you want to export must have your names capitalized.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a075b840144b4204bbe4436db01c7e0c/href">https://medium.com/media/a075b840144b4204bbe4436db01c7e0c/href</a></iframe><p>After defining our DTO, we create a simple private function (its name starts with a lower case) to convert degrees to radians, as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fc70c6df59e97be924cfaace31f8f2e/href">https://medium.com/media/0fc70c6df59e97be924cfaace31f8f2e/href</a></iframe><p>Finally, we transcript the formula to Go. At line #2 we create a new public function (its name starts with an upper case), that receives our 5 params and returns a boolean. So, in line #3, we get the difference in radians of longitude from point B to A. In lines #4 and #5 we get the latitude of points A and B in radians. At line #6, the important one, we hold the formula’s result. We use the default package math to keep things most simple as possible. At line #7, we check if the difference calculated is less than or equal to the radius given as a parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6976c78c2a1297d6317f6c9aa14e99e5/href">https://medium.com/media/6976c78c2a1297d6317f6c9aa14e99e5/href</a></iframe><p>Besides the method above, the next lines hold the most important code, the one that enables us to deploy this method on AWS.</p><p>At line #2 we declare the <a href="https://docs.aws.amazon.com/lambda/latest/dg/go-programming-model-handler-types.html">HandleRequest method</a>, which is the one who receives the request from the AWS Lambda environment. This method signature just follows some rules, but basically, it can receive a Context object, that provides runtime information for lambda invocation, an object of some type you need to define (in our case, we receive a Coordinates one) and, finally, it can return some object and a standard error, if something wrong occurs during lambda execution.</p><p>AWS takes care of the deserialization of our request into the given data type. So, at line #3, we just call our method that checks if the point A coordinates is inside the radius of point B coordinates.</p><p>Finally, at line#7 we declare the main method, which is the entry point that executes your lambda function code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2ee508120a6267eebefed1d1b10bf25/href">https://medium.com/media/c2ee508120a6267eebefed1d1b10bf25/href</a></iframe><h4>Build</h4><p>If you’re using Windows, I put an extra file on the repository, called <strong>build-aws-zip.bat</strong>, that you can run. This script basically, build our Go application and call <strong>build-lambda-zip</strong> tool to put it inside a zip file in /target directory, that we’ll deploy on AWS.</p><p>In UNIX environments, just build your Go application:</p><pre>go build</pre><p>And put the generated binary file inside a zip:</p><pre><strong>zip </strong>georadius-checker<strong>.zip </strong>georadius-checker</pre><p><strong>Deploy</strong></p><p>I assume you have got an AWS account. To deploy our lambda, access your management console. Once logged in, in Services menu, just type Lambda, and click on it, like the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MqcsCSeEgAqlhKcRXp-w4g.png" /></figure><p>In the Lambda page, click on the <strong>Create function</strong> button, and then, we set up our lambda as follows.</p><p>Select <strong>Author from scratch </strong>option, then in the field <strong>Function name</strong>, put the name of our Go file, without the extension (georadius-checker in this case). So, select Go 1.x at <strong>Runtime</strong> dropdown list. At permissions settings, leave the option “<strong>Create a new role with basic Lambda permissions</strong>” (which creates a role with permissions to upload logs to CloudWatch Logs). Then click the <strong>Create function</strong> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YyTsItZ2wA_Ys0u9_EBTiw.png" /></figure><p>The next screen seems as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H7xmbYhxJ-aKmcGlUKaTsA.png" /></figure><p>Click on the <strong>Upload</strong> button, select our zip file and in <strong>Handler</strong> field, put the name of our Go file again, without the extension, then click <strong>Save</strong> button.</p><p>To test our lambda, we’ll create two scenarios, the InsideRadiusTest and the OutsideRadiusTest. To do the first, click on the <strong>Test</strong> button and at <strong>Event name </strong>field, type <strong>InsideRadiusTest, </strong>then paste the code below and click <strong>Create </strong>button.</p><pre>{<br>  &quot;aLatitude&quot;: 38.6938,<br>  &quot;aLongitude&quot;: -9.4204,<br>  &quot;bLatitude&quot;: 38.7254,<br>  &quot;bLongitude&quot;: -9.1502,<br>  &quot;radius&quot;: 23710<br>}</pre><p>Now, we’ll create the <strong>OusideRadiusTest</strong>. Select the option <strong>Configure Test Events</strong> at the dropdown close to the Test button and paste the following code.</p><pre>{<br>  &quot;aLatitude&quot;: 38.6938,<br>  &quot;aLongitude&quot;: -9.4204,<br>  &quot;bLatitude&quot;: 38.7254,<br>  &quot;bLongitude&quot;: -9.1502,<br>  &quot;radius&quot;: 23700<br>}</pre><p>After the creation of the test Events, just select what you want to test in the dropdown, then click the <strong>Test </strong>button. The results seem as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DXGx22nkXDBW1npDgt0K4w.png" /></figure><h4>Conclusion</h4><p>I’ve intended to share with you some basics on how to write an AWS Lambda using Go language and how to deploy and test it into your AWS account. Now <a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-invocation.html">you can access it through AWS CLI</a> or even create an endpoint in the AWS API Gateway.</p><p>As you can see, it’s quite simple to write a lambda function and deploy it, and AWS has a lot of documentation describing how can you do that using other languages, like Node.js, Python, and Java, for instance.</p><p>But, besides all the technical things that we had seen here, I hope you have some fun discovering and investigating how small things could be written in a cloud-native way.</p><p>So, I hope this post is useful.</p><p>Thank you for reading and if you have any questions, count on me! =)</p><h4>References</h4><p><a href="https://golang.org/">GO</a></p><p><a href="http://www.movable-type.co.uk/scripts/latlong.html">Spherical Law of Cosiness</a></p><p><a href="https://docs.aws.amazon.com/en_us/lambda/?id=docs_gateway">AWS Lambda Documentation</a></p><p><a href="https://docs.aws.amazon.com/lambda/latest/dg/go-programming-model.html">Building Lambda Functions with GO</a></p><p><a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-go-how-to-create-deployment-package.html">AWS Lambda Deployment Package in Go</a></p><p><a href="https://docs.aws.amazon.com/cli/latest/reference/lambda/index.html#cli-aws-lambda">AWS CLI Lambda API reference</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a6d5800b443" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/writing-and-deploying-your-first-aws-lambda-7a6d5800b443">Writing and deploying your first AWS Lambda with Go</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Packaging your Spring Boot and Angular 2+ projects together]]></title>
            <link>https://levelup.gitconnected.com/packaging-your-spring-boot-and-angular-2-projects-together-a13a9c5efdb7?source=rss-3956e0d07089------2</link>
            <guid isPermaLink="false">https://medium.com/p/a13a9c5efdb7</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Diego Hordi]]></dc:creator>
            <pubDate>Tue, 21 Jan 2020 13:47:03 GMT</pubDate>
            <atom:updated>2020-01-21T16:36:34.783Z</atom:updated>
            <content:encoded><![CDATA[<p>This post shows how you can wrap your frontend and backend projects together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dq7qjtOZorrgWAN_" /><figcaption>Photo by <a href="https://unsplash.com/@chuttersnap?utm_source=medium&amp;utm_medium=referral">chuttersnap</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>First things first</h4><p>First, it’s necessary to give you a context before you put this solution to work. It could be useful when you think about local projects, where there is no elasticity (one of the most important benefits of cloud architectures) and you need to distribute a single WAR (or JAR) file inside a single container or a single application server, for instance.</p><p>The solution is not so simple. You don’t need to be an expert, but I assume you have some basic knowledge on Maven, Spring Boot, and Angular to go forward, especially because I’ll skip the project’s creation steps.</p><p>I’m using Java 11, Maven 3.6.1, Spring Boot 2.2.1, Node 10.16.0 and Angular 8.2.14.</p><p>To start, I recommend you to clone the project from my <a href="https://github.com/diegohordi/package-springboot-angular">GitHub</a>.</p><p>Let’s get our hands dirty!</p><h4>The main project</h4><p>The main project just contains the backend and frontend projects as modules, and its POM is as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aed33554e85bb9733815cc82c49d28a6/href">https://medium.com/media/aed33554e85bb9733815cc82c49d28a6/href</a></iframe><p>Notice that we put Spring Boot starter as parent POM here.</p><h4>The frontend project</h4><p>First, you need to edit the <strong>package.json</strong> file and put a new script on the “scripts” attribute, as follows in line #7.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/116e0737c0b3056c90c14f9ee7a9ddd9/href">https://medium.com/media/116e0737c0b3056c90c14f9ee7a9ddd9/href</a></iframe><p>This change is necessary due to the <a href="https://github.com/eirslett/frontend-maven-plugin">Frontend Maven Plugin</a> that we’ll use to build the frontend project.</p><p>This plugin deserves a lot of stars! With that, it’s possible to do many cool things, such as install the Node.js and NPM locally and run scripts. So, in order to use it, we need to setup de frontend’s POM. So let’s see it step-by-step.</p><p>In start lines, we set the parent POM (to the main project) and define the frontend properties, such as the versions of Node and NPM.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ee7f4096508ae01cfa3df43b29b91e3/href">https://medium.com/media/2ee7f4096508ae01cfa3df43b29b91e3/href</a></iframe><p>After that, we set up the Frontend Maven Plugin as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/431598960a942e6fba01d44350ecc936/href">https://medium.com/media/431598960a942e6fba01d44350ecc936/href</a></iframe><p>Just notice that at line #13 we set the working directory of the plugin — where node and NPM will be locally installed. Finally, we set up the goals that will be executed during the build lifecycle.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4c3d631515ccb8e9d36cd49143fb377/href">https://medium.com/media/f4c3d631515ccb8e9d36cd49143fb377/href</a></iframe><p>#1 execution install Node.js and NPM, #2 install project dependencies, and the last but not the least, we run the build-prod script to build the bundle.</p><h4><strong>The backend project</strong></h4><p>The backend project contains a very simple Spring Boot project. The main aspect you need to pay attention, it’s the fact that we’re using the <a href="https://maven.apache.org/plugins/maven-resources-plugin/">Maven Resources Plugin</a><strong> </strong>to<strong> </strong>gather the bundle built by frontend project and the <a href="https://docs.spring.io/spring-boot/docs/current/maven-plugin/index.html">Spring Boot Maven Plugin</a> associated to repackage goal to keep up all those things together. So, let’s see its POM step-by-step.</p><p>In start lines, we set the parent POM (to the main project) and define the backend properties, as we’ve done in the frontend project as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58468c2758b2ce869156c6fd1720f527/href">https://medium.com/media/58468c2758b2ce869156c6fd1720f527/href</a></iframe><p>After setting project properties, we declare our dependencies, as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ab4d2b7ff60547711e508f5afe94eae/href">https://medium.com/media/9ab4d2b7ff60547711e508f5afe94eae/href</a></iframe><p>And finally, we set the plugins to compile, gather the resources and package all those things.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/211f74e3257bbd58fd7f39625ef87982/href">https://medium.com/media/211f74e3257bbd58fd7f39625ef87982/href</a></iframe><h4><strong>Extra: Angular Route issue</strong></h4><p>Just as we need to use the rewrite module on the Apache HTTP Server or write some rewrite rules on NGINX, when we package an Angular project that uses a router inside a Spring Boot that uses an embedded Tomcat server, we need to deal with it too. There are a lot of solutions regarding how to solve this issue, but I prefer to write a filter to do the job. It’s quite simple, easy to change and it works very well, without any external dependency.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6902df31834fd5b85ef0777d3ba322e9/href">https://medium.com/media/6902df31834fd5b85ef0777d3ba322e9/href</a></iframe><h4>Creating the package</h4><p>To create the package just run the command below at the project’s main directory.</p><pre>mvn package</pre><p>After execution, a distributable WAR file will be generated at the /target directory on the backend’s project directory.</p><h4>Conclusion</h4><p>At this post, I’ve intended to share with you how we could package our Angular 2+’s frontend and Spring Boot’s backend in one distributable WAR file, using for that just Maven plugins and not other external tools.</p><p>As I said at the very beginning of this post, this solution is intended to be used when you’ve to deal with infrastructure restrictions, so, for many other scenarios, I recommend you to bring up these things separated.</p><p>I hope this post is useful.</p><p>Thank you for reading and if you have any questions, count on me! =)</p><h4>References</h4><p><a href="https://github.com/eirslett/frontend-maven-plugin">Frontend Maven Plugin</a></p><p><a href="https://maven.apache.org/plugins/maven-resources-plugin/">Maven Resources Plugin</a></p><p><a href="https://docs.spring.io/spring-boot/docs/current/maven-plugin/index.html">Spring Boot Maven Plugin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a13a9c5efdb7" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/packaging-your-spring-boot-and-angular-2-projects-together-a13a9c5efdb7">Packaging your Spring Boot and Angular 2+ projects together</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PostgreSQL restore-ready Docker image]]></title>
            <link>https://levelup.gitconnected.com/postgresql-restore-ready-docker-image-7001a54400e9?source=rss-3956e0d07089------2</link>
            <guid isPermaLink="false">https://medium.com/p/7001a54400e9</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Diego Hordi]]></dc:creator>
            <pubDate>Wed, 15 Jan 2020 07:28:59 GMT</pubDate>
            <atom:updated>2020-01-21T23:52:03.968Z</atom:updated>
            <cc:license>https://creativecommons.org/publicdomain/mark/1.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XBc_Q8_bYt57VQNj" /><figcaption>Photo by <a href="https://unsplash.com/@victoire_jonch?utm_source=medium&amp;utm_medium=referral">Victoire Joncheray</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This post is intended to share a PostgreSQL restore-ready image that I create in a personal project. It’s quite simple, with just a few lines.</p><p>First, you can clone or download the Dockerfile and bash script used in the article from my <a href="https://github.com/diegohordi/pgsql-restore-ready-image.git">GitHub</a>.</p><p>To build the new image, we extend the latest <a href="https://hub.docker.com/_/postgres">Postgres image available on Docker Hub</a> and copy one bash file with the instructions to create a new database and restore it using the given back-up file.</p><p>Let’s get our hands dirty!</p><h4>The Dockerfile</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7db804c2538a291ba5f249a76d7274e/href">https://medium.com/media/a7db804c2538a291ba5f249a76d7274e/href</a></iframe><p>At line #1 we extend the <a href="https://hub.docker.com/_/postgres">latest official Postgres image</a>, and from line #2 to #5 we create the build-args and the environment vars that we’ll use in the bash file to restore the database. At line #6 we create the volume and at #8 we copy the given back-up file passed through build-args to the created volume. At #10 we copy the bash file to the image’s entry point. At line #11 (if you’re using Windows) we convert the end line’s chars of bash file from DOS to Unix. At line #12 we grant the bash file the rights to run properly. Finally, we expose the PostgreSQL’s default port 5432 to the host.</p><h4>The bash file</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f5bfcb51c1613c5d125736f41aa21c6/href">https://medium.com/media/4f5bfcb51c1613c5d125736f41aa21c6/href</a></iframe><p>At line #3 we create the database with the given name, using the postgres user. At #4 we restore the database and at #5, we grant the user postgres the privileges to the created database. Lines #2 and #6 are just for status reporting purposes.</p><h4>Building the image</h4><p>To build the image, just execute the code below from the directory where you put the Dockerfile, replacing the &lt;image-name:tag-name&gt; for the name and tag you want, the &lt;backup-file-name&gt; for the name of the back-up file and finally, the &lt;database-name&gt; to the name of the database you want to restore.</p><p>To keep things simple, I recommend that you put your backup file in the same directory of the Dockerfile.</p><pre>docker build -t &lt;image-name:tag-name&gt; . --build-arg FILE=&lt;backup-file-name&gt; --build-arg DBNAME=&lt;database-name&gt;</pre><h4>Creating the container</h4><p>To create the container, execute the code below, replacing the &lt;container-name&gt; for the name you want, the &lt;port-number&gt; for the port you want to use to access the database and the &lt;image-name:tag-name&gt; for the name of your image.</p><pre>docker run -d --name &lt;container-name&gt; -p &lt;port-number&gt;:5432 &lt;image-name:tag-name&gt;</pre><h4>Accessing the database</h4><p>To access the database from the host of your container, just use the host <strong>localhost </strong>and the port number you gave in &lt;port-number&gt; with the user <strong>postgres</strong>, with no password.</p><h4>Conclusion</h4><p>In this post, I’ve shared how to extend the official Postgres image doing a simple restore command, allowing you to access the database as soon as the container was ready.</p><p>I use it to save my time and resources with short time-to-live projects, but you can extend it and perform other commands like vacuum, for instance.</p><p>Thank you for reading and if you have some questions/issues, count on me. =)</p><h4>References</h4><p><a href="https://docs.docker.com/engine/reference/commandline/build/">Docker Build reference guide</a></p><p><a href="https://docs.docker.com/engine/reference/commandline/run/">Docker Run reference guide</a></p><p><a href="https://hub.docker.com/_/postgres">Postgres official image</a></p><p><a href="https://www.postgresql.org/docs/9.2/app-pgrestore.html">The pg_restore reference guide</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7001a54400e9" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/postgresql-restore-ready-docker-image-7001a54400e9">PostgreSQL restore-ready Docker image</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>