<?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: John Patrick Echavez</title>
    <description>The latest articles on DEV Community by John Patrick Echavez (@patrickechavez).</description>
    <link>https://dev.to/patrickechavez</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%2F1232887%2Fc638203e-de1e-4955-a68a-ebb69e9e5cd8.jpeg</url>
      <title>DEV Community: John Patrick Echavez</title>
      <link>https://dev.to/patrickechavez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patrickechavez"/>
    <language>en</language>
    <item>
      <title>Docker Tutorial</title>
      <dc:creator>John Patrick Echavez</dc:creator>
      <pubDate>Fri, 29 Mar 2024 14:39:45 +0000</pubDate>
      <link>https://dev.to/patrickechavez/docker-tutorial-4mb3</link>
      <guid>https://dev.to/patrickechavez/docker-tutorial-4mb3</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Create a Docker Compose&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In the root folder, create a file with the name docker-compose.yaml&lt;/li&gt;
&lt;li&gt;Type this code inside the docker-compose.yaml file.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "4.28"
services:
  api:
    build: ./api
    container_name: api_container
    ports:
      - '4000:4000'
    volumes:
      - ./api:/app
      - /app/node_modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Version: "4.28"&lt;/strong&gt; is the version of the Docker desktop currently downloaded on your computer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;build: ./api&lt;/strong&gt; is the path of your project. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;container_name: api_container&lt;/strong&gt; is the name of the container in your api project folder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ports: - '4000:4000'&lt;/strong&gt; are ports of both your local and container. The left side port is for the local port, and the right side is for the container port.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;- ./api:/app&lt;/strong&gt; &lt;strong&gt;./api&lt;/strong&gt; is the directory path of your local project, and &lt;strong&gt;/app&lt;/strong&gt; is the directory path of your project inside the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;- ./app/node_modules&lt;/strong&gt; Adding the second volume path is to ignore the directory path if the user accidentally deletes the file. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the docker-compose.yaml&lt;br&gt;
This code create a container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stop the container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker compose down  --rmi all -v 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;--rmi all&lt;/strong&gt; to remove the all the images created from the docker compose&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-v&lt;/strong&gt; to delete all the volumes created from the docker compose.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Create a Docker image file with Layer Cache&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a file named &lt;strong&gt;DockerFile&lt;/strong&gt; and copy the code attached below inside the &lt;strong&gt;DockerFile&lt;/strong&gt; file
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:17-alpine
RUN npm install -g nodemon
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 4000
# required for docker desktop port mapping
CMD ["npm", "run", "dev"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. WORKDIR&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WORKDIR&lt;/strong&gt; is code that will create a directory inside the image. So based on the code example above, inside the Docker image, the source code will be stored in the app directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. EXPOSE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;EXPOSE&lt;/strong&gt; is a code to indicate a port number inside the container. So based on the code example above, in the container, the port number of your application is &lt;strong&gt;4000&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This code is &lt;strong&gt;optional&lt;/strong&gt;. This can be used as a reference in Docker Desktop when this DockerFile is created&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Create a Docker ignore&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;To make Docker ignore a file, create a file and name it &lt;strong&gt;.dockerignore&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Inside the &lt;strong&gt;.dockerignore&lt;/strong&gt; file, In each line, indicate the name of the directory or file that the user wanted to ignore.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Explanation: &lt;/p&gt;

&lt;p&gt;In the current application, if there are a lot of directories that are optional to copy inside the Docker, you need to ignore them because they will become slow when creating a Docker image.&lt;/p&gt;

&lt;p&gt;These are the following example files that are not necessary to move in the Docker:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;node_modules&lt;/li&gt;
&lt;li&gt;*.md&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Create a Docker image.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the terminal, type this command: &lt;strong&gt;docker build -t myapp .&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-t&lt;/strong&gt; is the tag that will be shown in the Docker Desktop&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;myapp&lt;/strong&gt; is the name of the Docker image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Create a docker image with version 1 as a tag.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t myapp:v1 .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Run the Docker image and automatically create a container.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --name myapp_c2 myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;myapp_c2&lt;/strong&gt; is the name of a container that has not been created yet. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;myapp&lt;/strong&gt; is the name of the Docker image you want to run.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Create and Run the Docker image, add a port for both local and container, and automatically create a container.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --name myapp_container -p 4000:4000 myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;myapp_container&lt;/strong&gt; is the name of a container that has not been created yet. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;myapp&lt;/strong&gt; is the name of the Docker image you want to run.&lt;/li&gt;
&lt;li&gt;The port number on the left is for the local port. &lt;/li&gt;
&lt;li&gt;The port number on the right is for the container port.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Create and Run a container with a specific version of an image&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --name myapp_container -p 4000:4000 myapp:v1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;myapp_container&lt;/strong&gt; is the name of a container that has not been created yet. &lt;/li&gt;
&lt;li&gt;The port number on the left is for the local port. &lt;/li&gt;
&lt;li&gt;The port number on the right is for the container port.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;myapp&lt;/strong&gt; is the name of the Docker image you want to run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;v1&lt;/strong&gt; is the tag to be written in the &lt;strong&gt;Tag Column.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Create and Run a container with a docker volume feature&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; The docker volume is to map the source code files on local and in the docker image. If there are changes in the source code in the local file, the source code in the Docker image will also change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --name myapp_container -p 4000:4000 --rm  -v /Users/ikuhironakagawa/Projects/docker-crash-course/api:/app -v /app/node_modules myapp:nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;-v /Users/ikuhironakagawa/Projects/docker-crash-course/api&lt;/strong&gt; is the local path of the sourcode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;:/app&lt;/strong&gt; is the image directory path of the source code inside.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-v /app/node_modules&lt;/strong&gt; is to ignore the files if there are changes in the local, and it will not refer to the Docker image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Show all the docker images&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Delete a docker image&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; You cannot delete a Docker image that is attached to or used in a container. So, destroy the container first, then the Docker image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker image rm mypp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;strong&gt;Show all containers and docker images&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Show all running containers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Run the existing container&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- docker ps -a
 //display all containers first to get the container name

- docker start myapp_container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stop the running container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- docker ps -a
 //Display all the running containers first to get the container name.

- docker stop myapp_container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Delete a container&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- docker ps -a
 //Display all the running containers first to get the container name.

- docker rm myapp_container

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Delete multiple container&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- docker ps -a
 //Display all the running containers first to get the container name.

- docker rm myapp_container myapp2_container

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Delete all containers, images and volumes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker system prune -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>[macOS] Prefix your current Git branch name to your Git commit message.</title>
      <dc:creator>John Patrick Echavez</dc:creator>
      <pubDate>Thu, 14 Dec 2023 01:46:03 +0000</pubDate>
      <link>https://dev.to/patrickechavez/prefix-your-current-git-branch-name-to-your-git-commit-message-2oal</link>
      <guid>https://dev.to/patrickechavez/prefix-your-current-git-branch-name-to-your-git-commit-message-2oal</guid>
      <description>&lt;p&gt;Automatically prefixing your commit messages with a ticket number is a good practice, especially if you are working with a team or using an issue tracking system. One way to achieve this is by using &lt;strong&gt;Git hooks&lt;/strong&gt;. &lt;strong&gt;Git hooks&lt;/strong&gt; are scripts that Git executes before or after certain events, such as committing or pushing. Here's a simple example of a Git hook that automatically adds a ticket number to your commit message. This example assumes that your ticket numbers are in the form of, for example, &lt;strong&gt;hotfix/ABC-123&lt;/strong&gt;, and you want to use this format in your commit messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open your terminal.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Navigate to the root directory of your Git repository.&lt;/p&gt;

&lt;p&gt;Create a file named &lt;strong&gt;prepare-commit-msg&lt;/strong&gt; in the &lt;strong&gt;.git/hooks/&lt;/strong&gt; directory. If it doesn't exist, you can create it. Make sure the file is executable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch .git/hooks/prepare-commit-msg
chmod +x .git/hooks/prepare-commit-msg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To open the &lt;strong&gt;prepare-commit-msg&lt;/strong&gt; file using &lt;strong&gt;TextEdit&lt;/strong&gt; from the terminal, you can use the open command. Here's the command you can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;open -e .git/hooks/prepare-commit-msg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a text editor, and add the following script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

COMMIT_MSG_FILE=$1

# Check if the commit message contains a ticket number
if ! grep -q "^\[[A-Za-z]+/[0-9]+\]" "$COMMIT_MSG_FILE"; then
    # Extract the feature name from the current branch name
    BRANCH_NAME=$(git symbolic-ref --short HEAD)

    # Add the branch name to the beginning of the commit message
    sed -i.bak -e "1s|^|\[$BRANCH_NAME\]  |" "$COMMIT_MSG_FILE"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can check if the prepare-commit-msg file is executable by using the ls command with the -l option, which displays detailed information about files. Navigate to the .git/hooks/ directory and run:&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 prepare-commit-msg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will show you detailed information about the file, including its permissions. If the file is executable, you should see an "x" in the permission section. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-rwxr-xr-x 1 user group 1234 Dec 14 12:34 prepare-commit-msg

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting commit message should now look like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[feature/header_add_nav_bar] add nav bar.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
  </channel>
</rss>
