Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Docker] Comand Docker hay quên!!

Posted on July 31, 2021March 21, 2025 By nim No Comments on [Docker] Comand Docker hay quên!!

Contents

Toggle
  • 1) Docker
    • 1.1) Docker vs VM
    • 1.2) Optimize the file size of the docker image based on dockerfile
  • 2) Docker-compose
  • 3) Docker images
  • 4) Build image, Write Dockerfile

1) Docker

Show log 10 dòng cuối:

docker logs -f --tail 10 container_name

Docker build

docker build -t ps:version1 --force-rm -f Dockerfile .
kubectl -n argocd port-forward deployment.apps/argocd-server 8080:8080

với các build mà sử dụng git private:

GITHUB_TOKEN=abcsldfnjsdbfasd

docker build -t ps:version1 --force-rm --build-arg GITHUB_TOKEN=${GITHUB_TOKEN} -f Dockerfile.ci .

trong Dockerfile thì bạn sẽ có dòng RUN như sau:

Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1

Nếu bạn bị lỗi trên thì thêm line bên dưới vào dockerfile

RUN git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/"

Run docker và excute ngay lập tức

docker run -it --entrypoint /bin/bash mrnim94/terraform-kubectl:0.0.5

Copying Files from Docker Container to Host

docker cp container_name:/path/in/container /path/on/host

##example:

# Copy a file
docker cp activemq-artemis:/var/lib/artemis/data/myfile.txt ./

# Copy a directory
docker cp activemq-artemis:/var/lib/artemis/etc/ ./artemis-config/

1.1) Docker vs VM

Docker and VM (Virtual Machine) are two different technologies used for virtualization.

A virtual machine is a software-based emulation of a physical machine that allows multiple operating systems to run on a single physical machine. Each VM includes a virtual CPU, memory, storage, and network interface, and can run any operating system that can run on a physical machine.

Docker, on the other hand, is a software platform that allows developers to create, deploy, and run applications in a containerized environment. Docker containers are lightweight and share the same operating system kernel, libraries, and other system resources, making them more efficient and portable than traditional virtual machines.

In summary, virtual machines emulate an entire operating system, while Docker containers only include the application and its dependencies, making them more lightweight and efficient. Both technologies have their own strengths and use cases, and the choice between them depends on the specific requirements of the application and its deployment environment.

More simple:

Virtual machines (VMs) are like fully functioning computers that run inside your computer. They simulate a complete operating system with its own hardware and resources, allowing you to run multiple operating systems on a single physical machine.

Docker is a tool that allows developers to create “containers” for their applications. Containers are like little packages that contain all the necessary code and dependencies for the application to run. These containers are much smaller and more efficient than virtual machines, as they share resources with the host operating system.

So, the main difference is that VMs are like full-fledged computers inside your computer, while Docker containers are like little packages that contain only the necessary code to run the application.

1.2) Optimize the file size of the docker image based on dockerfile

Optimizing the file size of a Docker image based on a Dockerfile can be achieved by following these best practices:

  1. Use a smaller base image: Start with a smaller base image like Alpine or BusyBox instead of a full-blown OS like Ubuntu or CentOS.
  2. Combine commands: Use multi-line commands to reduce the number of layers created in the image. For example, instead of creating a separate layer for each RUN command, you can combine multiple commands into a single RUN command.
  3. Clean up after each command: Remove any unnecessary files or temporary files after each command. This can be done using the “&& rm -rf /var/lib/apt/lists/*” command in an apt-get update command.
  4. Use .dockerignore file: Create a .dockerignore file to exclude unnecessary files from the Docker build context. This can significantly reduce the size of the final image.
  5. Minimize package installations: Only install packages that are required by your application. Avoid installing unnecessary packages or dependencies that are not needed.
  6. Use a multi-stage build: Use a multi-stage build to separate the build environment from the runtime environment. This can significantly reduce the size of the final image.

By following these best practices, you can create smaller and more efficient Docker images, which can reduce the overall build and deployment time and save storage space.

Explain more:

Certainly! Let me explain Practice 2 in more detail.

Practice 2 is about minimizing the number of layers in your Docker image. Each instruction in a Dockerfile creates a new layer in the final image, which can increase the size of the image. By combining multiple instructions into a single RUN command, you can reduce the number of layers and the overall size of the image.

For example, consider the following Dockerfile:

FROM ubuntu:latest

RUN apt-get update
RUN apt-get install -y \
    apache2 \
    php7.2 \
    libapache2-mod-php7.2 \
    php7.2-mysql \
    && apt-get clean

COPY . /var/www/html/

CMD ["apache2ctl", "-D", "FOREGROUND"]

In this Dockerfile, we have used separate RUN commands to update the package list, install the required packages, and clean up the apt cache. Each RUN command creates a new layer in the final image.

To reduce the number of layers, we can combine these instructions into a single RUN command like this:

FROM ubuntu:latest

RUN apt-get update && \
    apt-get install -y \
    apache2 \
    php7.2 \
    libapache2-mod-php7.2 \
    php7.2-mysql && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

COPY . /var/www/html/

CMD ["apache2ctl", "-D", "FOREGROUND"]

In this revised Dockerfile, we have combined the three RUN commands into a single RUN command. This reduces the number of layers and results in a smaller final image.

Overall, Practice 2 is about finding ways to minimize the number of layers in your Docker image by combining commands wherever possible. This can help reduce the size of the final image and make it more efficient.

2) Docker-compose

Run docker-compose chỉ định file:

docker-compose -f docker-compose.test.yml up

3) Docker images

docker save --output myimage.tar myimage_id
>>>>Lưu image ra đĩa
docker load -i myimage.tar
>>>>Nạp Image trên đĩa vào Docker
docker tag image_id imagename:version
>>>>Đổi tên Image

4) Build image, Write Dockerfile

https://www.youtube.com/watch?v=r_UBWjMUd-0

Docker, Kubernetes & Container

Post navigation

Previous Post: [windows]Shutdown máy tính client bằng Domain controller
Next Post: [Node exporter] Install node_exporter on MacOS

More Related Articles

[Lens/Kubernetes] Install Lens to manage the workloads On Kubernetes. Kubernetes & Container
[Harbor/Azure] Log in to Harbor with Azure. Docker
[K8s] Hướng dẫn sử dụng kubectl trên mấy local kết nối đến k8s master Kubernetes
[issue/alpine] docker 20.10.2 -> golang:1-alpine3.14 error: make: go: Operation not permitted Docker
[Istio-Multi Cluster] Tutorial is very detailed to install Primary-Remote on different networks Isito-EnvoyFilter
[Kubernetes] Lesson5: k8s easy – Multi-container, Probes Kubernetes & Container

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Azure] The subscription is not registered to use namespace ‘Microsoft.ContainerService’ May 8, 2025
  • [Azure] Insufficient regional vcpu quota left May 8, 2025
  • [WordPress] How to add a Dynamic watermark on WordPress. May 6, 2025
  • [vnet/Azure] VNet provisioning via Terraform. April 28, 2025
  • [tracetcp] How to perform a tracert command using a specific port. April 3, 2025

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.