jwilder/nginx-proxy image 이용 Nginx-web 컨테이너 추가
docker-compose Nginx-proxy 를 이용하여 Multi-Wordpress 컨테이너를 구성 하였습니다.
이번에는 WordPress 가 아닌 Nginx web-site를 구성 합니다.
Github
test@docker-test:~$ git clone https://github.com/visualwork/Docker-test.git
/Docker-test/test06 에 있습니다. 🙂
docker ps 확인시 nginx-proxy 컨테이너가 80 port 연결을 받아 다른 컨테이너의 Virtualhost로 접속하게 하였습니다.
test@docker-test:~/web-service$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5d75a1fe1bc0 wordpress:latest "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 80/tcp wp_blog2 c5a3310e88d8 mariadb "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 3306/tcp wp_blog_db2 9e7e6c795f52 wordpress:latest "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 80/tcp wp_blog 242f3c32f658 mariadb "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 3306/tcp wp_blog_db fc8761f89097 jwilder/nginx-proxy "/app/docker-entrypo…" 11 minutes ago Up 11 minutes 0.0.0.0:80->80/tcp nginx-proxy test@docker-test:~/web-service$
ex) blog1 docker-compose.yml file
wordpress: image: wordpress:latest expose: - 80 restart: always volumes: - ./web-data:/var/www/html environment: VIRTUAL_HOST: blog1.test.com
VIRTUAL_HOST : blog1.test.com 으로 설정 되어 있습니다. environment: 의 VIRTUAL_HOST 부분을 참조 하여 nginx-proxy 에서 자동으로 등록하게 되어 있습니다.
nginx 를 사용할 디렉토리를 생성후 임시docker-compose.yml 을 만들어 줍니다.
컨테이너 구동만을 테스트 하기 위하여 임시로 만드는 docker-compose 파일입니다.
test@docker-test:~/web-service$ mkdir www test@docker-test:~/web-service$ cd www test@docker-test:~/web-service/www$ vi docker-compose.yml version: '2' services: nginx-www: image: nginx:latest restart: always environment: - VIRTUAL_HOST=www.test.com container_name: nginx-www networks: default: external: name: nginx-proxy
nginx-www 컨테이너를 구동합니다
test@docker-test:~/web-service/www$ docker-compose up -d --build Pulling nginx-www (nginx:latest)... latest: Pulling from library/nginx 8176e34d5d92: Already exists 5b19c1bdd74b: Pull complete 4e9f6296fa34: Pull complete Digest: sha256:4771d09578c7c6a65299e110b3ee1c0a2592f5ea2618d23e4ffe7a4cab1ce5de Status: Downloaded newer image for nginx:latest Creating nginx-www ... done test@docker-test:~/web-service/www$
nginx-www 컨테이너 확인
test@docker-test:~/web-service/www$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7fcc8b64bf87 nginx:latest "nginx -g 'daemon of…" 43 seconds ago Up 42 seconds 80/tcp nginx-www 5d75a1fe1bc0 wordpress:latest "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 80/tcp wp_blog2 c5a3310e88d8 mariadb "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 3306/tcp wp_blog_db2 9e7e6c795f52 wordpress:latest "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 80/tcp wp_blog 242f3c32f658 mariadb "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 3306/tcp wp_blog_db fc8761f89097 jwilder/nginx-proxy "/app/docker-entrypo…" 18 minutes ago Up 18 minutes 0.0.0.0:80->80/tcp nginx-proxy test@docker-test:~/web-service/www$
nginx-proxy 컨테이너에 접속 합니다.
www.test.com ip 정보를 확인 합니다.
test@docker-test:~/web-service/www$ docker exec -it fc8761f89097 /bin/bash root@fc8761f89097:/app# cat /etc/nginx/conf.d/default.conf ~중략 # www.test.com upstream www.test.com { ## Can be connect with "nginx-proxy" network # nginx-www server 172.18.0.7:80; } server { server_name www.test.com; listen 80 ; access_log /var/log/nginx/access.log vhost; location / { proxy_pass http://www.test.com; } } root@fc8761f89097:/app#
docker inspect 명령어를 이용하여 nginx-www 에 설정된 ip 정보를 확인 합니다.
test@docker-test:~/web-service/www$ docker inspect nginx-www |grep 172 "Gateway": "172.18.0.1", "IPAddress": "172.18.0.7", test@docker-test:~/web-service/www$
정상적으로 설정이 되어 있습니다. 🙂
docker-compose down 하여 컨테이너를 정지 합니다.
test@docker-test:~/web-service/www$ docker-compose down Stopping nginx-www ... done Removing nginx-www ... done Network nginx-proxy is external, skipping test@docker-test:~/web-service/www$
디렉토리 생성
test@docker-test:~/web-service/www$ mkdir www-data test@docker-test:~/web-service/www$ mkdir -p mariadb/db-data test@docker-test:~/web-service/www$ mkdir -p nginx/conf test@docker-test:~/web-service/www$ mkdir -p php/conf
docker-compose file 생성
test@docker-test:~/web-service/www$ vi docker-compose.yml version: '2' services: mysql: image: mariadb:10.3.1 volumes: - ./mariadb/db-data/:/var/lib/mysql - ./mariadb/my.cnf:/etc/mysql/mariadb.conf.d/custom.cnf restart: always environment: MYSQL_ROOT_PASSWORD: root MYSQL_USER: docker MYSQL_PASSWORD: docker MYSQL_DATABASE: docker container_name: mariadb-www nginx-www: image: nginx:latest restart: always volumes: - ./nginx/conf:/etc/nginx/conf.d - ./www-data:/www environment: - VIRTUAL_HOST=www.test.com container_name: nginx-www php: build: php expose: - 9000 restart: always volumes: - ./php/conf/php.ini:/usr/local/etc/php/conf.d/custom.ini - ./www-data:/www container_name: php-www networks: default: external: name: nginx-proxy
nginx/conf/default.conf 파일생성
test@docker-test:~/web-service/www$ cd nginx/conf/default.conf server { listen 80 default_server; server_name localhost _; index index.php index.html index.htm; root /www; 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 파일생성
test@docker-test:~/web-service/www$ vi php/Dockerfile 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/conf/php.ini 파일생성
test@docker-test:~/web-service/www$ vi php/conf/php.ini display_errors = On display_startup_errors = On default_charset = "UTF-8" html_errors = On date.timezone = Asia/Seoul
mariadb/my.cnf 파일생성
test@docker-test:~/web-service/www$ vi mariadb/my.cnf # MariaDB database server configuration file. # # You can copy this file to one of: # - "/etc/mysql/my.cnf" to set global options, # - "~/.my.cnf" to set user-specific options. # # One can use all long options that the program supports. # Run program with --help to get a list of available options and with # --print-defaults to see which it would actually understand and use. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html # This will be passed to all mysql clients # It has been reported that passwords should be enclosed with ticks/quotes # escpecially if they contain "#" chars... # Remember to edit /etc/mysql/debian.cnf when changing the socket location. [client] port = 3306 socket = /var/run/mysqld/mysqld.sock default-character-set = utf8mb4 # Here is entries for some specific programs # The following values assume you have at least 32M ram # This was formally known as [safe_mysqld]. Both versions are currently parsed. [mysqld_safe] socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] # # * Basic Settings # #user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc_messages_dir = /usr/share/mysql lc_messages = en_US skip-external-locking # # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. #bind-address = 127.0.0.1 # # * Fine Tuning # max_connections = 100 connect_timeout = 5 wait_timeout = 600 max_allowed_packet = 16M thread_cache_size = 128 sort_buffer_size = 4M bulk_insert_buffer_size = 16M tmp_table_size = 32M max_heap_table_size = 32M # # * MyISAM # # This replaces the startup script and checks MyISAM tables if needed # the first time they are touched. On error, make copy and try a repair. myisam_recover_options = BACKUP key_buffer_size = 128M #open-files-limit = 2000 table_open_cache = 400 myisam_sort_buffer_size = 512M concurrent_insert = 2 read_buffer_size = 2M read_rnd_buffer_size = 1M # # * Query Cache Configuration # # Cache only tiny result sets, so we can fit more in the query cache. query_cache_limit = 128K query_cache_size = 64M # for more write intensive setups, set to DEMAND or OFF #query_cache_type = DEMAND # # * Logging and Replication # # Both location gets rotated by the cronjob. # Be aware that this log type is a performance killer. # As of 5.1 you can enable the log at runtime! #general_log_file = /var/log/mysql/mysql.log #general_log = 1 # # Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf. # # we do want to know about network errors and such #log_warnings = 2 # # Enable the slow query log to see queries with especially long duration #slow_query_log[={0|1}] slow_query_log_file = /var/log/mysql/mariadb-slow.log long_query_time = 10 #log_slow_rate_limit = 1000 #log_slow_verbosity = query_plan #log-queries-not-using-indexes #log_slow_admin_statements # # The following can be used as easy to replay backup logs or for replication. # note: if you are setting up a replication slave, see README.Debian about # other settings you may need to change. #server-id = 1 #report_host = master1 #auto_increment_increment = 2 #auto_increment_offset = 1 #log_bin = /var/log/mysql/mariadb-bin #log_bin_index = /var/log/mysql/mariadb-bin.index # not fab for performance, but safer #sync_binlog = 1 expire_logs_days = 10 max_binlog_size = 100M # slaves #relay_log = /var/log/mysql/relay-bin #relay_log_index = /var/log/mysql/relay-bin.index #relay_log_info_file = /var/log/mysql/relay-bin.info #log_slave_updates #read_only # # If applications support it, this stricter sql_mode prevents some # mistakes like inserting invalid dates etc. #sql_mode = NO_ENGINE_SUBSTITUTION,TRADITIONAL # # * InnoDB # # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/. # Read the manual for more InnoDB related options. There are many! default_storage_engine = InnoDB # you can't just change log file size, requires special procedure #innodb_log_file_size = 50M innodb_buffer_pool_size = 256M innodb_log_buffer_size = 8M innodb_file_per_table = 1 innodb_open_files = 400 innodb_io_capacity = 400 innodb_flush_method = O_DIRECT # # * Security Features # # Read the manual, too, if you want chroot! # chroot = /var/lib/mysql/ # # For generating SSL certificates I recommend the OpenSSL GUI "tinyca". # # ssl-ca=/etc/mysql/cacert.pem # ssl-cert=/etc/mysql/server-cert.pem # ssl-key=/etc/mysql/server-key.pem character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci # # * Galera-related settings # [galera] # Mandatory settings #wsrep_on=ON #wsrep_provider= #wsrep_cluster_address= #binlog_format=row #default_storage_engine=InnoDB #innodb_autoinc_lock_mode=2 # # Allow server to accept connections on all interfaces. # #bind-address=0.0.0.0 # # Optional setting #wsrep_slave_threads=1 #innodb_flush_log_at_trx_commit=0 [mysqldump] quick quote-names max_allowed_packet = 16M [mysql] #no-auto-rehash # faster start of mysql but no tab completion [isamchk] key_buffer = 16M # # * IMPORTANT: Additional settings that can override those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # !includedir /etc/mysql/conf.d/
docker-compose 실행
test@docker-test:~/web-service/www$ docker-compose up -d --build
컨테이너 확인
test@docker-test:~/web-service/www$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 130f3eb196e3 www_php "docker-php-entrypoi…" 10 seconds ago Up 9 seconds 9000/tcp php-www 563f8bbccd33 mariadb:10.3.1 "docker-entrypoint.s…" 10 seconds ago Up 9 seconds 0.0.0.0:3306->3306/tcp mariadb-www 677fc32c5a4b nginx:latest "nginx -g 'daemon of…" 10 seconds ago Up 9 seconds 80/tcp nginx-www 5d75a1fe1bc0 wordpress:latest "docker-entrypoint.s…" About an hour ago Up About an hour 80/tcp wp_blog2 c5a3310e88d8 mariadb "docker-entrypoint.s…" About an hour ago Up About an hour 3306/tcp wp_blog_db2 9e7e6c795f52 wordpress:latest "docker-entrypoint.s…" About an hour ago Up About an hour 80/tcp wp_blog 242f3c32f658 mariadb "docker-entrypoint.s…" About an hour ago Up About an hour 3306/tcp wp_blog_db fc8761f89097 jwilder/nginx-proxy "/app/docker-entrypo…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp nginx-proxy test@docker-test:~/web-service/www$
test.php 파일생성
test@docker-test:~/web-service/www$ vi www-data/test.php <?php phpinfo(); ?>
web browser 확인
mariadb 테스트
테스트를 위하여 그누보드5 설치를 진행 했습니다.
주의 Host : localhost -> db컨테이너 이름으로 변경해 줘야 합니다.
(Docker 네트워크에 관해서는 차후 포스팅 하도록 하겠습니다.)
설치후 로그인
blog1.test.com / blog2.test.com / www.test.com
수고 하셨습니다. 🙂