XeusNguyen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # DevOps Training Session 4: Docker ###### tags: `devops` `research` `reliable` Hello btb my blog, today i will make some lab relate about to setup docker and try to make project with docker through docker_script and docker compose to bunch up all image into one, Hope you enjoy it --> [:coffee:](https://drive.google.com/file/d/1kmv_2zlP1xY9MOoRFSvibVOBLAYqib8G/view?usp=sharing) 1. Write a Dockerfile that: - Use alpine as the base image - Set working directory as /app - Install git, zip, curl - Install NodeJS via NVM (node version must be parameterized) ==> **I have some problems with nvm-nodejs-npm on export the PATH --> solution is: Try to get nodejs and npm from CDN of alpine --> Make it successfull** ``` FROM alpine:latest WORKDIR /app # Get the require package RUN apk add -U curl bash ca-certificates openssl ncurses coreutils python3 make gcc g++ libgcc linux-headers grep util-linux binutils findutils git zip # Create a folder for storage nvm RUN mkdir /usr/local/nvm # Init version for pass through parameter by command ARG version=${verison} # Show what version is apply RUN echo ${version} # export env for install nvm ENV NVM_DIR /usr/local/nvm ENV NODE_VERSION ${version} #### Runable but path for export have smt wrong !! #### #====================================================================================== # Install nvm with node and npm # RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash \ # && . $NVM_DIR/nvm.sh \ # && nvm install $NODE_VERSION \ # && nvm alias default $NODE_VERSION \ # && nvm use default # ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules # ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH #====================================================================================== # Check the version input and apply package for specically option 12 or 14 RUN if [[ ${version}=="12" ]]; then apk add --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/v3.12/main/ nodejs npm; fi RUN if [[ ${version}=="14" ]]; then apk add --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/v3.14/main/ nodejs npm; fi # check the node and npm install into that one RUN node --version RUN npm --version ``` 2. Write a Powershell script and a Bash script that build the above Dockerfile into several images (must tag the image): - Node 14 image - Node 12 image Bash: ``` #!/bin/bash # read -p "Which node image do you want to work: " version if [[ $1 != "12" && $1 != "14" ]] then echo "$1 is not supported" exit 1 else docker build -t nodejs:lastestv$1 --build-arg version=$1 . fi ``` Powershell: ``` #!/usr/bin/pwsh param( [string]$version ) # $version = Read-Host -prompt "What version of node you want to apply" if ($version -eq "12" -or $version -eq "14") { docker build -t nodejs:lastestv$version --build-arg version=$version . } else { Write-Host "$version is not supported" } ``` ![](https://i.imgur.com/5GNqnnw.png) 3. Write a Dockerfile that: - Uses Node 14 image above - Clone this repo https://github.com/johnpapa/node-hello into the working directory - Build project - Run test only when the container is started - Be able to access the web from the host machine Dockerfile: ``` FROM nodejs:lastestv14 # Clone git hub repo to work dir RUN git clone https://github.com/johnpapa/node-hello.git /app # Build Project RUN npm install # Expose port for enable access port 3000 EXPOSE 3000 # RUN Project CMD ["npm", "start"] ``` Run Command `docker build -t <name_image>:<tag> PATH` `docker run -d --name <name> -p <localport>:<dockerport> <nameimage>` Example ``` docker build -t nodejs:node_hello /home/xeusnguyen/DevOps_OrientTrainningSession/Node_project_hello/ docker run -h nodejs_hello -p 3000:3000 nodejs:node_hello ``` ![](https://i.imgur.com/EjTKi2g.png) 4. Write a Powershell script and a Bash script that run the above Dockerfile (must set the container name) then check if the local web server is running Bash ``` #!/bin/bash # Access to Dockerfile [Folder contain that] --> Path specify read -p "where of docker file you want to executable: " dkfilepath if [[ -d $dkfilepath ]] then read -p "which name you want to apply in container: " namecontainer docker build -t nodejs:node_hello $dkfilepath docker run -d --name $namecontainer -p 3000:3000 nodejs:node_hello $(timeout 1 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/3000') if [[ $? -eq 0 ]] then echo "Accesible into web docker" else echo "Not accesiable into web docker" fi else echo "$dkfilepath is not a folder --> Make sure this it folder and contain Dockerfile you want to execute" fi ``` Powershell ``` #/usr/bin/pwsh $pthdkfile = Read-Host -p "Which path contain dockerfile" if (Test-Path -Path $pthdkfile) { $containername = Read-Host -p "Which name you want to apply for container" docker build -t nodejs:node_hello $pthdkfile docker run -d --name $containername -p 3000:3000 nodejs:node_hello $connection = New-Object System.Net.Sockets.TcpClient("127.0.0.1", "3000") if ($connection.Connected) { Write-Host "Accesible into web docker" } else { Write-Host "Not accesiable into web docker" } } else { Write-Host "$pthdkfile is not a folder --> Make sure this it folder and contain Dockerfile you want to execute" } ``` 5. Write a Powershell script and a Bash script that run a script inside the web container to check if the Node app is running Bash ``` #!/bin/bash read -p "name container of web page: " namecontainer result=$(docker exec -it $namecontainer curl 127.0.0.1:3000 || set -e) if [[ $result ]] then echo "The container can accesible inside --> Return from container $result" fi ``` Powershell ``` #!/usr/bin/pwsh $nameContainer = Read-Host -prompt "What name of container" $result = docker exec -it $nameContainer curl 127.0.0.1:3000 if ($result -ne "") { Write-Host "Docker can accesible inside and it return for us is $result" } ``` 6. Write a Powershell script and a Bash script that remove all containers and images Bash ``` #!/bin/bash docker stop $(docker ps -aq) docker rm $(docker ps -aq) docker rmi $(docker images -aq) ``` Powershell ``` #!/usr/bin/pwsh docker stop $(docker ps -aq) docker rm $(docker ps -aq) docker rmi $(docker images -aq) ``` 7. Create a docker-compose.yaml file that: - Contains 2 services, a Node 12 app, and a Node 14 app. Using Dockerfiles and images above - Create an index.js similar to this file but change the message - Override the index.js in the Node 12 app with the above index.js Docker-compose file ``` services: nodejs12-web: build: dockerfile: /home/xeusnguyen/DevOps_OrientTrainningSession/Node_project_hello_v12/Dockerfile context: . ports: - '3000:3000' nodejs14-web: build: dockerfile: /home/xeusnguyen/DevOps_OrientTrainningSession/Node_project_hello_v14/Dockerfile context: . ports: - '3001:3000' ``` 8. Write a Powershell script and a Bash script that compose the above docker-compose.yaml file then check if the local web servers is running Bash: ``` #!/bin/bash # Create node12 cd Node_create || exit ./bash_node.sh 12 # Create node14 ./bash_node.sh 14 # Return create a 2-web node12 vs node14 with docker compose cd .. || exit docker-compose up --build --detach # Check the server is exist echo "==========================================================" echo "=================Status Container Below===================" echo "==========================================================" $(timeout 1 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/3000') if [[ $? -eq 0 ]] then sleep 2 result=$(curl -L 127.0.0.1:3000) echo "Server is running in port 3000, and return this message $result" else echo "Server is not running in port 3000" fi $(timeout 1 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/3001') if [[ $? -eq 0 ]] then sleep 2 result=$(curl -L 127.0.0.1:3001) echo "Server is running in port 3001, and return this message $result" else echo "Server is not running in port 3001" fi ``` Powershell ``` #!/usr/bin/pwsh # Create node12 Set-Location Node_create ./pwsh_node.ps1 -version 12 # Create node14 ./pwsh_node.ps1 -version 14 # Return create a 2-web node12 vs node14 with docker compose Set-Location .. docker-compose up --build --detach # Check the server is exist Write-Host "==========================================================" Write-Host "=================Status Container Below===================" Write-Host "==========================================================" $connection = New-Object System.Net.Sockets.TcpClient("127.0.0.1", "3000") if ($connection.Connected) { Start-Sleep 2 $result=curl -L 127.0.0.1:3000 Write-Host "Server is running in port 3000, and return this message $result" } else { Write-Host "Server is not running in port 3000" } $connection = New-Object System.Net.Sockets.TcpClient("127.0.0.1", "3001") if ($connection.Connected) { Start-Sleep 2 $result=curl -L 127.0.0.1:3001 Write-Host "Server is running in port 3001, and return this message $result" } else { Write-Host "Server is not running in port 3001" } ``` ## Conclusion ## Reference [Docker-Doc](https://docs.docker.com/) [Different btw CMD vs Entrypoint vs RUN ](https://techmaster.vn/posts/36513/su-khac-biet-giua-run-cmd-va-entrypoint-trong-dockerfile)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully