Home [Development] Docker
Post
Cancel

[Development] Docker

The Benefits

  • With Docker, you can manage your infrastructure in the same ways you manage your applications.

  • 서버마다, 개발자들이 사용하는 PC 마다 설치와 설정을 다르게 해야하는 번거로움을 해결해줌

Screen Shot 2022-08-25 at 1 58 58 PM


The Features

  • Docker provides the ability to package and run an application in a loosely isolated environment called a container
    • Containers are great for continuous integration and continuous delivery (CI/CD) workflows
  • The isolation and security allows you to run many containers simultaneously on a given host.
  • Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host.

  • reference : https://docs.docker.com/get-started/overview/

Docker Architecture

  • Docker uses a client-server architecture

docker_architecture_pull_run_powerpoint_presentation_mockup_slide01

Screen Shot 2022-09-21 at 2 41 39 PM


Docker Container Orchestraction : Docker swarm vs Kubernetes

Docker Swarm

Architecture

Screen Shot 2022-09-21 at 2 34 52 PM

  • A group of either physical or virtual machines that are running the Docker application and that have been configured to join together in a cluster. The activities of the cluster are controlled by a swarm manager, and machines that have joined the cluster are referred to as nodes.
  • The key components of a Docker Swarm are Docker Nodes, Docker Services, and Docker Tasks.

Kubernetes

https://uzzing.github.io/posts/Kubernetes/


Docker Image & Container

Screen Shot 2022-08-25 at 2 09 05 PM

Screen Shot 2022-08-25 at 2 07 34 PM


CLI

[Reference] https://docs.docker.com/engine/reference/commandline/docker/

  • build / container / create / events / exec / image / images / inspect / kill / login / logout / network / ps / pull / push / rename / restart / rm / rmi / run / search / start / stop / swarm / system / tag / update / version / volume …

Example

  • docker –version

  • docker pull [IMAGE]
    • ex) docker pull nginx
  • docker images ls Screen Shot 2022-08-25 at 3 16 50 PM

  • docker run -d -p [PORT:hostmap] [IMAGE]
    • ex) docker run -d -p 8080:80 nginx:latest -> localhost:8080
    • ex) docker run -d -p 8080:80 -p 3000:80 nginx:latest -> localhost:8080, localhost:3000
    • ex) docker run –name websitename -d -p 8080:80 -p 3000:80 nginx:latest -> NAMES : websitename
    • ex) docker run –name website -v $(pwd):/usr/share/nginx/html:ro -d -p 8080:80 -p 3000:80 nginx
      • ro : read only
    • ex) docker run –name website-copy –volumes-from website -d -p 8081:80 -p nginx
    • ex) docker run –name website -p 8080:80 -d website:latest
  • docker ps (= docker container ls) Screen Shot 2022-08-25 at 3 17 29 PM

  • docker stop [CONTAINER ID] (= [NAMES])
    • ex) docker stop 662e3476d306
    • ex) docker stop vigorous_mestorf
  • docker rm $(docker ps -aq)
    • docker rm -f $(docker ps -aq)
    • docker rmi -f $(docker ps -aq)
  • docker exec -it website bash

Screen Shot 2022-08-25 at 3 14 54 PM


Build images

docker images docker bulid image docker-layers

ARG vs ENV

argvsenv

  • Write Dockerfile, .dockerignore -> build -> run

Dockerfile Screen Shot 2022-08-25 at 5 37 20 PM

  • DockerFile : 로컬환경 자원을 사용해 빌드한 파일을 인스턴스에 넘겨줌

  • DockerFile-with-build : 인스턴스 내부의 자원을 사용해 빌드 -> 시간이 비약적으로 증가함

  • OPTION
    • FROM : 생성할 이미지의 base image
    • COPY : 로컬의 파일을 이미지에 복사
    • EXPOSE : 노출할 포트 지정
    • ENTRYPOINT : 컨테이너가 시작될 때 수행할 명령어를 지정
  • example Screen Shot 2022-08-25 at 5 47 58 PM Screen Shot 2022-08-25 at 5 48 41 PM

    1
    2
    3
    4
    
    FROM openjdk:11-jdk-slim-buster
    COPY build/libs/login-service.jar app.jar
    EXPOSE 8080
    ENTRYPOINT ["java", "-jar", "/app.jar"]
    
    1
    2
    
    FROM nginx
    COPY nginx.conf /etc/nginx/nginx.conf
    
    1
    2
    
    FROM nginx:alpine
    ADD . /usr/share/nginx/html
    
    1
    2
    3
    4
    5
    6
    
    FROM node:latest
    WORKDIR /app
    ADD package*.json ./
    RUN npm install
    ADD . .     -> first . : all the files in the folder, second . : to /app folder
    CMD node index.js
    

.dockerignore

  • ignore files, folders
  • example
    1
    2
    3
    4
    5
    
    node_modules
    Dockerfile
    .git
    *.gulp.js 
    folder/**
    
docker build
Build an image from a Dockerfile
  • example
  • docker build –tag website:latest .
  • docker build -t user-service-api:latest .
docker run
Run a command in a new container
  • example
  • docker run –name user-api -d -p 3000:3000 user-service-api:latest

Volumes

Screen Shot 2022-08-25 at 5 32 01 PM


Versioning

docker tag
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  • docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
  • example
  • docker tag amigoscode-website:latest amigoscode-website:1
  • docker tag amigoscode-website:latest amigoscode-website:2

Example

  • docker pull nginx:alpine
  • docker pull node:alpine

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
events {}

http {
  upstream app {
    server 172.17.0.1:8080;
  }

  server {
    listen 80;

    location / {
      proxy_pass http://app;
    }
  }
}
  • docker build -t user-service-api:latest

  • docker build -t website:latest


Docker Registry

  • A Docker registry stores Docker images. Screen Shot 2022-08-30 at 4 49 15 PM Screen Shot 2022-08-29 at 11 10 42 AM Screen Shot 2022-08-29 at 11 11 38 AM

CI/CD : Docker + Jenkins

docker cicd


Nginx

1
a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.
  • HTTP Web Server
  • Reverse Proxy Server for load balancer
  • maximum performance and stability.
  • Nginx uses an asynchronous event-driven approach, rather than threads, to handle requests.
    • it provide predictable performance under high loads. (concurrency, high performance and low memory usage)
  • Nginx is easy to configure in order to serve static web content or to act as a proxy server

Nginx in Docker

1
2
3
4
5
NGINX is the native load balancer in Docker EE.

NGINX is the most widely deployed Ingress controller for Kubernetes.
Whether you choose Docker Swarm or Kubernetes, 
NGINX is the best choice for scaling container orchestration traffic.

[Reference]

https://subicura.com/2017/01/19/docker-guide-for-beginners-2.html

Docker and Kubernetes TutorialFull Course [2021] https://youtu.be/bhBSlnQcq2k
This post is licensed under CC BY 4.0 by the author.

[Development] Kubernetes

[Development] Elasticsearch

Comments powered by Disqus.