本文是记录常用的docker
实例的笔记,尽量使用简单的文字介绍docker
实例的使用。所有操作仅在CENTOS7上测试过,但是docker
命令是通用的,不同的是配置文件目录位置或者系统命令。
docker 常用系统镜像
busybox
alpine
debian
ubuntu
centOS
fedora
为镜像添加SSH服务 基于ubuntu_14.04
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 示例中$为宿主机内,@代表docker容器内 $ docker run -it ubuntu:14.04 /bin/bash @ apt-get update @ apt-get install openssh-server -y @ mkdir -p /var/run/sshd @ /usr/sbin/sshd -D & # 可以检查端口22 查看程序运行情况 # 修改ssh服务安全登陆配置 取消pam登陆限制 @ sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd @ mkdir /root/.ssh # 编辑文件 复制信息到文件中 可以通过 ssh-keygen-t rsa 命令生成 @ vi /root/.ssh/authorized_keys # 创建自动启动脚本 @ vi /run.sh @ chmod +x /run.sh @ exit
run.sh 内容
1 2 #!/bin/bash /usr/sbin/sshd -D
或者使用Dockerfile直接生成镜像:
1 2 3 4 5 6 7 8 9 10 11 12 FROM ubuntu:14.04 MAINTAINER docker_user(user@docker.com) RUN apt-get update RUN apt-get install -y openssh-server RUN mkdir -p /var/run/sshd RUN mkdir -p /root/.ssh RUN sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd ADD authorized_keys /root/.ssh/authorized_keys ADD run.sh /run.sh RUN chmod 755 /run.sh EXPOSE 22 CMD ["/run.sh"]
使用命令生成镜像:
1 2 # 运行命令 docker build -t sshd:Docerfile
apache WEB服务与应用
Dockerfile
1 2 FROM httpd:2.4 COPY ./public-html /usr/local/apache2/htdocs/
构建并运行
1 2 3 4 5 # 构建 docker build -t apache2-image . # 运行 docket run -it --rm --name apache-container -p 80:80 apache2-image docker run -it --rm --name my-apache-app -p 80:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4
使用自定义镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 FROM sshd:Dockerfile MAINTAINER docker\_user(user@docker.com) ENV DEBIAN\_FRONTEND noninteractive RUN apt-get -yq install apache2 && \\ rm -rf /var/lib/apt/lists/* RUN echo "Asia/Shanghai" > /etc/timezone && \\ dpkg-reconfigure -f noninteractive tzdata ADD run.sh /run.sh RUN chmod 755 /*.sh RUN mkdir -p /var/lock/apache2 && mkdir -p /app && rm -rf /var/www/html && ln -s /app /var/www/html COPY sample/ /app ENV APACHE\_RUN\_USER www-data ENV APACHE\_RUN\_GROUP www-data ENV APACHE\_LOG\_DIR /var/log/apache2 ENV APACHE\_PID\_FILE /var/run/apache2.pid ENV APACHE\_RUN\_DIR /var/run/apache2 ENV APACHE\_LOCK\_DIR /var/lock/apache2 ENV APACHE\_SERVERADMIN admin@localhost ENV APACHE\_SERVERNAME localhost ENV APACHE\_SERVERALIAS docker.localhost ENV APACHE\_DOCUMENTROOT /var/www EXPOSE 80 WORKDIR /app CMD ["/run.sh"]
run.sh 内容
1 2 /usr/sbin/sshd & exec apache2 -D FOREGROUND
nginx WEB服务与应用
Dockerfile
1 2 FROM nginx COPY ./index.html /usr/sahre/nginx/html
构建并运行
1 2 3 4 5 6 # 构建 docker build -t my-nginx . # 运行 docker run --name nginx-container -d my-nginx # 直接运行 docker run --name nginx-container -p 80:80 -v index.html:/usr/share/nginx/html:ro -d nginx
tomcat WEB服务与应用
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 FROM sshd:Dockerfile MAINTAINER docker\_user(user@docker.com) ENV DEBIAN\_FRONTEND noninteractive RUN echo "Asia/Shanghai" > /etc/timezone && \\ dpkg-reconfigure -f noninteractive tzdata RUN apt-get install -yq --no-install-recommends wget pwgen ca-certificates && \\ apt-get clean && \\ rm -rf /var/lib/apt/lists/* ENV CATALINA\_HOME /tomcat ENV JAVA\_HOME /jdk ADD apache-tomcat-7.0.56 /tomcat ADD jdk /jdk ADD create\_tomcat\_admin\_user.sh /create\_tomcat\_admin\_user.sh ADD run.sh /run.sh RUN chmod +x /*.sh RUN chmod +x /tomcat/bin/*.sh EXPOSE 8080 CMD ["/run.sh"]
create_tomcat_admin_user.sh 内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 if[-f /.tomcat\_admin\_created ];then echo "Tomcat 'admin' user already created" exit 0 fi PASS=${TOMCAT_PASS:-$(pwgen -s 12 1)} \_word=$([${TOMCAT\_PASS}] && echo "preset" || echo "random") echo "=> Creating and admin user with a ${_word} password in Tomcat" sed -i -r 's/<\/tomcat-users>//' ${CATALINA\_HOME}/conf/tomcat-users.xml echo '<role rolename="manager-gui"/>' >> ${CATALINA_HOME}/conf/tomcat_users.xml echo '<role rolename="manager-script"/>' >> ${CATALINA\_HOME}/conf/tomcat\_users.xml echo '<role rolename="manager-jmx"/>' >> ${CATALINA_HOME}/conf/tomcat_users.xml echo '<role rolename="admin-gui"/>' >> ${CATALINA\_HOME}/conf/tomcat\_users.xml echo '<role rolename="admin-script"/>' >> ${CATALINA_HOME}/conf/tomcat_users.xml echo '<user username="admin" password="${PASS}" roles="manager-gui,manager-script,manager-jmx,admin-gui,admin-script" />' >> ${CATALINA_HOME}/conf/tomcat_users.xml echo '</tomcat-users>' >> ${CATALINA\_HOME}/conf/tomcat\_users.xml echo '==> Done!' touch /.tomcat\_admin\_created echo '====================================================' echo 'You can now configure to this Tomcat Server using' echo "" echo "admin:${PASS}" echo "" echo '===================================================='
run.sh 内容
1 2 3 4 5 if [! -f /.tomcat_admin_created];then /create_tomcat_admin_user.sh & fi /usr/sbin/sshd -D & exec ${CATALINA\_HOME}/bin/catalina.sh run
构建并运行
1 2 3 4 5 6 # 构架 docker build -t tomcat7.0:jdk1.6 . # 运行 docker run -d -P tomcat7.0:jdk1.6 # 获取密码 docker logs <容器ID或者名称>
jetty WEB服务与应用
1 docker run -d -p 80:8080 -p 443:8443 jetty
LAMP 服务与应用
LAMP 即 Linux + Apache + Mysql + Php
1 2 3 4 5 docker run -p 80:80 -it linode/lamp /bin/bash @ systemctl start apache2 @ systemctl start mysql # 或者 docker run -p 80:80 -p 3306:3306 -d tutum/lamp
可持续开发与管理 服务与应用
Jenkins + Gitlab
1 2 docker run -p 8080:8080 -p 50000:50000 -v /your/path:/var/jenkins\_home jenkins docker run --detach --hostname gitlab.example.com --publish 443:443 --publish 80:80 --publish 23:23 --name gitlib --restart always --volume /srv/gitlib/config:/etc/gitlib --volume /srv/gitlib/logs:/var/log/gitlib --volume /srv/gitlib/data:/var/opt/gitlib gitlib/gitlib-ce:latest
mysql 服务与应用
1 2 3 4 5 6 7 8 # 运行 自定义密码和配置文件 docker run --name hi-mysql -eMYSQL\_ROOT\_PASSWORD=123 -v /my/custom:/etc/mysql/conf.d -d mysql:5.6.40 # 链接使用mysql docker run --name some-app --link some-mysql:mysql -d application-that-uses-mysql # 备份 docker exec hi-mysql /bin/sh -c 'exec mysqldump -uroot -p"123456" <db名称> | gzip' > /root/backup/back-$(date +%Y%m%d).sql.gz # 备份所有 docker exec hi-mysql /bin/sh -c 'exec mysqldump --all-databases -uroot -p"123456" | gzip' > /root/backup/back-$(date +%Y%m%d).sql.gz
mongo 服务与应用
1 2 3 4 docker run --name mongo-container -d mongo docker run -it --link mongo-container:db alpine sh # 直接进入数据命令交互模式 docker run -it --link mongo-container:db --entrypoint mongo mongo --host db
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 FROM sshd MAINTAINER docker\_user(user@docker.com) RUN apt-get update && apt-get install -y mongodb pwgen && apt-get clean && rm -rf /var/lib/apt/lists/* RUN mkdir -p /data/db VOLUME /data/db ENV AUTH yes ADD run.sh /run.sh ADD set\_mongodb\_password.sh /set\_mongodb\_password.sh RUN chmod 755 /*.sh EXPOSE 27017 EXPOSE 28017 CMD ["/run.sh"]
set_mongodb_password.sh 内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 if [-f /.mongodb\_password\_set];then echo "MongoDB password already set!" exit 0 fi /usr/bin/mongod --smallfiles --nojournal & PASS=${MONGODB_PASS:-$(pwgen -s 12 1)} \_word=$([${MONGODB\_PASS}] && echo "preset" || echo "random") RET=1 while [[RET -ne 0]];do echo "=> Waiting for confirmation of MongoDB service startup" sleep 5 mongo admin --eval "help" > /dev/null 2>&1 RET=$? done echo "=> Creating an admin user with a ${\_word} password in MongoDB" mongo admin --eval "db.addUser({user:'admin',pwd:'$PASS',roles:['userAdminAnyDatabase','dbAdminAnyDatabase']});" mongo admin --eval "db.shutdownServer();" echo "=> Done!" touch /.mongodb_password_set echo "=================================================" echo "You can now connect to this MongoDB server using" echo "" echo "mongo admin -u admin -p $PASS --host <host> --port <port>" echo "" echo "Please remember to change the above password as soon as possible!" echo "================================================="
run.sh 内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if [-f /.mongodb\_password\_set];then /set\_mongodb\_password.sh fi if ["$AUTH" == "yes"];then export mongodb='/usr/bin/mongod --nojournal --auth --httpinterface --rest' else export mongodb='/usr/bin/mongod --nojournal --httpinterface --rest' fi if [!-f /data/db/mongod.lock];then eval $mongodb else export mongodb=$mongodb' --dbpath /data/db' rm /data/db/mongod.lock mongod --dbpath /data/db --repair && eval $mongodb fi
构建并运行
1 2 3 4 5 6 7 8 9 # 构建 docker build -t mongodb-image . # 运行 docker run -d -p 27017:17017 -p 28017:28017 mongodb # 使用 docker logs <容器名称或者ID> 获取密码 # 指定密码运行 docker run -d -p 27017:17017 -p 28017:28017 -eMONGODB\_PASS="mypass" mongodb # 不需要密码 docker run -d -p 27017:17017 -p 28017:28017 -eAUTH=no mongodb
redis 服务与应用
1 docker run --name redis-container -d redis
memcached 服务与应用
1 2 # 限制内存使用大小 docker run --name memcached-container -d memcached -m 64
CouchDB 服务与应用
1 docker run -d --name couchdb-container couchdb
Cassandra 服务与应用
1 docker run --name my-cassandra -d cassandra:latest
搭建集群
1 2 docker run --name my-cassandra -d -e CAS-SANDRA\_BROADCAST\_ADDRESS=10.42.42.42 -p 7000:7000 cassandra:latest docker run --name my-cassandra -d -e CAS-SANDRA\_BROADCAST\_ADDRESS=10.43.43.43 -p 7000:7000 -e CASSANDRA\_SEEDS=10.42.42.42 cassandra:latest
RabbitMQ 服务与应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 安装以及链接命令 docker run -d --hostname my-rabbit --name some-rabbit rabbitmq:3 docker run -d --hostname my-rabbit --name some-rabbit -e RABBITMQ\_ERLANG\_COOKIE='secret cookie here' rabbitmq:3 docker run -it --rm --link some-rabbit:my-rabbit -e RABBITMQ\_ERLANG\_COOKIE='secret cookie here' rabbitmq:3 bash @ rabbitmqctl -n rabbit@my-rabbit list\_users docker run -it --rm --link some-rabbit:my-rabbit -e RABBITMQ\_ERLANG\_COOKIE='secret cookie here' -e RABBITMQ\_NODENAME=rabbit@my-rabbit rabbitmq:3 bash @ rabbitmqctl list\_users # 管理 docker run -d --hostname my-rabbit --name some-rabbit -p 8080:15672 rabbitmq:3-management # 默认用户名密码 guest guest docker run -d --hostname my-rabbit --name some-rabbit -e RABBITMQ\_DEFAULT\_USER=user -e RABBITMQ\_DEFAULT\_PASS=password rabbitmq:3-management # vhost docker run -d --hostname my-rabbit --name some-rabbit -e RABBITMQ\_DEFAULT\_VHOST=my\_vhost rabbitmq:3-management docker run --name some-app --link some-rabbit:rabbit -d application-that-uses-rabbitmq
Celery 服务与应用
1 2 3 4 5 6 # 官方镜像 docker run --link some-rabbit:rabbit --name some-celery -d celery:latest # 检查集群状态 docker run --link some-rabbit:rabbit --rm celery celery status # 启动celery worker docker run --link some-redis:redis -e CELERY\_BROKER\_URL=redis://redis --name some-celery -d celery
Hadoop 服务与应用
1 2 # 使用官方镜像 docker run -it sequenceiq/hadoop-docker:2.7.0 /etc/bootstrap.sh -bash
Spark 服务与应用
1 2 3 # 官方 docker pull sequenceiq/spark:1.6.0 docker run -it -p 8088:8088 -p 8042:8042 -h sandbox sequenceiq/spark:1.6.0 bash
Storm 服务与应用
1 2 3 4 5 6 git clone https://github.com/denverdino/dockerstorm.git cd docker-swarm/local docker-compose build docker-compose up -d docker-compose scale supervisor=3 docker-compose start topology
Elasticsearch 服务与应用
1 2 3 4 5 6 7 docker run -d elesticsearch docker run -d elesticsearch -Des.node.name="TestNode" # 自定义配置 docker run -d -v "$PWD/config":/usr/share/elasticsearch/config elesticsearch # 数据持久化 docker run -d -v "$PWD/esdata":/usr/share/elasticsearch/data elesticsearch # 9200 对外服务 9300 内部通信
C/C++ 开发环境
Dockerfile
1 2 3 4 5 FROM gcc:4.9 COPY . /usr/src/myapp WORKDIR /usr/src/myapp RUN gcc -o myapp main.c CMD ["./myapp"]
构建并运行
1 2 3 docker build -t gcc-image . docker run -it --rm --name gcc-container gcc-image docker run --rm --v "$PWD":/usr/src/myapp -w /usr/src/myapp gcc:4.9 gcc -o myapp main.c
LLVM
Clang
1 docker pull bowery/clang
Java 开发环境
Dockerfile
1 2 3 4 5 FROM java:7 COPY . /usr/src/javaapp WORKDIR /usr/src/javaapp RUN javac HelloWorld.java CMD ["java","HelloWorld"]
构建并运行
1 2 3 docker build -t java-image docker run -it --rm --name java-container java-image docker run --rm -v "$PWD":/usr/src/javaapp -w /usr/src/javaapp java:7 javac HelloWorld.java
Python 开发环境
Dockerfile
1 2 FROM python:3-onbuild CMD ["python3.5","./py3-sameple.py"]
PyPy
Dockerfile
1 2 FROM pypy:3-onbuild CMD ["pypy3","./hi.py"]
Javascript 开发环境
1 docker pull node:4-onbuild
Go 开发环境
1 2 3 4 5 docker pull golang # beego git clone https://github.com/astaxie/beego.git # Gogs docker run --rm --name gogs -p 8300:3000 -p 8322:22 -v /srv/lxc/gogs/data:/data gogs/gogs
Php 开发环境
Ruby 开发环境
1 2 docker pull ruby:2.1.2-onbuild docker pull jruby:1.7.24-onbuild
Perl 开发环境
R 开发环境
Erlang 开发环境
1 docker pull erlang:latest