[docker] docker-compose web-service test

docker-compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.

 

참고페이지: https://docs.docker.com/compose/https://docs.docker.com/compose/overview/

docker-compose 란?  간단히 설명하면 YAML 방식 설정파일을 이용하여 여러 컨테이너를 설치? 하고 운영 하는툴로 보시면 됩니다.

 

Github

test@docker-test:~$ git clone https://github.com/visualwork/Docker-test.git

/Docker-test/test02 에 있습니다. 🙂

 

docker-compose 설치

test@ubuntu1604:~$ sudo curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
test@ubuntu1604:~$ sudo chmod +x /usr/local/bin/docker-compose
test@tomomo:~$ docker-compose --version
docker-compose version 1.11.2, build dfed245
test@tomomo:~$

 

 

docker-compose 를 이용하여 web-service 를 테스트 합니다.

(Nginx + php71 + mysql57)

Docker-compose 디렉토리 구조

sanjuk@ubuntu1604:~/web-service$ tree
.
├── docker-compose.yml
├── nginx
│   └── conf
│       └── default.conf
└── php
    ├── conf
    │   └── php.ini
    └── Dockerfile

4 directories, 4 files
sanjuk@ubuntu1604:~/web-service$

 

docker-compose.yml 파일 

위치: /home/sanjuk/web-service

version: '2'

services:
    mysql:
        image: mysql:5.7
        ports:
            - 3306:3306
        volumes:
            - ./mysql:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_USER: docker
            MYSQL_PASSWORD: docker
            MYSQL_DATABASE: docker

    nginx:
        image: nginx:1.10.2
        ports:
            - 80:80
        restart: always
        volumes:
            - ./nginx/conf:/etc/nginx/conf.d
            - ~/Projects:/code
        links:
            - php
        depends_on:
            - php

    php:
        build: php
        expose:
            - 9000
        restart: always
        volumes:
            - ./php/conf/php.ini:/usr/local/etc/php/conf.d/custom.ini
            - ~/Projects:/code
        links:
            - mysql

 

version : compose file format 참고페이지 https://docs.docker.com/compose/compose-file/#reference-and-guidelines

services: 컨테이너 별로 만들어 주면 됩니다. ex) nginx , apache , mariadb , mysql 등

mysql 필드 설명

image: mysql:5.7 version 을 사용합니다.

ports: 3306:3306  / host의 3306 포트와 컨테이너의 3306 포트를 연결 합니다.

volumes: /mysql:/var/lib/mysql 내부 디렉토리 와 컨테이너 디렉토리를 연결 합니다.

restart: always  리부팅시에도 자동으로 실행을 합니다.

environment: 환경변수 설정?
MYSQL_ROOT_PASSWORD: root  /  mysql root 패스워드
MYSQL_USER: docker                   / mysql user 생성
MYSQL_PASSWORD: docker        / user password 생성
MYSQL_DATABASE: docker          / user database 생성

nginx 필드 설명

links: – php / php 컨테이너와 연결을 합니다.

depends_on: 서비스와 시작순서간의 종속성 설정?

The depends_on option can be used in place of links to indicate dependencies between services and startup order

참고페이지: https://docs.docker.com/compose/compose-file/compose-versioning/#version-2

 

php 필드 설명

build: php  / Dockerfile 을 통하여 build 시 build 라는 필드를 입력합니다.

expose: 9000 : 외부로 연결하지 않고 다른 컨테이너에서만 액세스 할수 있습니다.

Expose ports without publishing them to the host machine – they’ll only be accessible to linked services. Only the internal port can be specified.

참고페이지: https://docs.docker.com/compose/compose-file/compose-file-v2/

links: mysql 컨테이너와 연결

Link to containers in another service. Either specify both the service name and a link alias (“SERVICE:ALIAS”), or just the service name.

 

nginx default.conf 파일 생성

위치: /home/sanjuk/web-service/nginx/conf

server {
    listen       80 default_server;
    server_name  localhost _;
    index        index.php index.html index.htm;
    root         /code;

    location / {
        try_files   $uri $uri/ /index.php?$query_string;
        autoindex on;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

 

php/Dockerfile 생성

위치 : /home/sanjuk/web-service/php

FROM php:7.1-fpm

RUN apt-get update

# Some basic extensions
RUN docker-php-ext-install -j$(nproc) json mbstring opcache pdo pdo_mysql mysqli

# Curl
RUN apt-get install -y libcurl4-openssl-dev
RUN docker-php-ext-install -j$(nproc) curl

# GD
RUN apt-get install -y libpng-dev libjpeg-dev
RUN docker-php-ext-install -j$(nproc) gd

# Intl
RUN apt-get install -y libicu-dev
RUN docker-php-ext-install -j$(nproc) intl

 

php.ini 생성

위치 /home/sanjuk/web-service/php/conf

display_errors = On
display_startup_errors = On
default_charset = "UTF-8"
html_errors = On
date.timezone = Asia/Seoul

 

docker-compose 실행

실행위치

sanjuk@ubuntu1604:~/web-service$ pwd
/home/sanjuk/web-service
sanjuk@ubuntu1604:~/web-service$ ls
docker-compose.yml  nginx  php
sanjuk@ubuntu1604:~/web-service$

 

실행

sanjuk@ubuntu1604:~/web-service$ docker-compose up -d --build

(web-service 디렉토리에서 실행을 하며 docker-compose.yml 파일이 있어야 합니다.)

 

컨테이너 동작 확인

sanjuk@ubuntu1604:~/web-service$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                         NAMES
b94d040597dd        nginx:1.10.2        "nginx -g 'daemon of…"   About a minute ago   Up 38 seconds       0.0.0.0:80->80/tcp, 443/tcp   webservice_nginx_1
2738120d80de        webservice_php      "docker-php-entrypoi…"   About a minute ago   Up 39 seconds       9000/tcp                      webservice_php_1
efeb60c666f9        mysql:5.7           "docker-entrypoint.s…"   About a minute ago   Up 39 seconds       0.0.0.0:3306->3306/tcp        webservice_mysql_1
sanjuk@ubuntu1604:~/web-service$

 

info.php 파일생성

sanjuk@ubuntu1604:~$ sudo chown sanjuk:sanjuk Projects/
sanjuk@ubuntu1604:~$ sudo vi Projects/info.php
<?php phpinfo(); ?>

 

webbrowser확인

 

 

댓글 남기기