Docker(4)

Docker multi Container Application 구축

  • application과 database가 결합된 Container 결합 환경 구성
  • Docker
docker network create myapp-net
docker volume create mydb_data
docker run -itd \
--name=mysql_app \
-v mydb_data:/var/lib/mysql \
--restart=always \
-p 3306:3306 \
--net=myapp-net \
-e MYSQL_ROOT_PASSWORD=password# \
-e MYSQL_DATABASE=wpdb \
-e MYSQL_USER=wpuser \
-e MYSQL_PASSWORD=wppassword \
mysql:8.0



docker volume create myweb_data
docker run -itd \
--name=wordpress_app \
-v myweb_data:/var/www/html \
-v ${PWD}/myweb-log:/var/log \
--restart=always \
-p 8888:80 \
--net=myapp-net \
-e WORDPRESS_DB_HOST=mysql_app:3306 \
-e WORDPRESS_DB_NAME=wpdb \
-e WORDPRESS_DB_USER=wpuser \
-e WORDPRESS_DB_PASSWORD=wppassword \
--link mysql_app:mysql \
wordpress

docker ps

docker-compose 이용한 Container Application 환경구축

sudo curl -L https://github.com/docker/compose/release/download/1.27.4/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
sudo chown jeff /usr/local/bin/docker-compose

docker compose -v
docker compose version

mkdir my_wp $$ cd $_

nano docker-compose.yml
version: "3.9"
services:
  mydb:
    image: mysql:8.0
    container_name: mysql_app
    volumes:
      - mydb_data:/var/lib/mysql
    restart: always
    networks:
      - backend-net
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  myweb:
    depends_on:
      - mydb
    image: wordpress:latest
    container_name: wordpress_app
    networks:
      - frontend-net
      - backend-net
    ports:
      - "8888:80"
    volumes:
      - myweb_data:/var/www/html
      - ${PWD}/myweb-log:/var/log
    restart: always
    environment:
      WORDPRESS_DB_HOST: mydb:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
networks:
  frontend-net: {}
  backend-net: {}
volumes:
  mydb_data: {}
  myweb_data: {}
  
  
docker-compose up
// -d를 넣으면 background에서 수행
docker-compose ps
docker-compose down
// 만들어진 컨테이너 중지, 삭제, 네트워크 할당 해제
  • http://www.yamllint.com
  • https://codebeautify.org
  • network 를 명시하지 않으면 default로 폴더명_default 로 네트워크를 자동으로 설정함(docker0를 사용하지 않음)

Dockerfiles

  • Container 가 동작하기 위한 구성정보를 프로비저닝한 파일
  • Docker build 를 통해 DockerFile을 image 생성
  • 사용자의 개입이 전혀 없이 자동으로 실행될 수 있게 작성되어야 함(매크로와 같이)

mkdir appimage && cd $_
nano Dockerfile_nginx

----
FROM ubuntu:18.04

RUN apt-get update && apt-get install -y -q nginx

COPY index.html /usr/share/nginx/html/

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
----

docker build -f Dockerfile_nginx -t webapp:1.0 .
docker images | grep webapp

docker image history webapp:1.0
docker run -it -d --name webapp_test1 -p 8009:80 webapp:1.0

docker login
docker tag webapp:1.0 cys779988/webapp:1.0
docker push cys779988/webapp:1.0

docker pull cys779988/webapp:1.0
mkdir appimage2 && cd $_
nano Dockerfile_nginx2

----
FROM ubuntu:18.04

RUN apt-get update && apt-get install -y nginx \
curl \
nano \
vim

RUN echo 'Docker Container Application.' > /usr/share/nginx/html/index.html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
----

docker build -f Dockerfile_nginx2 -t webapp:2.0 .
docker images | grep webapp

Shell Script로 Dockerfile 작성

mkdir appimage3 && cd $_
nano Dockerfile

----
FROM ubuntu:18.04

RUN apt-get update && \
apt-get -y install apache2

RUN echo 'Docker Container Application.' > /var/www/html/index.html
RUN mkdir /webapp
RUN echo '. /etc/apache2/envvars' > /webapp/run_http.sh && \
    echo 'mkdir -p /var/run/apache2' >> /webapp/run_http.sh && \
    echo 'mkdir -p /var/lock/apache2' >> /webapp/run_http.sh && \
    echo '/usr/sbin/apache2 -D FOREGROUND' >> /webapp/run_http.sh && \
    chmod 744 /webapp/run_http.sh
    
EXPOSE 80
CMD /webapp/run_http.sh
----

docker build -t webapp:3.0 .
docker images | grep webapp
docker tag webapp:3.0 cys779988/webapp:3.0
docker push cys779988/webapp:3.0

Ubuntu 기반의 Base image 생성

docker run -it --name myweb -p 8005:80 ubuntu:14.04 bash
apt-get update
apt-get install -y apache2
service apache2 start
cd /var/www/html

apt-get -y install php5
cd html
nano index.php

----
<?php phpinfo(); ?>
----

service apache2 restart
(ctrl) + (p) + (q)

docker cp docker-phpserver/index.php2 myweb:/var/www/html/index.php
// php파일 myweb 컨테이너 안으로 복사
service apache2 restart

docker ps | grep ubuntu
docker commit myweb cys779988/ub_php_apache2:1.0
docker login
docker push cys779988/ub_php_apache2:1.0

//2번서버
docker pull cys779988/ub_php_apache2:1.0
docker run -it -d -p 8007:80 cys779988/ub_php_apache2:1.0
//컨테이너는 만들어지지만 apache2를 실행하는 명령어를 직접 실행해줘야 하기때문에 docker run 명령으로만으로는 apache2를 실행할 수 없음

//Dockerfile로 컨테이너를 만들고 apache2를 자동으로 실행시키는 코드 작성
mkdir devteam && cd $_
nano Dockerfile

----

FROM cys779988/ub_php_apache2:1.0
MAINTAINER cys779988 <cys779988@dshub.cloud>
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
----

docker build -t phpserver:2.0 .
docker images
docker run -it -d -p 8006:80 --name dev-php phpserver:2.0

mkdir phpweb && cd $_
cp ../docker-phpserver/index.php2 ./
nano Dockerfile

----
FROM ubuntu:14.04
MAINTAINER "cys779988 <cys779988@dshub.cloud>"
LABEL title "Iac, PHP application"
RUN apt-get update && apt-get -y install apache2 \
                                         php5 \
                                         git \
                                         curl \
                                         ssh \
                                         wget
ENV APACHE2_RUN_USER www-data
ENV APACHE2_RUN_GROUP www-data
ENV APACHE2_LOG_DIR /var/log/apache2
ENV APACHR2_PID_FILE /var/run/apache2/apache2.pid

RUN echo 'Hello, Docker Application.' > /var/www/html/index.html
COPY index.php2 /var/www/html/index.php
EXPOSE 90
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
----


git clone https://github.com/brayanlee/webapp.git
cd webapp/
ls
nano Dockerfile_web

----

FROM ubuntu:14.04
MAINTAINER "cys779988 <cys779988@dshub.cloud>"
LABEL "purpose"="webserver practice"
RUN apt-get update && apt-get -y install apache2 \
                                         nano \
                                         curl
ADD webapp.tar.gz /var/www/html
WORKDIR /var/www/html
EXPOSE 80
CMD /usr/sbin/apachectl -D FOREGROUND
----

docker build -f Dockerfile_web -t myhttp:2.0 .
docker run -d -p 8181:80 --name myapacheserver2 myhttp:2.0
docker exec -it myapacheserver2 bash

Nodejs script runserver요청

mkdir devteam_js && cd $_
nano runapp.js

----
----

nano Dockerfile

----

FROM node:15.3.0-alpine3.10
RUN apk add --no-cache tini curl
WORKDIR /app
COPY runapp.js .
EXPOSE 6060
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "runapp.js"]
----

docker build -t nodejs:1.0 .
docker images | grep nodejs
docker run -itd -p 6060:6060 --name=nodejs -h node-run nodejs:1.0
curl localhost:6060

Go 언어를 활용한 서비스 환경 구성

  • GO + scratch, 다단계빌드 활용한 웹 서비스 개발 환경 구성
  • 두 가지 언어를 연동한 인프라 환경 개발 구성
mkdir devteam_go && cd $_
nano goapp.go
nano Dockerfile

----

FROM golang:1.15-alpine3.12 AS gobuilder-stage

MAINTAINER cys779988 <cys779988@dshub.cloud>
LABEL "purpose"="Service Deployment using Multi-stage builds."

WORKDIR /usr/src/goapp
COPY goapp.go .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /usr/local/bin/gostart

FROM scratch AS runtime-stage
COPY --from=gobuilder-stage /usr/local/bin/gostart /usr/local/bin/gostart
CMD ["/usr/local/bin/gostart"]
----

docker build -t goapp:1.0 .

docker run --name goapp-deploy \
-p 9090:9090 -d \
-h goapp-container \
goapp:1.0

curl localhost:9090

docker ps
docker inspect goapp-deploy
docker stop goapp-deploy
docker rm goapp-deploy

docker image tag goapp:1.0 cys779988/goapp:1.0
docker push cys779988/goapp:1.0

Private Registry 구축

  1. registry image download
// 1번, 2번 서버에 적용

docker pull registry
docker pull hyper/docker-registry-web
docker info


// Insecure Registries 옵션 수정

sudo nano /etc/init.d/docker

----
DOCKER_OPTS=--insecure-registry 192.168.56.111:5000
----

sudo nano /etc/docker/daemon.json

----
{ "insecure-registries": ["192.168.56.111:5000"] }
----

sudo service docker restart
docker version

docker run -d \
-v /home/jeff/registry_data:/var/lib/registry \
-p 5000:5000 \
--restart=always \
--name=local-registry \
registry

docker ps | grep registry

sudo netstat -nlp | grep 5000

curl -X GET http://192.168.56.111:5000/v2/_catalog

curl -X GET http://192.168.56.111:5000/v2/myhttp/tags/list


docker image tag webapp:1.0 192.168.56.111:5000/webapp:1.0
docker image tag phpserver:1.0 192.168.56.111:5000/phpserver:1.0
docker image tag goapp:1.0 192.168.56.111:5000/goapp:1.0
// image tag 설정
docker images

docker push 192.168.56.111:5000/goapp:1.0
docker push 192.168.56.111:5000/phpserver:1.0
docker push 192.168.56.111:5000/webapp:1.0
// private registry에 upload

curl -X GET http://192.168.56.111:5000/v2/_catalog

{"repositories":["goapp","phpserver","webapp"]}
// push 한 이미지가 확인됨

//2번서버
docker pull 192.168.56.111:5000/webapp:1.0
docker image tag 192.168.56.111:5000/webapp:1.0 dev_http:1.1
docker run -d -p 8100:80 --name myweb-server dev_http:1.1


docker run -it -d -p 9090:8000 --name registry-web \
--link local-registry \
-e REGISTRY_URL=http://192.168.56.111:5000/v2 \
-e REGISTRY_NAME=192.168.56.111:5000 \
--restart=always \
hyper/docker-registry-web

// 192.168.56.111:9090 접속시 registry container WEB UI 제공

Categories:

Updated:

Comments