<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nandes Simanjuntak</title>
    <description>The latest articles on DEV Community by Nandes Simanjuntak (@nandes007).</description>
    <link>https://dev.to/nandes007</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1007911%2Fae79216d-f008-41ff-adee-2dc5ad87507e.jpeg</url>
      <title>DEV Community: Nandes Simanjuntak</title>
      <link>https://dev.to/nandes007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nandes007"/>
    <language>en</language>
    <item>
      <title>Deploying Golang and Angular Applications with Docker and Nginx</title>
      <dc:creator>Nandes Simanjuntak</dc:creator>
      <pubDate>Tue, 23 Apr 2024 14:09:31 +0000</pubDate>
      <link>https://dev.to/nandes007/deploying-golang-and-angular-applications-with-docker-and-nginx-2bne</link>
      <guid>https://dev.to/nandes007/deploying-golang-and-angular-applications-with-docker-and-nginx-2bne</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Welcome to my blog! In this article, I've documented the process of deploying a Golang backend and Angular frontend application using Docker and Nginx. This tutorial will walk you through the steps to set up a production-ready environment for your Golang and Angular applications.&lt;/p&gt;

&lt;p&gt;Project Repository&lt;br&gt;
You can find the code for this project on GitHub: &lt;a href="https://github.com/nandes007/image-converter"&gt;Project Repository&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My Image Converter Application&lt;br&gt;
Check out the deployed application to see it in action: &lt;a href="https://my-converter.ninedaystech.com/"&gt;My Image Converter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Personal Website&lt;br&gt;
Visit my personal website to learn more about me and my other projects: &lt;a href="https://ninedaystech.com/"&gt;ninedaystech.com&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Choosing your server provider
&lt;/h2&gt;

&lt;p&gt;Before diving into deployment, the first step is to select a reliable server provider. There are several options available, including Google Cloud Platform (GCP), Amazon Web Services (AWS), and others. For this guide, i've chosen DigitalOcean as my server provider for its simplicity, affordability, and robust features. &lt;/p&gt;

&lt;p&gt;To get started with DigitalOcean, you can follow these steps: &lt;a href="https://www.digitalocean.com/community/tags/getting-started"&gt;https://www.digitalocean.com/community/tags/getting-started&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Prepare your project
&lt;/h2&gt;

&lt;p&gt;Let's take an example with a deployment that use docker compose:&lt;/p&gt;

&lt;p&gt;Below &lt;strong&gt;Dockerfile&lt;/strong&gt; for backend side, like i have mentioned above i develop my simple application using go for the backend&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# syntax=docker/dockerfile:1

ARG GO_VERSION=1.22

FROM golang:${GO_VERSION}-alpine

ENV GO_ENV production

WORKDIR /usr/src/backend

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -o main ./cmd

EXPOSE 9090

CMD ["./main"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for the frontend i develop my simple application using &lt;strong&gt;Angular&lt;/strong&gt;. Below &lt;strong&gt;Dockerfile&lt;/strong&gt; for frontend side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:20-alpine AS build

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm cache clean --force

RUN npm install -g @angular/cli

RUN npm install
RUN npx ngcc --properties es2023 browser module main --first-only --create-ivy-entry-points

COPY . .

RUN npm run build

FROM nginx:stable

COPY --from=build /usr/src/app/dist/frontend/browser /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the frontend side, i did &lt;a href="https://docs.docker.com/guides/docker-concepts/building-images/multi-stage-builds"&gt;multi-stage&lt;/a&gt; builds in my dockerfile. Because i use &lt;a href="https://docs.docker.com/guides/docker-concepts/building-images/multi-stage-builds/"&gt;multi-stage&lt;/a&gt; which is my angular application will run on nginx image, so i need add configuration file for nginx. The configuration file named as &lt;em&gt;nginx.conf&lt;/em&gt;. Below the example for nginx configuration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        root   /usr/share/nginx/html;
        index  index.html index.htm;

        location / {
            try_files $uri $uri/ /index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then i will create that image and run the container using docker compose. Below my docker compose configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services:
  backend:
    build:
      context: ./backend
    ports:
      - 9090:9090
    volumes:
      - ./backend/converted_images:/go/src/app/converted_images

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - 8081:80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I will run my backend using port 9090 and port 8081 for my frontend. I used port forwarding for my frontend because my nginx run on port 80.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment section
&lt;/h2&gt;

&lt;p&gt;Okay, let's deploy our project. First you need connect to your server/droplets. Open your terminal and run command like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh username@your_ip_address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, you need to install docker and nginx in your server. If you using digital ocean maybe this can help you to install docker and nginx: &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04"&gt;docker installation guide for ubuntu&lt;/a&gt; and &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04"&gt;nginx installation guide for ubuntu&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clone Project
&lt;/h3&gt;

&lt;p&gt;Begin by cloning your project onto your server. I typically place my projects in the &lt;code&gt;/var/www/html&lt;/code&gt; directory. Once the cloning is complete, navigate to the project directory and run the following command to start your project: &lt;code&gt;docker compose up -d&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CONTAINER ID   IMAGE                      COMMAND                  CREATED        STATUS        PORTS                                       NAMES
c93969a04b43   image-converter-backend    "./main"                 46 hours ago   Up 46 hours   0.0.0.0:9090-&amp;gt;9090/tcp, :::9090-&amp;gt;9090/tcp   image-converter-backend-1
3856213fb4f5   image-converter-frontend   "/docker-entrypoint.…"   46 hours ago   Up 46 hours   0.0.0.0:8081-&amp;gt;80/tcp, :::8081-&amp;gt;80/tcp       image-converter-frontend-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure NGINX
&lt;/h3&gt;

&lt;p&gt;After installation done, next we need to tell NGINX to listen to port 80 (http requests) for our custom domain name and map it back to the port we configured in our &lt;code&gt;docker-compose.yml&lt;/code&gt; file and our app’s production config.&lt;/p&gt;

&lt;p&gt;SSH into your server and create a new file in &lt;code&gt;/etc/nginx/sites-available/&lt;/code&gt; (you can name it yourdomain.com) with the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
  listen        80;
  server_name   yourdomain.com www.yourdomain.com;
  location / {
    proxy_pass  http://localhost:9090;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we need to create a symlink to the file for the &lt;code&gt;/sites-enabled&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/yourdomain.com ./
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check that the symlink was created successfully:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls -l

# output:
... yourdomain.com -&amp;gt; /etc/nginx/sites-available/yourdomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next check that the NGINX syntax is OK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t

# output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally restart the service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: The above step is only for backend, you can follow the same step and enter the &lt;code&gt;proxy_pass&lt;/code&gt; with appropriate address&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You've successfully deployed your project to your server. Now, let's access your newly deployed application and ensure that all features are running smoothly. You can find &lt;a href="https://github.com/nandes007/image-converter"&gt;the project repository&lt;/a&gt; and &lt;a href="https://my-converter.ninedaystech.com/"&gt;the final result&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope this guide has been helpful in simplifying the deployment process for you. If you have any questions, feedback, or suggestions for improvement, please don't hesitate to leave a comment below. Your input is valuable and will help us enhance future tutorials.&lt;/p&gt;

&lt;p&gt;Thank you for reading, and happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Docker Basic</title>
      <dc:creator>Nandes Simanjuntak</dc:creator>
      <pubDate>Wed, 04 Oct 2023 00:14:25 +0000</pubDate>
      <link>https://dev.to/nandes007/docker-basic-5da2</link>
      <guid>https://dev.to/nandes007/docker-basic-5da2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Docker is one of the most popular Container Manager that introduced around 2013. Docker is a free and open source application, so that we can use it for free. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker's methodologies for shipping, testing, and deploying code, you can significantly reduce the delay between writing code and running it in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Docker platform
&lt;/h2&gt;

&lt;p&gt;Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security lets you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you don't need to rely on what's installed on the host. You can share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.&lt;/p&gt;

&lt;p&gt;Docker provides tooling and a platform to manage the lifecycle of your containers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Develop your application and its supporting components using containers.&lt;/li&gt;
&lt;li&gt;The container becomes the unit for distributing and testing your application.&lt;/li&gt;
&lt;li&gt;When you're ready, deploy your application into your production environment, as a container or an orchestrated service. This works the same whether your production environment is a local data center, a cloud provider, or a hybrid of the two.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Docker Architecture
&lt;/h2&gt;

&lt;p&gt;Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. When we install Docker, usually it already contains Docker Client and Docker Daemon. The Docker client and daemon can run on the same system and the Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frrq5c5v0wdkx99xcuy8q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frrq5c5v0wdkx99xcuy8q.png" alt="Image description" width="800" height="422"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# command that you can use to check version of docker
docker version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker Registry
&lt;/h2&gt;

&lt;p&gt;Docker Registry is where we store Docker Images, with Docker Registry we can store image that we have made and it will can use in Docker Demon anywhere as long as it can connect to Docker Registry.&lt;/p&gt;

&lt;p&gt;Example Docker Registry:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Docker Hub : &lt;a href="https://hub.docker.com/"&gt;https://hub.docker.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Digital Ocean Container Registry : &lt;a href="https://www.digitalocean.com/products/container-registry/"&gt;https://www.digitalocean.com/products/container-registry/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;Google Cloud Container Registry : &lt;a href="https://cloud.google.com/container-registry"&gt;https://cloud.google.com/container-registry&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Docker Image
&lt;/h2&gt;

&lt;p&gt;Docker Image is similar to an application installer, where in Docker Image there are applications and dependencies. Before we run application in docker, we need to be sure, that have Docker Image from that application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# To show list docker image
docker image ls

# To download Docker Image from Docker Registry
# You can use http://hub.docker.com/ to find image that you want use
docker image pull imagename:tag

# Example to pull image
docker image pull redis:latest

# Remove Docker Image
docker image rm redis:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker Container
&lt;/h2&gt;

&lt;p&gt;A container is a runnable of an image. If a Docker Image is like an application installer, then a Docker Container is similar to an application from the installer. One image can use for a lot container with different Docker Container name. When you make Docker Container from Docker Image, the image can't to remove. You must be remove your container firstly then you can remove your image as you want. By default when we create container, the container is not running, we must run it firstly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# To show list docker container
docker container ls

# Create container
# Note: exampleredis is name of container and
# redis:lates is the image and tag that want we use in container
docker container create --name exampleredis redis:latest

# Run container
docker container start exampleredis

# Stop container
docker container stop exampleredis

# Remove container
docker container rm exampleredis

# View log in container
docker container logs exampleredis

# Execute bash script in container
docker container exec -i -t exampleredis /bin/bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Container Port
&lt;/h2&gt;

&lt;p&gt;When we run container, a container is relatively isolated from other containers and its host machine. Docker has the ability to do port forwarding, that is forwarding a port on the host system to a Docker Container when we create the container. This method is suitable if we want to expose the ports in the container to the outside via the host system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Command to forward port in container to system host.
docker container create --name containername --publish posthost:portcontainer image:tag

# Ex: forward nginx port in container
# Example below nginx using port 80 in container and we publish it to port 8080 to system host.
docker container create --name nginxcontainer --publish 8080:80 nginx:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Environment Varible
&lt;/h2&gt;

&lt;p&gt;We can use the environment variables in Docker to dynamically configure the application. We can pass --env as a param when we create a container and fill your configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Pass --env parameter as environment variable
docker container create --name containername --env KEY=value --env KEY2=value image:tag

# Here the example when we use mongo db image in container and pass the environment variable.
docker container create --name mongocontainer --env MONGO_INITDB_ROOT_USERNAME=root --env MONGO_INITDB_ROOT_PASSWORD=password mongo:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Container Resource Limit
&lt;/h2&gt;

&lt;p&gt;When we create container, by default will use all CPU and Memory that given to Docker on Mac and Windows, and will use all CPU and Memory that available in the Linux system host. When one of container to many use CPU and Memory, it will affect to performance in other container or maybe the system host. So that it better to use resource limit when we create container. Pass --memory or --cpus as argument when create container, example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Implement resource limit using nginx image
docker container create --name containername --publish 8081:80 --memory 100m --cpus 0.5 nginx:latest

# If you want to view resource usage, use:
docker container stats
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bind Mounts
&lt;/h2&gt;

&lt;p&gt;Bind Mounts is the ability to mount (share) files or folders on the host system to containers on docker. This feature is very useful when we want to send configurations from outside the container, or for example save data created in the application inside the container into a folder on the host system. If a file or folder does not exist on the host system, it will be automatically created by Docker. To mount, we can use the --mount parameter when creating the container and the contents of the --mount parameter have their own rules.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>learning</category>
      <category>beginners</category>
      <category>go</category>
    </item>
  </channel>
</rss>
