<返回更多

Docker 安装 Nginx、PHP、MySQL、Tomcat、Python、Redis、Apache

2019-12-30    
加入收藏

Docker 安装 Nginx

docker pull nginx 命令安装

查找 Docker Hub 上的 nginx 镜像

runoob@runoob:~/nginx$ docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 3260 [OK] 
jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 674 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 207 [OK]
million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), centos... 67 [OK]
maxexcloo/nginx-php Docker framework container with Nginx and ... 57 [OK]
...

这里我们拉取官方的镜像

$ docker pull nginx

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像。

runoob@runoob:~/nginx$ docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 555bbd91e13c 3 days ago 182.8 MB

以下命令使用 NGINX 默认的配置来启动一个 Nginx 容器实例:

$ docker run --name runoob-nginx-test -p 8081:80 -d nginx

执行以上命令会生成一串字符串,类似 6dd4380ba70820bd2acc55ed2b326dd8c0ac7c93f68f0067daecad82aef5f938,这个表示容器的 ID,一般可作为日志的文件名。

我们可以使用 docker ps 命令查看容器是否有在运行:

$ docker ps
CONTAINER ID IMAGE ... PORTS NAMES
6dd4380ba708 nginx ... 0.0.0.0:8081->80/tcp runoob-nginx-test

PORTS 部分表示端口映射,本地的 8081 端口映射到容器内部的 80 端口。

在浏览器中打开 http://127.0.0.1:8081/,效果如下:

Docker 安装 Nginx、PHP、MySQL、Tomcat、Python、Redis、Apache

 


nginx 部署

首先,创建目录 nginx, 用于存放后面的相关东西。

$ mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf

拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录,容器 ID 可以查看 docker ps 命令输入中的第一列:

docker cp 6dd4380ba708:/etc/nginx/nginx.conf ~/nginx/conf

部署命令

$ docker run -d -p 8082:80 --name runoob-nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx

命令说明:

启动以上命令后进入 ~/nginx/www 目录:

$ cd ~/nginx/www

创建 index.html 文件,内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
 <h1>我的第一个标题</h1>
 <p>我的第一个段落。</p>
</body>
</html>

输出结果为:

Docker 安装 Nginx、PHP、MySQL、Tomcat、Python、Redis、Apache

 

相关命令

如果要重新载入 NGINX 可以使用以下命令发送 HUP 信号到容器:

$ docker kill -s HUP container-name

重启 NGINX 容器命令:

$ docker restart container-name

Docker 安装 PHP

安装 PHP 镜像

方法一、docker pull php

查找Docker Hub上的php镜像

runoob@runoob:~/php-fpm$ docker search php
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
php While designed for web development, the PH... 1232 [OK] 
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 207 [OK]
phpmyadmin/phpmyadmin A web interface for MySQL and MariaDB. 123 [OK]
eboraas/Apache-php PHP5 on Apache (with SSL support), built o... 69 [OK]
php-zendserver Zend Server - the integrated PHP Applicati... 69 [OK] 
million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS... 67 [OK]
webdevops/php-nginx Nginx with PHP-FPM 39 [OK]
webdevops/php-apache Apache with PHP-FPM (based on webdevops/php) 14 [OK]
phpunit/phpunit PHPUnit is a programmer-oriented testing f... 14 [OK]
tetraweb/php PHP 5.3, 5.4, 5.5, 5.6, 7.0 for CI and run... 12 [OK]
webdevops/php PHP (FPM and CLI) service container 10 [OK]
...

这里我们拉取官方的镜像,标签为5.6-fpm

runoob@runoob:~/php-fpm$ docker pull php:5.6-fpm

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为php,标签为5.6-fpm的镜像。

runoob@runoob:~/php-fpm$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php 5.6-fpm 025041cd3aa5 6 days ago 456.3 MB

Nginx + PHP 部署

Nginx 部署可以查看:Docker 安装 Nginx,一些 Nginx 的配置参考这篇文章。

启动 PHP:

$ docker run --name myphp-fpm -v ~/nginx/www:/www -d php:5.6-fpm

命令说明:

创建 ~/nginx/conf/conf.d 目录:

mkdir ~/nginx/conf/conf.d 

在该目录下添加 ~/nginx/conf/conf.d/runoob-test-php.conf 文件,内容如下:

server {
 listen 80;
 server_name localhost;
 location / {
 root /usr/share/nginx/html;
 index index.html index.htm index.php;
 }
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 }
 location ~ .php$ {
 fastcgi_pass php:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
 include fastcgi_params;
 }
}

配置文件说明:

启动 nginx:

docker run --name runoob-php-nginx -p 8083:80 -d 
 -v ~/nginx/www:/usr/share/nginx/html:ro 
 -v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro 
 --link myphp-fpm:php 
 nginx

接下来我们在 ~/nginx/www 目录下创建 index.php,代码如下:

<?php
echo phpinfo();
?>

浏览器打开 http://127.0.0.1:8083/index.php,显示如下:

Docker 安装 Nginx、PHP、MySQL、Tomcat、Python、Redis、Apache

 

Docker 安装 MySQL

方法一、docker pull mysql

查找Docker Hub上的mysql镜像

runoob@runoob:/mysql$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relati... 2529 [OK] 
mysql/mysql-server Optimized MySQL Server Docker images. Crea... 161 [OK]
centurylink/mysql Image containing mysql. Optimized to be li... 45 [OK]
sameersbn/mysql 36 [OK]
google/mysql MySQL server for Google Compute Engine 16 [OK]
appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]
marvambass/mysql MySQL Server based on Ubuntu 14.04 6 [OK]
drupaldocker/mysql MySQL for Drupal 2 [OK]
azukiapp/mysql Docker image to run MySQL by Azuki - http:... 2 [OK]
...

这里我们拉取官方的镜像,标签为5.6

runoob@runoob:~/mysql$ docker pull mysql:5.6

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql,标签为5.6的镜像。

runoob@runoob:~/mysql$ docker images |grep mysql
mysql 5.6 2c0964ec182a 3 weeks ago 329 MB

方法二、通过 Dockerfile构建

创建Dockerfile

首先,创建目录mysql,用于存放后面的相关东西。

runoob@runoob:~$ mkdir -p ~/mysql/data ~/mysql/logs ~/mysql/conf

data目录将映射为mysql容器配置的数据文件存放路径

logs目录将映射为mysql容器的日志目录

conf目录里的配置文件将映射为mysql容器的配置文件

进入创建的mysql目录,创建Dockerfile

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-cachenskip-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"]

通过Dockerfile创建一个镜像,替换成你自己的名字

runoob@runoob:~/mysql$ docker build -t mysql .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

runoob@runoob:~/mysql$ docker images |grep mysql
mysql 5.6 2c0964ec182a 3 weeks ago 329 MB

使用mysql镜像

运行容器

runoob@runoob:~/mysql$ docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
21cb89213c93d805c5bacf1028a0da7b5c5852761ba81327e6b99bb3ea89930e
runoob@runoob:~/mysql$ 

命令说明:

查看容器启动情况

runoob@runoob:~/mysql$ docker ps 
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
21cb89213c93 mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql

Docker 安装 Tomcat

方法一、docker pull tomcat

查找Docker Hub上的tomcat镜像

runoob@runoob:~/tomcat$ docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementa... 744 [OK] 
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 19 [OK]
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
consol/tomcat-8.0 Tomcat 8.0.15, 8080, "admin/admin" 14 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 8 [OK]
davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle JAVA 7/8 wi... 6 [OK]
andreptb/tomcat Debian Jessie based image with Apache Tomc... 4 [OK]
kieker/tomcat 2 [OK]
fbrx/tomcat Minimal Tomcat image based on Alpine linux 2 [OK]
jtech/tomcat Latest Tomcat production distribution on l... 1 [OK]

这里我们拉取官方的镜像

runoob@runoob:~/tomcat$ docker pull tomcat

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为tomcat的镜像。

runoob@runoob:~/tomcat$ docker images|grep tomcat
tomcat latest 70f819d3d2d9 7 days ago 335.8 MB

方法二、通过 Dockerfile 构建

创建Dockerfile

首先,创建目录tomcat,用于存放后面的相关东西。

runoob@runoob:~$ mkdir -p ~/tomcat/webapps ~/tomcat/logs ~/tomcat/conf

webapps目录将映射为tomcat容器配置的应用程序目录

logs目录将映射为tomcat容器的日志目录

conf目录里的配置文件将映射为tomcat容器的配置文件

进入创建的tomcat目录,创建Dockerfile

FROM openjdk:8-jre
ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME
# let "Tomcat Native" live somewhere isolated
ENV TOMCAT_NATIVE_LIBDIR $CATALINA_HOME/native-jni-lib
ENV LD_LIBRARY_PATH ${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$TOMCAT_NATIVE_LIBDIR
# runtime dependencies for Tomcat Native Libraries
# Tomcat Native 1.2+ requires a newer version of OpenSSL than debian:jessie has available
# > checking OpenSSL library version >= 1.0.2...
# > configure: error: Your version of OpenSSL is not compatible with this version of tcnative
# see http://tomcat.10.x6.nabble.com/VOTE-Release-Apache-Tomcat-8-0-32-tp5046007p5046024.html (and following discussion)
# and https://github.com/docker-library/tomcat/pull/31
ENV OPENSSL_VERSION 1.1.0f-3+deb9u2
RUN set -ex; 
 currentVersion="$(dpkg-query --show --showformat '${Version}n' openssl)"; 
 if dpkg --compare-versions "$currentVersion" '<<' "$OPENSSL_VERSION"; then 
 if ! grep -q stretch /etc/apt/sources.list; then 
# only add stretch if we're not already building from within stretch
 { 
 echo 'deb http://deb.debian.org/debian stretch main'; 
 echo 'deb http://security.debian.org stretch/updates main'; 
 echo 'deb http://deb.debian.org/debian stretch-updates main'; 
 } > /etc/apt/sources.list.d/stretch.list; 
 { 
# add a negative "Pin-Priority" so that we never ever get packages from stretch unless we explicitly request them
 echo 'Package: *'; 
 echo 'Pin: release n=stretch*'; 
 echo 'Pin-Priority: -10'; 
 echo; 
# ... except OpenSSL, which is the reason we're here
 echo 'Package: openssl libssl*'; 
 echo "Pin: version $OPENSSL_VERSION"; 
 echo 'Pin-Priority: 990'; 
 } > /etc/apt/preferences.d/stretch-openssl; 
 fi; 
 apt-get update; 
 apt-get install -y --no-install-recommends openssl="$OPENSSL_VERSION"; 
 rm -rf /var/lib/apt/lists/*; 
 fi
RUN apt-get update && apt-get install -y --no-install-recommends 
 libapr1 
 && rm -rf /var/lib/apt/lists/*
# see https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/KEYS
# see also "update.sh" (https://github.com/docker-library/tomcat/blob/master/update.sh)
ENV GPG_KEYS 05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23
ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.5.32
ENV TOMCAT_SHA512 fc010f4643cb9996cad3812594190564d0a30be717f659110211414faf8063c61fad1f18134154084ad3ddfbbbdb352fa6686a28fbb6402d3207d4e0a88fa9ce
ENV TOMCAT_TGZ_URLS 
# https://issues.apache.org/jira/browse/INFRA-8753?focusedCommentId=14735394#comment-14735394
 https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz 
# if the version is outdated, we might have to pull from the dist/archive :/
 https://www-us.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz 
 https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz 
 https://archive.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz
ENV TOMCAT_ASC_URLS 
 https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc 
# not all the mirrors actually carry the .asc files :'(
 https://www-us.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc 
 https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc 
 https://archive.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc
RUN set -eux; 
 
 savedAptMark="$(apt-mark showmanual)"; 
 apt-get update; 
 
 apt-get install -y --no-install-recommends gnupg dirmngr; 
 
 export GNUPGHOME="$(mktemp -d)"; 
 for key in $GPG_KEYS; do 
 gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; 
 done; 
 
 apt-get install -y --no-install-recommends wget ca-certificates; 
 
 success=; 
 for url in $TOMCAT_TGZ_URLS; do 
 if wget -O tomcat.tar.gz "$url"; then 
 success=1; 
 break; 
 fi; 
 done; 
 [ -n "$success" ]; 
 
 echo "$TOMCAT_SHA512 *tomcat.tar.gz" | sha512sum -c -; 
 
 success=; 
 for url in $TOMCAT_ASC_URLS; do 
 if wget -O tomcat.tar.gz.asc "$url"; then 
 success=1; 
 break; 
 fi; 
 done; 
 [ -n "$success" ]; 
 
 gpg --batch --verify tomcat.tar.gz.asc tomcat.tar.gz; 
 tar -xvf tomcat.tar.gz --strip-components=1; 
 rm bin/*.bat; 
 rm tomcat.tar.gz*; 
 rm -rf "$GNUPGHOME"; 
 
 nativeBuildDir="$(mktemp -d)"; 
 tar -xvf bin/tomcat-native.tar.gz -C "$nativeBuildDir" --strip-components=1; 
 apt-get install -y --no-install-recommends 
 dpkg-dev 
 gcc 
 libapr1-dev 
 libssl-dev 
 make 
 "openjdk-${JAVA_VERSION%%[.~bu-]*}-jdk=$JAVA_DEBIAN_VERSION" 
 ; 
 ( 
 export CATALINA_HOME="$PWD"; 
 cd "$nativeBuildDir/native"; 
 gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; 
 ./configure 
 --build="$gnuArch" 
 --libdir="$TOMCAT_NATIVE_LIBDIR" 
 --prefix="$CATALINA_HOME" 
 --with-apr="$(which apr-1-config)" 
 --with-java-home="$(docker-java-home)" 
 --with-ssl=yes; 
 make -j "$(nproc)"; 
 make install; 
 ); 
 rm -rf "$nativeBuildDir"; 
 rm bin/tomcat-native.tar.gz; 
 
# reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies
 apt-mark auto '.*' > /dev/null; 
 [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; 
 apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; 
 rm -rf /var/lib/apt/lists/*; 
 
# sh removes env vars it doesn't support (ones with periods)
# https://github.com/docker-library/tomcat/issues/77
 find ./bin/ -name '*.sh' -exec sed -ri 's|^#!/bin/sh$|#!/usr/bin/env bash|' '{}' +
# verify Tomcat Native is working properly
RUN set -e 
 && nativeLines="$(catalina.sh configtest 2>&1)" 
 && nativeLines="$(echo "$nativeLines" | grep 'Apache Tomcat Native')" 
 && nativeLines="$(echo "$nativeLines" | sort -u)" 
 && if ! echo "$nativeLines" | grep 'INFO: Loaded APR based Apache Tomcat Native library' >&2; then 
 echo >&2 "$nativeLines"; 
 exit 1; 
 fi
EXPOSE 8080
CMD ["catalina.sh", "run"]

通过Dockerfile创建一个镜像,替换成你自己的名字

runoob@runoob:~/tomcat$ docker build -t tomcat .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

runoob@runoob:~/tomcat$ docker images|grep tomcat
tomcat latest 70f819d3d2d9 7 days ago 335.8 MB

使用tomcat镜像

运行容器

runoob@runoob:~/tomcat$ docker run --name tomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat 
acb33fcb4beb8d7f1ebace6f50f5fc204b1dbe9d524881267aa715c61cf75320
runoob@runoob:~/tomcat$

命令说明:

-p 8080:8080:将容器的8080端口映射到主机的8080端口

-v $PWD/test:/usr/local/tomcat/webapps/test:将主机中当前目录下的test挂载到容器的/test

查看容器启动情况

runoob@runoob:~/tomcat$ docker ps 
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
acb33fcb4beb tomcat "catalina.sh run" ... 0.0.0.0:8080->8080/tcp tomcat

通过浏览器访问

Docker 安装 Nginx、PHP、MySQL、Tomcat、Python、Redis、Apache

 

Docker 安装 Python


方法一、docker pull python:3.5

查找Docker Hub上的python镜像

runoob@runoob:~/python$ docker search python
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted,... 982 [OK] 
kaggle/python Docker image for Python... 33 [OK]
azukiapp/python Docker image to run Python ... 3 [OK]
vimagick/python mini python 2 [OK]
tsuru/python Image for the Python ... 2 [OK]
pandada8/alpine-python An alpine based python image 1 [OK]
1science/python Python Docker images based on ... 1 [OK]
lucidfrontier45/python-uwsgi Python with uWSGI 1 [OK]
orbweb/python Python image 1 [OK]
pathwar/python Python template for Pathwar levels 1 [OK]
rounds/10m-python Python, setuptools and pip. 0 [OK]
ruimashita/python ubuntu 14.04 python 0 [OK]
tnanba/python Python on CentOS-7 image. 0 [OK]

这里我们拉取官方的镜像,标签为3.5

runoob@runoob:~/python$ docker pull python:3.5

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为python,标签为3.5的镜像。

runoob@runoob:~/python$ docker images python:3.5 
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.5 045767ddf24a 9 days ago 684.1 MB

方法二、通过 Dockerfile 构建

创建Dockerfile

首先,创建目录python,用于存放后面的相关东西。

runoob@runoob:~$ mkdir -p ~/python ~/python/myapp

myapp目录将映射为python容器配置的应用目录

进入创建的python目录,创建Dockerfile

FROM buildpack-deps:jessie
# remove several traces of debian python
RUN apt-get purge -y python.*
# http://bugs.python.org/issue19846
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
ENV LANG C.UTF-8
# gpg: key F73C700D: public key "Larry Hastings <larry@hastings.org>" imported
ENV GPG_KEY 97FC712E4C024BBEA48A61ED3A5CA953F73C700D
ENV PYTHON_VERSION 3.5.1
# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
ENV PYTHON_PIP_VERSION 8.1.2
RUN set -ex 
 && curl -fSL "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" -o python.tar.xz 
 && curl -fSL "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" -o python.tar.xz.asc 
 && export GNUPGHOME="$(mktemp -d)" 
 && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEY" 
 && gpg --batch --verify python.tar.xz.asc python.tar.xz 
 && rm -r "$GNUPGHOME" python.tar.xz.asc 
 && mkdir -p /usr/src/python 
 && tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz 
 && rm python.tar.xz 
 
 && cd /usr/src/python 
 && ./configure --enable-shared --enable-unicode=ucs4 
 && make -j$(nproc) 
 && make install 
 && ldconfig 
 && pip3 install --no-cache-dir --upgrade --ignore-installed pip==$PYTHON_PIP_VERSION 
 && find /usr/local -depth 
 ( 
 ( -type d -a -name test -o -name tests ) 
 -o 
 ( -type f -a -name '*.pyc' -o -name '*.pyo' ) 
 ) -exec rm -rf '{}' + 
 && rm -rf /usr/src/python ~/.cache
# make some useful symlinks that are expected to exist
RUN cd /usr/local/bin 
 && ln -s easy_install-3.5 easy_install 
 && ln -s idle3 idle 
 && ln -s pydoc3 pydoc 
 && ln -s python3 python 
 && ln -s python3-config python-config
CMD ["python3"]

通过Dockerfile创建一个镜像,替换成你自己的名字

runoob@runoob:~/python$ docker build -t python:3.5 .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

runoob@runoob:~/python$ docker images python:3.5 
REPOSITORY TAG IMAGE ID CREATED SIZE
python 3.5 045767ddf24a 9 days ago 684.1 MB

使用python镜像

在~/python/myapp目录下创建一个 helloworld.py 文件,代码如下:

#!/usr/bin/python
print("Hello, World!");

运行容器

runoob@runoob:~/python$ docker run -v $PWD/myapp:/usr/src/myapp -w /usr/src/myapp python:3.5 python helloworld.py

命令说明:

-v $PWD/myapp:/usr/src/myapp :将主机中当前目录下的myapp挂载到容器的/usr/src/myapp

-w /usr/src/myapp :指定容器的/usr/src/myapp目录为工作目录

python helloworld.py :使用容器的python命令来执行工作目录中的helloworld.py文件

输出结果:

Hello, World!

Docker 安装 redis


方法一、docker pull redis:3.2

查找Docker Hub上的redis镜像

runoob@runoob:~/redis$ docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source ... 2321 [OK] 
sameersbn/redis 32 [OK]
torusware/speedus-redis Always updated official ... 29 [OK]
bitnami/redis Bitnami Redis Docker Image 22 [OK]
anapsix/redis 11MB Redis server image ... 6 [OK]
webhippie/redis Docker images for redis 4 [OK]
clue/redis-benchmark A minimal docker image t... 3 [OK]
williamyeh/redis Redis image for Docker 3 [OK]
unblibraries/redis Leverages phusion/baseim... 2 [OK]
greytip/redis redis 3.0.3 1 [OK]
servivum/redis Redis Docker Image 1 [OK]
...

这里我们拉取官方的镜像,标签为3.2

runoob@runoob:~/redis$ docker pull redis:3.2

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为redis,标签为3.2的镜像。

runoob@runoob:~/redis$ docker images redis 
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 3.2 43c923d57784 2 weeks ago 193.9 MB

方法二、通过 Dockerfile 构建

创建Dockerfile

首先,创建目录redis,用于存放后面的相关东西。

runoob@runoob:~$ mkdir -p ~/redis ~/redis/data

data目录将映射为redis容器配置的/data目录,作为redis数据持久化的存储目录

进入创建的redis目录,创建Dockerfile

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 redis && useradd -r -g redis redis
RUN apt-get update && apt-get install -y --no-install-recommends 
 ca-certificates 
 wget 
 && rm -rf /var/lib/apt/lists/*
# grab gosu for easy step-down from root
ENV GOSU_VERSION 1.7
RUN set -x 
 && 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
ENV REDIS_VERSION 3.2.0
ENV REDIS_DOWNLOAD_URL http://download.redis.io/releases/redis-3.2.0.tar.gz
ENV REDIS_DOWNLOAD_SHA1 0c1820931094369c8cc19fc1be62f598bc5961ca
# for redis-sentinel see: http://redis.io/topics/sentinel
RUN buildDeps='gcc libc6-dev make' 
 && set -x 
 && apt-get update && apt-get install -y $buildDeps --no-install-recommends 
 && rm -rf /var/lib/apt/lists/* 
 && wget -O redis.tar.gz "$REDIS_DOWNLOAD_URL" 
 && echo "$REDIS_DOWNLOAD_SHA1 *redis.tar.gz" | sha1sum -c - 
 && mkdir -p /usr/src/redis 
 && tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 
 && rm redis.tar.gz 
 && make -C /usr/src/redis 
 && make -C /usr/src/redis install 
 && rm -r /usr/src/redis 
 && apt-get purge -y --auto-remove $buildDeps
RUN mkdir /data && chown redis:redis /data
VOLUME /data
WORKDIR /data
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 6379
CMD [ "redis-server" ]

通过Dockerfile创建一个镜像,替换成你自己的名字

runoob@runoob:~/redis$ docker build -t redis:3.2 .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

runoob@runoob:~/redis$ docker images redis 
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 3.2 43c923d57784 2 weeks ago 193.9 MB

使用redis镜像

运行容器

runoob@runoob:~/redis$ docker run -p 6379:6379 -v $PWD/data:/data -d redis:3.2 redis-server --appendonly yes
43f7a65ec7f8bd64eb1c5d82bc4fb60e5eb31915979c4e7821759aac3b62f330
runoob@runoob:~/redis$

命令说明:

-p 6379:6379 : 将容器的6379端口映射到主机的6379端口

-v $PWD/data:/data : 将主机中当前目录下的data挂载到容器的/data

redis-server --appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置

查看容器启动情况

runoob@runoob:~/redis$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
43f7a65ec7f8 redis:3.2 "docker-entrypoint.sh" ... 0.0.0.0:6379->6379/tcp agitated_cray

连接、查看容器

使用redis镜像执行redis-cli命令连接到刚启动的容器,主机IP为172.17.0.1

runoob@runoob:~/redis$ docker exec -it 43f7a65ec7f8 redis-cli
172.17.0.1:6379> info
# Server
redis_version:3.2.0
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:f449541256e7d446
redis_mode:standalone
os:Linux 4.2.0-16-generic x86_64
arch_bits:64
multiplexing_api:epoll
...

Docker 安装 Apache


方法一、docker pull httpd

查找Docker Hub上的httpd镜像

runoob@runoob:~/apache$ docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server .. 524 [OK] 
centos/httpd 7 [OK]
rgielen/httpd-image-php5 Docker image for Apache... 1 [OK]
microwebapps/httpd-frontend Httpd frontend allowing... 1 [OK]
lolhens/httpd Apache httpd 2 Server 1 [OK]
publici/httpd httpd:latest 0 [OK]
publicisworldwide/httpd The Apache httpd webser... 0 [OK]
rgielen/httpd-image-simple Docker image for simple... 0 [OK]
solsson/httpd Derivatives of the offi... 0 [OK]
rgielen/httpd-image-drush Apache HTTPD + Drupal S... 0 [OK]
learninglayers/httpd 0 [OK]
sohrabkhan/httpd Docker httpd + php5.6 (... 0 [OK]
aintohvri/docker-httpd Apache HTTPD Docker ext... 0 [OK]
alizarion/httpd httpd on centos with mo... 0 [OK]
...

这里我们拉取官方的镜像

runoob@runoob:~/apache$ docker pull httpd

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为httpd的镜像。

runoob@runoob:~/apache$ docker images httpd
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest da1536b4ef14 23 seconds ago 195.1 MB

方法二、通过 Dockerfile构建

创建Dockerfile

首先,创建目录apache,用于存放后面的相关东西。

runoob@runoob:~$ mkdir -p ~/apache/www ~/apache/logs ~/apache/conf 

www目录将映射为apache容器配置的应用程序目录

logs目录将映射为apache容器的日志目录

conf目录里的配置文件将映射为apache容器的配置文件

进入创建的apache目录,创建Dockerfile

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"]

Dockerfile文件中 COPY httpd-foreground /usr/local/bin/ 是将当前目录下的httpd-foreground拷贝到镜像里,作为httpd服务的启动脚本,所以我们要在本地创建一个脚本文件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

赋予httpd-foreground文件可执行权限

runoob@runoob:~/apache$ chmod +x httpd-foreground

通过Dockerfile创建一个镜像,替换成你自己的名字

runoob@runoob:~/apache$ docker build -t httpd .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

runoob@runoob:~/apache$ docker images httpd
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest da1536b4ef14 23 seconds ago 195.1 MB

使用apache镜像

运行容器

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/

查看容器启动情况

runoob@runoob:~/apache$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
79a97f2aac37 httpd "httpd-foreground" ... 0.0.0.0:80->80/tcp sharp_swanson

通过浏览器访问

Docker 安装 Nginx、PHP、MySQL、Tomcat、Python、Redis、Apache

 

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>