Docker 安装 MySQL
方法一、docker pull mysql
查找Docker Hub上的mysql镜像
[email protected]:/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
[email protected]:~/mysql$ docker pull mysql:5.6
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql,标签为5.6的镜像。
[email protected]:~/mysql$ docker images |grep mysql mysql 5.6 2c0964ec182a 3 weeks ago 329 MB
方法二、通过 Dockerfile构建
创建Dockerfile
首先,创建目录mysql,用于存放后面的相关东西。
[email protected]:~$ 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 addedRUN groupadd -r mysql && useradd -r -g mysql mysql# add gosu for easy step-down from rootENV GOSU_VERSION 1.7RUN 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 <[email protected]>" 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"]
通过Dockerfile创建一个镜像,替换成你自己的名字
[email protected]:~/mysql$ docker build -t mysql .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
[email protected]:~/mysql$ docker images |grep mysql mysql 5.6 2c0964ec182a 3 weeks ago 329 MB
使用mysql镜像
运行容器
[email protected]:~/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.621cb892[email protected]runoob:~/mysql$
命令说明:
- -p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。
- -v -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。
- -v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。
- -v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。
- -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
查看容器启动情况
[email protected]:~/mysql$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES21cb89213c93 mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql
Docker 安装 Tomcat
方法一、docker pull tomcat
查找Docker Hub上的tomcat镜像
[email protected]:~/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]
这里我们拉取官方的镜像
[email protected]:~/tomcat$ docker pull tomcat
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为tomcat的镜像。
[email protected]:~/tomcat$ docker images|grep tomcat tomcat latest 70f819d3d2d9 7 days ago 335.8 MB
方法二、通过 Dockerfile 构建
创建Dockerfile
首先,创建目录tomcat,用于存放后面的相关东西。
[email protected]:~$ 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 isolatedENV 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/31ENV 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创建一个镜像,替换成你自己的名字
[email protected]:~/tomcat$ docker build -t tomcat .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
[email protected]:~/tomcat$ docker images|grep tomcat tomcat latest 70f819d3d2d9 7 days ago 335.8 MB
使用tomcat镜像
运行容器
[email protected]:~/tomcat$ docker run --name tomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat acb33fcb4beb8d7f1ebace6f50f5fc204b1dbe9d524881267aa715c61cf75320 [email protected]:~/tomcat$
命令说明:
-p 8080:8080:将容器的8080端口映射到主机的8080端口
-v $PWD/test:/usr/local/tomcat/webapps/test:将主机中当前目录下的test挂载到容器的/test
查看容器启动情况
[email protected]:~/tomcat$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES acb33fcb4beb tomcat "catalina.sh run" ... 0.0.0.0:8080->8080/tcp tomcat
通过浏览器访问
Docker 安装 Python
<h2安装 python="" 镜像
方法一、docker pull python:3.5
查找Docker Hub上的python镜像
[email protected]:~/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
[email protected]:~/python$ docker pull python:3.5
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为python,标签为3.5的镜像。
[email protected]:~/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,用于存放后面的相关东西。
[email protected]:~$ 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 <[email protected]>" 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创建一个镜像,替换成你自己的名字
[email protected]:~/python$ docker build -t python:3.5 .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
[email protected]:~/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!");
运行容器
[email protected]:~/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镜像
[email protected]:~/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
[email protected]:~/redis$ docker pull redis:3.2
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为redis,标签为3.2的镜像。
[email protected]:~/redis$ docker images redis REPOSITORY TAG IMAGE ID CREATED SIZE redis 3.2 43c923d57784 2 weeks ago 193.9 MB
方法二、通过 Dockerfile 构建
创建Dockerfile
首先,创建目录redis,用于存放后面的相关东西。
[email protected]:~$ 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 addedRUN 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创建一个镜像,替换成你自己的名字
[email protected]:~/redis$ docker build -t redis:3.2 .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
[email protected]:~/redis$ docker images redis REPOSITORY TAG IMAGE ID CREATED SIZE redis 3.2 43c923d57784 2 weeks ago 193.9 MB
使用redis镜像
运行容器
[email protected]:~/redis$ docker run -p 6379:6379 -v $PWD/data:/data -d redis:3.2 redis-server --appendonly yes43f7a65ec7[email protected]oob:~/redis$
命令说明:
-p 6379:6379 : 将容器的6379端口映射到主机的6379端口
-v $PWD/data:/data : 将主机中当前目录下的data挂载到容器的/data
redis-server --appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置
查看容器启动情况
[email protected]:~/redis$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES43f7a65ec7f8 redis:3.2 "docker-entrypoint.sh" ... 0.0.0.0:6379->6379/tcp agitated_cray
连接、查看容器
使用redis镜像执行redis-cli命令连接到刚启动的容器,主机IP为172.17.0.1
[email protected]:~/redis$ docker exec -it 43f7a65ec7f8 redis-cli172.17.0.1:6379> info# Serverredis_version:3.2.0redis_git_sha1:00000000redis_git_dirty:0redis_build_id:f449541256e7d446 redis_mode:standalone os:Linux 4.2.0-16-generic x86_64 arch_bits:64multiplexing_api:epoll...
Docker 安装 MongoDB
方法一、docker pull mongo:3.2
查找Docker Hub上的mongo镜像
[email protected]:~/mongo$ docker search mongo NAME DESCRIPTION STARS OFFICIAL AUTOMATED mongo MongoDB document databases ... 1989 [OK] mongo-express Web-based MongoDB admin int... 22 [OK] mvertes/alpine-mongo light MongoDB container 19 [OK]mongooseim/mongooseim-docker MongooseIM server the lates... 9 [OK]torusware/speedus-mongo Always updated official Mon... 9 [OK]jacksoncage/mongo Instant MongoDB sharded cluster 6 [OK]mongoclient/mongoclient Official docker image for M... 4 [OK]jadsonlourenco/mongo-rocks Percona Mongodb with Rocksd... 4 [OK]asteris/apache-php-mongo Apache2.4 + PHP + Mongo + m... 2 [OK]19hz/mongo-container Mongodb replicaset for coreos 1 [OK]nitra/mongo Mongo3 centos7 1 [OK]ackee/mongo MongoDB with fixed Bluemix p... 1 [OK]kobotoolbox/mongo https://github.com/kobotoolb... 1 [OK]valtlfelipe/mongo Docker Image based on the la... 1 [OK]
这里我们拉取官方的镜像,标签为3.2
[email protected]:~/mongo$ docker pull mongo:3.2
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mongo,标签为3.2的镜像。
[email protected]:~/mongo$ docker images mongo:3.2REPOSITORY TAG IMAGE ID CREATED SIZE mongo 3.2 282fd552add6 9 days ago 336.1 MB
方法二、通过 Dockerfile 构建
创建Dockerfile
首先,创建目录mongo,用于存放后面的相关东西。
[email protected]:~$ mkdir -p ~/mongo ~/mongo/db
db目录将映射为mongo容器配置的/data/db目录,作为mongo数据的存储目录
进入创建的mongo目录,创建Dockerfile
FROM debian:wheezy# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get addedRUN groupadd -r mongodb && useradd -r -g mongodb mongodb RUN apt-get update && apt-get install -y --no-install-recommends numactl && rm -rf /var/lib/apt/lists/* # grab 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 # gpg: key 7F0CEB10: public key "Richard Kreuter <[email protected]>" imported RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 492EAFE8CD016A07919F1D2B9ECBEC467F0CEB10 ENV MONGO_MAJOR 3.0 ENV MONGO_VERSION 3.0.12 RUN echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/$MONGO_MAJOR main" > /etc/apt/sources.list.d/mongodb-org.list RUN set -x && apt-get update && apt-get install -y mongodb-org=$MONGO_VERSION mongodb-org-server=$MONGO_VERSION mongodb-org-shell=$MONGO_VERSION mongodb-org-mongos=$MONGO_VERSION mongodb-org-tools=$MONGO_VERSION && rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/mongodb && mv /etc/mongod.conf /etc/mongod.conf.orig RUN mkdir -p /data/db /data/configdb && chown -R mongodb:mongodb /data/db /data/configdb VOLUME /data/db /data/configdb COPY docker-entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] EXPOSE 27017 CMD ["mongod"]
通过Dockerfile创建一个镜像,替换成你自己的名字
[email protected]:~/mongo$ docker build -t mongo:3.2 .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
[email protected]:~/mongo$ docker images mongo:3.2REPOSITORY TAG IMAGE ID CREATED SIZE mongo 3.2 282fd552add6 9 days ago 336.1 MB
使用mongo镜像
运行容器
[email protected]:~/mongo$ docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo:3.2cda8830cad5fe35e9c4aed037bbd5434b69b19bf2075c8626911e6ebb08cad51 [email protected]:~/mongo$
命令说明:
-p 27017:27017 :将容器的27017 端口映射到主机的27017 端口
-v $PWD/db:/data/db :将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录
查看容器启动情况
[email protected]:~/mongo$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES cda8830cad5f mongo:3.2 "/entrypoint.sh mongo" ... 0.0.0.0:27017->27017/tcp suspicious_goodall
使用mongo镜像执行mongo 命令连接到刚启动的容器,主机IP为172.17.0.1
[email protected]:~/mongo$ docker run -it mongo:3.2 mongo --host 172.17.0.1MongoDB shell version: 3.2.7connecting to: 172.17.0.1:27017/testWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, see http://docs.mongodb.org/Questions? Try the support group http://groups.google.com/group/mongodb-user>
Docker 安装 Apache
方法一、docker pull httpd
查找Docker Hub上的httpd镜像
[email protected]:~/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]...
这里我们拉取官方的镜像
[email protected]:~/apache$ docker pull httpd
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为httpd的镜像。
[email protected]:~/apache$ docker images httpd REPOSITORY TAG IMAGE ID CREATED SIZE httpd latest da1536b4ef14 23 seconds ago 195.1 MB
方法二、通过 Dockerfile构建
创建Dockerfile
首先,创建目录apache,用于存放后面的相关东西。
[email protected]:~$ 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-dataENV 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#requirementsRUN 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/bashset -e# Apache gets grumpy about PID files pre-existingrm -f /usr/local/apache2/logs/httpd.pidexec httpd -DFOREGROUND
赋予httpd-foreground文件可执行权限
[email protected]:~/apache$ chmod +x httpd-foreground
通过Dockerfile创建一个镜像,替换成你自己的名字
[email protected]:~/apache$ docker build -t httpd .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
[email protected]:~/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/
查看容器启动情况
[email protected]:~/apache$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES79a97f2aac37 httpd "httpd-foreground" ... 0.0.0.0:80->80/tcp sharp_swanson
通过浏览器访问
Docker 命令大全
容器生命周期管理
容器操作
容器rootfs命令
镜像仓库
本地镜像管理
info|version
Docker 资源汇总
Docker官方英文资源
docker官网:http://www.docker.com
Docker windows入门:https://docs.docker.com/windows/
Docker Linux 入门:https://docs.docker.com/linux/
Docker mac 入门:https://docs.docker.com/mac/
Docker 用户指引:https://docs.docker.com/engine/userguide/
Docker 官方博客:http://blog.docker.com/
Docker Hub: https://hub.docker.com/
Docker开源: https://www.docker.com/open-source
Docker中文资源
Docker中文网站:https://www.docker-cn.com/
Docker安装手册:https://docs.docker-cn.com/engine/installation/
Docker 国内镜像
网易加速器:http://hub-mirror.c.163.com
官方中国加速器:https://registry.docker-cn.com
ustc的镜像:https://docker.mirrors.ustc.edu.cn
daocloud:https://www.daocloud.io/mirror#accelerator-doc(注册后使用)
原文地址:http://blog.51cto.com/lwm666/2149759