<?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 Richard Coker on Medium]]></title>
        <description><![CDATA[Stories by Richard Coker on Medium]]></description>
        <link>https://medium.com/@cokerrd?source=rss-afc16a5a9a10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*pWw5Mel9mbGdrYQnn4OZyg.jpeg</url>
            <title>Stories by Richard Coker on Medium</title>
            <link>https://medium.com/@cokerrd?source=rss-afc16a5a9a10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:38:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cokerrd/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[9 Docker tips you didn’t know you needed(until now)]]></title>
            <link>https://medium.com/@cokerrd/9-docker-tips-you-didnt-know-you-needed-until-now-b5304e7e3e5b?source=rss-afc16a5a9a10------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5304e7e3e5b</guid>
            <category><![CDATA[containerization]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-compose]]></category>
            <dc:creator><![CDATA[Richard Coker]]></dc:creator>
            <pubDate>Tue, 28 Jan 2025 10:26:02 GMT</pubDate>
            <atom:updated>2025-01-28T10:26:02.376Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*22gxp8vtkuG3FO5Bze3dkg.png" /></figure><p>Docker is pretty much the go-to tool in modern app development, powering everything from testing to CI/CD pipelines and micro-services, plus Docker itself is easy to grasp — <em>well, at least on the surface</em>. In this guide, aimed at junior and mid-level engineers with basic Docker knowledge, I’ll share 9 tips to help you “dock” into deeper waters (sorry, I just had to) and boost your Docker skills for production. , scripts dockerfile,</p><h4>1. Keep an Eye on Container Activity</h4><p>Keeping an eye on your container’s logs is necessary for debugging and understanding what’s happening inside your app. Instead of running docker logs over and over — and over again , use the --follow flag to tail the logs in real time, pretty much like how tail -f works for log files.</p><pre>docker logs --follow &lt;container_name_or_id&gt;</pre><p>This lets you see the latest log entries as they happen, which is perfect for monitoring long-running processes or identifying issues on the fly. It’s such a simple, yet command, saving a whole lot of time when troubleshooting.<br><em>(Trust me, this would come in so handy)</em></p><h4>2. Secure Sensitive Data with Docker Secrets</h4><p>When building Docker images, it’s necessary to handle sensitive data like API keys, tokens, and passwords securely. If you pass in sensitive data as environment variables or build arguments plainly, they can be found in you docker image build which is a no-no in production because anyone who has access to your image can have access to the credentials as well. so how do you solve this problem? <em>I’m glad you asked!</em> Docker BuildKit provides a streamlined way to manage secrets during the build process, ensuring they don’t end up in your image layers. It is best practice to using secrets when passing sensitive data in containers.</p><pre># Enable Docker BuildKit<br>export DOCKER_BUILDKIT=1<br><br># Build with a secret<br>docker build --secret id=my_secret,src=/path/to/secret/file.txt -t app:latest .</pre><p>In your Dockerfile you can reference it as follows</p><pre># syntax=docker/dockerfile:1.2<br><br>FROM alpine:latest<br><br>RUN --mount=type=secret,id=my_secret \<br>    cp /run/secrets/my_secret /etc/my_secret_data</pre><h4>3. Never Run Your Container as Root</h4><p>One common pattern you’ll notice in Docker tutorials or sample Dockerfiles is that containers often run as the root user. But how can you tell if your container is running as root? It’s easy — just run docker exec -it your_container_name whoami, and it will confirm the user inside the container.</p><p><strong>Why avoid running as root?</strong> Using root in your Docker image increases security risks, as vulnerabilities in your app could allow attackers to gain root access on the host machine. Running as a non-root user reduces the risk of privilege escalation and protects both your container and the host. The best practice is to create a non-root user in your Dockerfile, assigning only the permissions needed to run the application securely.</p><pre><br># Create a non-root user and group<br>RUN addgroup --system --gid 1001 nodejs<br>RUN adduser --system --uid 1001 nextjs<br><br># Set the correct permission for prerender cache<br>RUN mkdir .next<br>RUN chown nextjs:nodejs .next<br><br># Automatically leverage output traces to reduce image size <br>COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./<br>COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static<br><br>USER nextjs<br><br># Start the Next.js application<br>CMD [&quot;node&quot;, &quot;server.js&quot;]</pre><p>This example demonstrates creating a user(next js) and group (node js) while assigning permissions for the .next directory to the nextjs user, and this useful when you want to limit or set specific privileges to the user running the container</p><h4>4. Boost Build Speed with Container Registry Caching</h4><p>You might already be familiar with Docker’s built-in image layer caching, but did you know you can also cache from a container registry? This technique is particularly useful in CI/CD pipelines. Instead of pulling base images or dependencies every time, you can cache these layers in your container registry, giving your builds a serious speed boost, granted you organised your Dockerfile effectively.</p><pre>docker build --cache-from your-container-registry/your-base-image:tag -t your-new-image:latest .</pre><p>when incorporating this in your pipeline make sure your pipeline has the necessary access to your container registry <em>i.e Dockerhub, AWS ECR, Google Container Registry or whatever Azure uses</em></p><h4>5. Maintain a Tidy Docker Environment</h4><p>Maintaining a tidy Docker environment is crucial for performance and storage management, especially when you’re frequently building and running containers leading up to megabytes of gigabytes of wasted resources. One of the most effective commands for this task is docker prune. It helps you free up disk space by removing unused data like stopped containers, dangling images, and unused networks—all with a single command.</p><pre># Remove all stopped containers<br>docker container prune<br><br># Remove dangling images<br>docker image prune<br><br># Remove all unused images<br>docker image prune -a<br><br># Remove all unused networks<br>docker network prune<br><br># Comprehensive cleanup<br>docker system prune<br><br># Remove all resources without prompt<br>docker prune -af</pre><p>Pairing up the docker prune -af in addition to the caching mechanism would make a powerful combo that would significantly improve the efficiency of your pipeline. As with great power comes great responsibility just be careful before implementing this.</p><h4>6.Manage Container Resources Wisely</h4><p>Have you ever had your application or service slow down or crash unexpectedly because one of your containers was hogging all the resources? Managing resources in Docker is crucial to prevent such scenarios. By limiting CPU and memory usage, you can ensure that your containers play nicely with others on the host.</p><pre>#restrict cpu<br>docker run --name my_app --cpus=&quot;1.5&quot; my_image<br><br>#restrict memory<br>docker run --name my_app --memory=&quot;512m&quot; my_image<br><br>#restrict swap memory<br>docker run --name my_app --memory=&quot;512m&quot; --memory-swap=&quot;1g&quot; my_image</pre><p>By setting resource limits, you ensure your containers run smoothly without overwhelming the host, enhancing performance and reliability. However, don’t go overboard — strict limits can cause containers to crash repeatedly, leading to a never-ending loop. As with everything — balance is key!</p><h4>7. Enhance Reliability with Health Checks</h4><p>Docker’s health checks are your secret weapon for maintaining reliable applications. By implementing health checks in your Dockerfile, you can automatically monitor the status of your running containers to ensure they’re functioning as expected.</p><p>You can define a health check using the HEALTHCHECK instruction, specifying a command that Docker will run to assess the container&#39;s health.</p><pre>#your preceeding dockerfile configuration<br>HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD curl -f http://localhost/ || exit 1</pre><p>In this example, Docker attempts to access a web service every 30 seconds. If it fails three consecutive times, the container is marked as unhealthy. This information is invaluable for orchestration tools like Docker Swarm or Kubernetes, which can take action — like restarting or replacing the container — if it becomes unhealthy. By incorporating health checks into your Docker setup, you enhance application reliability and improve your deployment strategy.</p><h4>8. Make the Most of Docker Restart Policies</h4><p>Speaking of deployment strategy, one way to enhance the resilience of your Docker containers is by utilizing restart policies. These policies allow you to define how Docker should handle container failures, ensuring your applications remain available even after unexpected crashes.</p><p>Here’s a brief overview of the different Docker restart policies you can use, along with their descriptions:</p><ul><li>No Restart (no)- This is the default policy. The container will not restart automatically if it stops.</li><li>Always- The container will restart indefinitely, regardless of the exit status.</li><li>Unless Stopped- Similar to the `always` policy, but it won’t restart the container if it was manually stopped.</li><li>On-Failure- The container will restart only if it exits with a non-zero exit status, indicating an error.</li></ul><pre>#no restart <br>docker run --restart no my-container<br><br>#Always<br>docker run --restart always my-container<br><br>#unless stopped<br>docker run --restart unless-stopped my-container<br><br>#on failure<br>docker run --restart on-failure:5 my-container<br></pre><h4>9. Simplify Your Dockerfile with External Scripts</h4><p>When building Docker images, it’s often necessary to execute scripts or commands as part of the container’s startup process. Instead of cluttering your Dockerfile with long commands, consider using external scripts for better organization and maintainability.</p><p>take for example a script entrypoint.sh</p><pre>#!/bin/bash<br><br># Function to run migrations with retries<br>run_migrations() {<br>    local retries=5<br>    local count=0<br>    until python manage.py migrate; do<br>        ((count++))<br>        if [ $count -eq $retries ]; then<br>            echo &quot;Migration failed after $retries attempts. Exiting.&quot;<br>            exit 1<br>        fi<br>        echo &quot;Migration failed. Retrying in 2 seconds... ($count/$retries)&quot;<br>        sleep 2<br>    done<br>}<br><br># Run migrations<br>run_migrations<br><br># Start the Gunicorn server<br>echo &quot;Starting Gunicorn server...&quot;<br>exec gunicorn myproject.wsgi:application --bind 0.0.0.0:8000</pre><p>with Dockerfile config</p><pre>#preceeding Dockerfile config<br><br># Make the entrypoint script executable<br>RUN chmod +x entrypoint.sh<br><br># Set the entrypoint to the script<br>ENTRYPOINT [&quot;./entrypoint.sh&quot;]</pre><p>This setup ensures that your Django application is correctly migrated before starting the server, enhancing reliability in your Dockerized environment. Now think of the possibilities of the scripts you could write and also the maintainability as well.</p><p>As a bonus tip for making it to the end, the last tip would be to simply take some time to read <a href="https://docs.docker.com/reference/cli/docker/">Docker’s documentation</a> as you can find a whole lot of handy things that are aren’t listed here.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5304e7e3e5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building an AWS CI/CD pipeline for a Java web application]]></title>
            <link>https://medium.com/@cokerrd/building-an-aws-ci-cd-pipeline-for-a-java-web-application-b46cfe7a0fcf?source=rss-afc16a5a9a10------2</link>
            <guid isPermaLink="false">https://medium.com/p/b46cfe7a0fcf</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <dc:creator><![CDATA[Richard Coker]]></dc:creator>
            <pubDate>Mon, 11 Mar 2024 18:14:39 GMT</pubDate>
            <atom:updated>2024-03-11T18:14:39.102Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Objective: </strong>Build and integrate a CI/ CD pipeline using just AWS services.</p><p><strong>Services used:</strong></p><ul><li>AWS Code Commit <em>to store the source code</em></li><li>AWS Code build<em> to build the artifact</em></li><li>S3 bucket<em> to store the artifact</em></li><li>AWS Deploy <em>to deploy the artifact from the s3 bucket to Elastic Beanstalk</em></li><li>Elastic Beanstalk <em>to deploy the web application</em></li><li>RDS database <em>for the database of the web application</em></li><li>AWS pipeline <em>to initiate the triggers to start the pipeline</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cbs2IMQcCEg4bkzHlnrSJQ.png" /></figure><p>Here are the steps to build and integrate this pipeline:</p><ol><li>Configure the Elastic beanstalk</li><li>Connect the RDS to the Elastic beanstalk</li><li>Create and configure the CodeCommit repository</li><li>Build the artifact with CodeBuild</li><li>Build the pipeline using CodePipeline</li></ol><h3>Configuring Elastic Beanstalk</h3><ol><li>Configuring the environment</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tViABz6onvr-4GTYoEVlgA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bJ44LoN1PYcdUZOnWpfMsg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y3ufDMgNMAyUJm6m9rXXGQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9QQwtDcO_gIBPg9NOIwtwg.png" /></figure><p>The Beanstalk is a web server environment, and its a managed Tomcat 9 platform</p><p>2. Configuring the service access</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t0hvjf_Scxlr1Ds_XcKIlA.png" /></figure><p>3. Configuring the network</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PYGFgk-FKy6tfgidoAx0Pg.png" /></figure><p>The default VPC was selected and the instances are set to have public IPs in case of wanting to SSH into them and all the AZs are checked for high availability.</p><p>4. Configuring the instance traffic and scaling</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*epVqFoEPgY0JIGvWyqI32w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23JzDUUQygOBwkRm-vMtqg.png" /></figure><p>The root volume is set on default, the instance type is t2 mirco to keep it free tier eligible and it minimum instances set to be running is 2 instances and at it’s maximum is 4 instances.</p><p>The scaling triggers as well as the rest of the configuration is left at its default</p><p>5. Configuring monitoring and updates</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HIkw-bCk_P6e7OzEqEgwJA.png" /></figure><p>The deployment policy is set on Rolling at a percentage of 50 % which simply means that in the event of an update there would only be downtime for half of the instances while updating.</p><p>Monitoring as well as the other configurations were left on default settings</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AUto6n91DncjNsZshz8s0Q.png" /></figure><h3>Connecting the RDS to the Elastic Beanstalk</h3><p>The first step is to create the RDS; second is to change the inbound rules for the RDS to allow Beanstalk on port 3306; third is initialize the database by ssh into a beanstalk EC2 instance, install mysql client and log into RDS; then the final step is simply to deploy the sql file to the RDS instance.</p><ol><li>Create RDS database</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KZzIS1FP9ecThv1yPHn7qA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MOYHgg15ti3cPGuaZr5XfQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jNJGrEZuvEQ_FkAy4MSnqg.png" /></figure><p>The storage is set to 20gb(which is the lowest) and I created a new security group for RDS which I would set the inbound rules later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fsFJE6CODCfGVUuxXfUE5Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U8rOzZN7UJrbCL8vepFzkw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gTiCzYfSXWASbnWs8LgYpw.png" /></figure><p>The initial database name is set to ‘accounts’ because that is what the vprofile web application is going to be looking for to connect.</p><p>2. Update the RDS security group to allow Beanstalk on port 3306</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oZDGhVgML14U2J-aswhizw.png" /></figure><p>3. SSH to the EC2 instance then connect to the RDS instance</p><p>first the MySQL client which is mariadb</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8gDL9ZOV3li1lpHmWjSxMg.png" /></figure><p>Then to connect to the RDS instance</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5GgyRK8HHd8ctwJ3oNU5QQ.png" /></figure><p>4. Deploy the SQL file to the RDS instance</p><p>first thing is to clone the git repository in order to get the SQL file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m1nWeXpezCc67G6BNj94Gg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*2QTmKaznsXSOcmAl-WYnUw.png" /></figure><p>Now to initialize the database using the SQL file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3oGdZ6rBE6Hvgv4DZt4QYg.png" /></figure><h3>Creating the CodeCommit repository</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hR23i6ijlSYAf0YqCbbdcw.png" /></figure><p>We can connect to the CodeCommit repo either by HTTPS or SSH but since SSH is more secure that is generally advised for good practice.</p><p>The first step in doing so is creating an IAM user with an AWS code commit managed policy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bqcEMZpFbiW7HG8qIG4KFg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*raLicbLD-2i-lfPapbWzrg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GU7P3l2QDzOA9yLHk__Hkg.png" /></figure><p>I need to upload SSH public key for AWS CodeCommit but first I need to generate the keys from my local machine</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KBkfYBcQTxk55GKQ1AyW-g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DqEz90F78QEDiYMmaWFCWA.png" /></figure><p>So now I have the public key (with ‘.pub’ extension) and the private key, so now I have to copy the public key from my local machine to the SSH public key for the vprofile CodeCommit user</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*II-HC9dWBEEQAEi3HBWggQ.png" /></figure><p>Now to create an SSH config file which SSH would use to determine the access key when the host is CodeCommit</p><p>I also have to make sure the config file has the right permission</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*miFrRJOqcbKTjwvIMsamNw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*utK4qDMRrvcQb1E2-N3m5Q.png" /></figure><p>Cloning the CodeCommit repository on my local machine then I can copy the source code into the folder, configure the git file and push to origin</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Eiz57Qx6PrDA8JrdKiSv8A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k_IPokO401POdP6FQ9rdtQ.png" /></figure><h3>Building the artifact with CodeBuild</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bT0QQA-TwhVX2aIDSFV1gA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4RImP9l6C4SMW7AUP6GsLQ.png" /></figure><p>The Source is set to the CodeCommit repo set earlier and the CodeBuild is running on an Ubuntu OS with the following build specification commands</p><pre>version: 0.2<br><br><br>phases:<br>  install:<br>   runtime-versions:<br>      java: corretto8<br>  pre_build:<br>    commands:<br>      - apt-get update<br>      - wget https://downloads.apache.org/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.tar.gz<br>      - tar xzf apache-maven-3.8.8-bin.tar.gz<br>      - ln -s apache-maven-3.8.8 maven<br>      - sed -i &#39;s/jdbc.password=admin123/jdbc.password=bUa1wyoXoFJDmx2xCpUd/&#39; src/main/resources/application.properties<br>      - sed -i &#39;s/jdbc.username=admin/jdbc.username=admin/&#39; src/main/resources/application.properties<br>      - sed -i &#39;s/db01:3306/vprofile-rds.cygzxzvqjts5.us-east-1.rds.amazonaws.com:3306/&#39; src/main/resources/application.properties<br>  build:<br>    commands:<br>      - mvn install<br>artifacts:<br>  files:<br>     - &#39;**/*&#39;<br>  base-directory: &#39;target/vprofile-v2&#39;</pre><p>In the install phase it installs the package corretto 8, in the pre build phase it runs a list of commands; first to update the package list, then download a maven binary, extract it and link it, then it searches for the database values and replaces it with the database values. In the build phase it runs mvn install which creates the artifact. Then the artifact phase which states the base directory for the artifact</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jhg7UDEyUEk83nZIQTwGPw.png" /></figure><p>The atrtifact is uploaded to an s3 bucket</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MOp5zOvYLlqUOivrIqmndA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FwIrcoH98xhUiOQerxt0-Q.png" /></figure><h3>Building the pipeline using CodePipeline</h3><p>All the services are then conncected using the CodePipeline</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-vrQhO6h4_MCb_AO5DqYcQ.png" /></figure><p>The source provider is set to the CodeCommit repository built earlier, so when there are new commit to the repository so the pipeline can be triggered</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_VegccqFjHKELOGfM03SZQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*btcFwxCx_xr5k58XlXBKvw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1xjka2Sf8kQvVNSHDvuRbQ.png" /></figure><h3>Results</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AGo_m2_ix1BgLfMDecgxFg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3rISOAyZ-RQakp8TNtdu9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gNr5zu4tsFbN5ctvUDH7sA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MjJqX_KcLmtGyEbr7-_3WA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b46cfe7a0fcf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Containerizing a micro service application]]></title>
            <link>https://medium.com/@cokerrd/containerizing-a-micro-service-application-f2dcda7ebf24?source=rss-afc16a5a9a10------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2dcda7ebf24</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devops-practice]]></category>
            <dc:creator><![CDATA[Richard Coker]]></dc:creator>
            <pubDate>Sat, 24 Feb 2024 08:59:17 GMT</pubDate>
            <atom:updated>2024-02-24T08:59:17.177Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZEIbARPzNgYRhLvXh6lAJQ.png" /><figcaption>Micro service application architecture</figcaption></figure><p><strong>Overview :</strong> This project contains 4 services:</p><ul><li>API gateway— This api gateway runs on Nginx service, listens and routes the requests based on the urls, this is how the other services in the application communicate</li><li>Emart api — This backend service is written in NodeJS and uses a MongoDB database</li><li>Books api- This backend service is written in Java and uses a MySQL database</li><li>Client- The front end of the application is built using angular</li></ul><p><strong>Objective:</strong> Containerize the web application using Docker</p><p>The source code can be found<a href="https://github.com/Ozii-cr/Emart-App"><strong> here</strong></a></p><p>Steps:</p><ol><li>Write the Docker files for the Emart api, Books api and client</li><li>Write the Docker compose file including the images for the mongoDB, MySQl and nginx</li></ol><h3>Docker file for the Angular Application</h3><p>The docker file for the angular application is set up as a multistage Docker file so it can install all the dependencies and then build the application in the first stage and then serve the application using nginx in the second stage.</p><p>The nginx configuration file listens on port 4200 and serves the application on ‘/’</p><pre>#Stage 1: Build the AngularJS application<br>FROM node:14 as builder<br><br>#set the working directory <br>WORKDIR /usr/src/app<br><br># Copy the entire application to the working directory<br>COPY ./ ./<br><br># Install dependencies and build the artifact<br>RUN npm install &amp;&amp; npm run build --prod<br><br>#Stage 2: Serve the application using Nginx<br>FROM nginx:latest<br><br># Copy the built AngularJS application from the builder <br>COPY --from=builder /usr/src/app/dist/client /usr/share/nginx/html<br><br># Copy the Nginx configuration file<br>COPY nginx.conf /etc/nginx/nginx.conf<br><br># Expose the port the app runs on<br>EXPOSE 4200</pre><h3><strong>Docker file for the NodeJS API</strong></h3><p>The node js api docker file is a multistage docker file which installs the dependencies and builds the artifact in the first stage, and then copies the aritfact in the second stage and runs the application</p><pre># Stage 1: Use an official Node.js image as the base image to build the artifact<br>FROM node:14 as nodeapi-build<br><br># Set the working directory in the container<br>WORKDIR /usr/src/app<br><br># Copy the entire application to the working directory<br>COPY ./ ./<br><br># Install dependencies<br>RUN npm install<br><br>#Stage 2: Copy the artifact<br>FROM node:14<br><br># Set the working directory in the container<br>WORKDIR /usr/src/app<br><br>#copy the artifact from the build stage<br>COPY --from=nodeapi-build /usr/src/app/ ./<br><br># Expose the port the app runs on<br>EXPOSE 5000<br><br># Define the command to run the application<br>CMD [&quot;npm&quot;, &quot;start&quot;]</pre><h3>Docker file for Java API</h3><p>The docker file for the java api is a multi stage docker file where in the first stage builds the artifact and then copies it in the second stage similar to the nodejs dockerfile and this is good practice because it reduces the size of the image</p><pre>#Stage 1: Use an official OpenJDK image as the base image to build the artifact<br>FROM openjdk:8 as build<br><br># Set the working directory in the container<br>WORKDIR /usr/src/app<br><br># Download and install Maven dependencies<br>RUN apt update &amp;&amp; apt install -y maven <br><br># Copy the entire application to the working directory<br>COPY ./ ./<br><br># Build the Java application while skipping the tests<br>RUN mvn install -Dskiptests<br><br>#Stage 2: Copy the artifact<br>FROM openjdk:8 <br><br># Set the working directory in the container<br>WORKDIR /usr/src/app<br><br>#copy the artifact from the build stage<br>COPY --from=build /usr/src/app/target/book-work-0.0.1-SNAPSHOT.jar ./book-work-0.0.1.jar<br><br><br># Expose the port the API will run on<br>EXPOSE 9000<br><br>#  command to run the Java application<br>ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;book-work-0.0.1.jar&quot;]</pre><p>Nginx API gateway configuration file</p><p>The configuration file simply just has three routing rules, one for the ‘/’ path which routes to the angular application, the second on ‘/api’ which routes to the node js api and then on ‘/webapi’ which routes to the java api</p><pre><br>upstream client {<br>    server client:4200;<br>}<br>server {<br>    listen 80;<br>    location / {<br>        proxy_set_header Host $host;<br>        proxy_set_header X-Real-IP $remote_addr;<br>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>        proxy_set_header X-Forwarded-Proto $scheme; <br><br>        proxy_http_version 1.1;<br>        proxy_set_header Upgrade $http_upgrade;<br>        proxy_set_header Connection &quot;upgrade&quot;;<br>        proxy_pass http://client/;<br>    }<br>    location /api {<br>        proxy_set_header Host $host;<br>        proxy_set_header X-Real-IP $remote_addr;<br>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>        proxy_set_header X-Forwarded-Proto $scheme; <br>        proxy_http_version 1.1;<br><br>        proxy_pass http://api:5000;<br>    }<br>    location /webapi {<br>        proxy_set_header Host $host;<br>        proxy_set_header X-Real-IP $remote_addr;<br>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>        proxy_set_header X-Forwarded-Proto $scheme;<br>        proxy_pass http://webapi:9000;<br>    }<br>}</pre><h3>Docker Compose file</h3><p>The client, api and webapi services build the images from the dockerfiles in their respective paths.</p><p>The client container which is the angular application depends on the both the api and web api services to be running before it can execute, like wise the api depends on the nginx gateway service and the mongdb database it should be connected to, and the web api depends on the mysql database as well.</p><p>The values for the environment variables set for both the mongodb database and mysql database images are gotten from their configuration files ‘nodeapi/config/keys.js’ and ‘javaapi/src/main/resources/application.properties’</p><pre>version: &#39;3.8&#39;<br>services:<br>    client:<br>        build:<br>            context: ./client<br>        ports:<br>            - &quot;4200:4200&quot;<br>        container_name: client<br>        depends_on:<br>            [api, webapi]<br><br>    api:<br>        build: <br>            context: ./nodeapi<br>        ports:<br>            - &quot;5000:5000&quot;<br>        container_name: api<br>        depends_on:<br>            [nginx, emongodb]<br><br>    webapi:<br>        restart: always<br>        build: <br>            context: ./javaapi<br>        ports:<br>            - &quot;9000:9000&quot;<br>        container_name: webapi<br>        depends_on:<br>            [emysqldb]<br>    <br>    nginx:<br>        restart: always<br>        image: nginx:latest<br>        container_name: nginx<br>        ports:<br>            - &quot;80:80&quot;<br>        volumes:<br>            - &quot;./nginx/default.conf:/etc/nginx/conf.d/default.conf&quot;<br>        command: [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]<br>        depends_on:<br>            [client]<br><br>    emongodb:<br>        image: mongo:4<br>        container_name: emongodb<br>        restart: always<br>        ports:<br>            - &quot;27017:27017&quot;<br>        environment:<br>            MONGO_INITDB_DATABASE: epoc<br>    <br>    emysqldb:<br>        image: mysql:5.7<br>        container_name: emysqldb<br>        restart: always<br>        ports:<br>          - &quot;3306:3306&quot;<br>        environment:<br>          MYSQL_ROOT_PASSWORD: emysqldbpass<br>          MYSQL_DATABASE: books</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ut3RFInJK45nEpwDAFoSHw.png" /></figure><p>To run the containerised application you simply run</p><pre>docker-compose up</pre><p>and to stop the containers from running</p><pre>docker-compose down</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XzoAIptNpPg6-cMWWDh06w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tWc1anyP65hHQTBjUKDsqA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IurmM-RyHLSJrVcMofgnlw.png" /></figure><p>The source code can be found<a href="https://github.com/Ozii-cr/Emart-App"><strong> here</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2dcda7ebf24" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>