docker学习笔记(六)—— docker容器实现web服务

   日期:2020-05-25     浏览:135    评论:0    
核心提示:一、docker容器实现apache服务1.通过 Dockerfile实现创建文件夹用来存放文件mkdir -p ~/apache/www ~/apache/logs ~/apache/confwww将映射为apache容器配置的应用程序目录,logs为apache日志文件,conf中的配置文件为apache容器的配置文件。创建Dockerfile文件,通过vim打开该文件FROM debian:jessie # add our user and group first to make s运维

一、docker容器实现apache服务
1.通过 Dockerfile实现
创建文件夹用来存放文件

mkdir -p ~/apache/www ~/apache/logs   ~/apache/conf

www将映射为apache容器配置的应用程序目录,logs为apache日志文件,conf中的配置文件为apache容器的配置文件。
创建Dockerfile文件,通过vim打开该文件

FROM debian:jessie # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added #RUN groupadd -r www-data && useradd -r --create-home -g www-data www-data ENV HTTPD_PREFIX /usr/local/apache2 ENV PATH $PATH:$HTTPD_PREFIX/bin RUN mkdir -p "$HTTPD_PREFIX" \ && chown www-data:www-data "$HTTPD_PREFIX" WORKDIR $HTTPD_PREFIX # install httpd runtime dependencies # https://httpd.apache.org/docs/2.4/install.html#requirements RUN apt-get update \ && apt-get install -y --no-install-recommends \ libapr1 \ libaprutil1 \ libaprutil1-ldap \ libapr1-dev \ libaprutil1-dev \ libpcre++0 \ libssl1.0.0 \ && rm -r /var/lib/apt/lists/* ENV HTTPD_VERSION 2.4.20 ENV HTTPD_BZ2_URL https://www.apache.org/dist/httpd/httpd-$HTTPD_VERSION.tar.bz2 RUN buildDeps=' \ ca-certificates \
curl \ bzip2 \ gcc \ libpcre++-dev \ libssl-dev \ make \ ' \set -x \ && apt-get update \ && apt-get install -y --no-install-recommends $buildDeps \ && rm -r /var/lib/apt/lists/* \ \&& curl -fSL "$HTTPD_BZ2_URL" -o httpd.tar.bz2 \ && curl -fSL "$HTTPD_BZ2_URL.asc" -o httpd.tar.bz2.asc \ # see https://httpd.apache.org/download.cgi#verify && export GNUPGHOME="$(mktemp -d)" \ && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys A93D62ECC3C8EA12DB220EC934EA76E6791485A8 \ && gpg --batch --verify httpd.tar.bz2.asc httpd.tar.bz2 \ && rm -r "$GNUPGHOME" httpd.tar.bz2.asc \ \&& mkdir -p src \ && tar -xvf httpd.tar.bz2 -C src --strip-components=1 \ && rm httpd.tar.bz2 \ && cd src \ \&& ./configure \ --prefix="$HTTPD_PREFIX" \ --enable-mods-shared=reallyall \ && make -j"$(nproc)" \ && make install \ \&& cd .. \ && rm -r src \ \&& sed -ri \ -e 's!^(\s*CustomLog)\s+\S+!\1 /proc/self/fd/1!g' \ -e 's!^(\s*ErrorLog)\s+\S+!\1 /proc/self/fd/2!g' \ "$HTTPD_PREFIX/conf/httpd.conf" \ \&& apt-get purge -y --auto-remove $buildDeps COPY httpd-foreground /usr/local/bin/ EXPOSE 80 CMD ["httpd-foreground"]

其中 COPY httpd-foreground /usr/local/bin/是将当前目录下的httpd-foreground拷贝到镜像里,作为apache启动脚本。
创建一个启动脚本,并给与可执行权限
httpd-foreground

#!/bin/bash set -e # Apache gets grumpy about PID files pre- existing rm -f /usr/local/apache2/logs/httpd.pid exec httpd -DFOREGROUND

通过Dockerfile创建一个镜像

docker build -t 容器名(可以自己定义)

创建完成后通过命令查看是否创建成功

docker images 容器名

2.docker pull httpd
通过命令查找docker仓库里面的镜像

docker search httpd

找到后对对应的镜像进行一个拉去

docker pull httpd

下载完成后查看是否安装成功

dockers images

通过命令对容器进行一个基本的绑定

docker run -p 80:80 -v $PWD/www/:/usr/local/apache2/htdocs/ -v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf -v $PWD/logs/:/usr/local/apache2/logs/ -d httpd

-p 80:80 :将容器的 80 端口映射到主机的 80 端口
-v $PWD/www/:/usr/local/apache2/htdocs/ :将主机中当前目录下的 www 目录挂载到容器的 /usr/local/apache2/htdocs/
-v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf :将主机中当前目录下的 conf/httpd.conf 文件挂载到容器的/usr/local/apache2/conf/httpd.conf
-v $PWD/logs/:/usr/local/apache2/logs/ :将主机中当前目录下的 logs 目录挂载到容器的 /usr/local/apache2/logs
通过命令查看docker容器的运行情况
docker ps
二、dockers容器实现ningx服务
1.通过Dockerfile实现(创建方式与apache类似,只是配置文件又区别)

FROM debian:jessie MAINTAINER NGINX Docker Maintainers "docker-maint@nginx.com" ENV NGINX_VERSION 1.10.1-1~jessie RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 \ && echo "deb http://nginx.org/packages/debian/ jessie nginx" >> /etc/apt/sources.list \&& apt-get update \ && apt-get install --no-install-recommends --no-install-suggests -y \ ca-certificates \ nginx=${NGINX_VERSION} \ nginx-module-xslt \ nginx-module-geoip \ nginx-module-image-filter \ nginx-module-perl \ nginx-module-njs \ gettext-base \ && rm -rf /var/lib/apt/lists/* # forward request and error logs to docker log collector RUN ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log EXPOSE 80 443 CMD ["nginx", "-g", "daemon off;"]

2.docker pull nginx(配置与apache类似)
三、docker容器实现MySQL服务
1.通过Dockerfile实现(创建方式与apache类似,只是配置文件又区别)

FROM debian:jessie
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added RUN groupadd -r mysql && useradd -r -g mysql mysql # add gosu for easy step-down from root ENV GOSU_VERSION 1.7 RUN set -x \ && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \ && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu- $(dpkg --print-architecture)" \ && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ && export GNUPGHOME="$(mktemp -d)" \ && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \ && chmod +x /usr/local/bin/gosu \ && gosu nobody true \ && apt-get purge -y --auto-remove ca-certificates wget RUN mkdir /docker-entrypoint-initdb.d # FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db: # File::Basename # File::Copy # Sys::Hostname # Data::Dumper
RUN apt-get update && apt-get install -y perl pwgen --no-install-recommends && rm -rf /var/lib/apt/lists/* # gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5 ENV MYSQL_MAJOR 5.6 ENV MYSQL_VERSION 5.6.31-1debian8 RUN echo "deb http://repo.mysql.com/apt/debian/ jessie mysql-${MYSQL_MAJOR}" > /etc/apt/sources.list.d/mysql.list # the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql) # also, we set debconf keys to make APT a little quieter RUN { \ echo mysql-community-server mysql-community-server/data-dir select ''; \ echo mysql-community-server mysql-community-server/root-pass password ''; \ echo mysql-community-server mysql-community-server/re-root-pass password ''; \ echo mysql-community-server mysql-community-server/remove-test-db select false; \ } | debconf-set-selections \ && apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}" && rm -rf /var/lib/apt/lists/* \ && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \ && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \ # ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime && chmod 777 /var/run/mysqld # comment out a few problematic configuration values
# don't reverse lookup hostnames, they are usually another container RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf \ && echo 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/mysql/my.cnf > /tmp/my.cnf \ && mv /tmp/my.cnf /etc/mysql/my.cnf VOLUME /var/lib/mysql COPY docker-entrypoint.sh /usr/local/bin/ RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 3306 CMD ["mysqld"]

2.docker pull mysql

docker run -p 3306:3306 --name mymysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6 21cb89213c93d805c5bacf1028a0da7b5c5852761ba81327e6b99b b3ea89930e

-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口
-v $PWD/conf/my.cnf:/etc/mysql/my.cnf:将主机当前目录下的 conf/my.cnf 挂 载到容器的/etc/mysql/my.cnf
-v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的/logs
-v $PWD/data:/mysql_data:将主机当前目录下的 data 目录挂载到容器的/mysql_data
-e MYSQL_ROOT_PASSWORD=123456:初始化 root

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服