• Docker小记


    1.Docker的安装

    这里给一个比较通用的教程

    Ubuntu 14.04 16.04 (使用apt-get进行安装)

     1 # step 1: 安装必要的一些系统工具
     2 
     3 sudo apt-get update
     4 
     5 sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
     6 
     7 # step 2: 安装GPG证书
     8 
     9 curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    10 
    11 # Step 3: 写入软件源信息
    12 
    13 sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
    14 
    15 # Step 4: 更新并安装 Docker-CE
    16 
    17 sudo apt-get -y update
    18 
    19 sudo apt-get -y install docker-ce
    20 
    21 # 安装指定版本的Docker-CE:
    22 
    23 # Step 1: 查找Docker-CE的版本:
    24 
    25 # apt-cache madison docker-ce
    26 
    27 # docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
    28 
    29 # docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
    30 
    31 # Step 2: 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)
    32 
    33 # sudo apt-get -y install docker-ce=[VERSION]

    CentOS 7 (使用yum进行安装)

     1 # step 1: 安装必要的一些系统工具
     2 
     3 sudo yum install -y yum-utils device-mapper-persistent-data lvm2
     4 
     5 # Step 2: 添加软件源信息
     6 
     7 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
     8 
     9 # Step 3: 更新并安装 Docker-CE
    10 
    11 sudo yum makecache fast
    12 
    13 sudo yum -y install docker-ce
    14 
    15 # Step 4: 开启Docker服务
    16 
    17 sudo service docker start
    18 
    19 # 注意:
    20 
    21 # 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,你可以通过以下方式开启。同理可以开启各种测试版本等。
    22 
    23 # vim /etc/yum.repos.d/docker-ee.repo
    24 
    25 # 将 [docker-ce-test] 下方的 enabled=0 修改为 enabled=1
    26 
    27 #
    28 
    29 # 安装指定版本的Docker-CE:
    30 
    31 # Step 1: 查找Docker-CE的版本:
    32 
    33 # yum list docker-ce.x86_64 --showduplicates | sort -r
    34 
    35 # Loading mirror speeds from cached hostfile
    36 
    37 # Loaded plugins: branch, fastestmirror, langpacks
    38 
    39 # docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
    40 
    41 # docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable
    42 
    43 # docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
    44 
    45 # Available Packages
    46 
    47 # Step2 : 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos)
    48 
    49 # sudo yum -y install docker-ce-[VERSION]


    新版的 Docker 使用 /etc/docker/daemon.json(Linux) 或者 %programdata%dockerconfigdaemon.json(Windows) 来配置 Daemon。

    请在该配置文件中加入(没有该文件的话,请先建一个):

    {"registry-mirrors": ["https://h2htys9o.mirror.aliyuncs.com", "https://docker.mirrors.ustc.edu.cn"]}

    2.Docker常用命令

    这里给出一篇文档:https://doc.yonyoucloud.com/doc/docker_practice/image/create.html

    列举一些常用命令

    构建,运行,停止容器

    docker build -it test_tag .       //-it是为了给容易命名新的tag,.为dockerfile的路径

    docker run -itdp 80:80 test_tag  //-it是容器的tag,-d是后台运行,-p是端口映射

    docker stop 容器id

    进入容器

    docker exec -it 容器id /bin/bash //这里使用/bin/bash进入容器

    docker exec -it 容器id sh            // /bin/bash用不了的情况可以使用该命令进入容器

    删除容器或镜像

    docker rm 容器id

    docker rmi 镜像id

    docker stop `docker ps -aq`       //关闭所有容器

    docker rm `docker ps -aq`          //删除所有容器

    docker rmi `docker images`   //删除所有镜像,只有先执行前两个命令才可用第三个命令,不然会报错

    重命名镜像

    docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签)

    查看容器ip

    docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-ID>

    docker inspect <container id>

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

    3.常用Dockerfile案例

    (1)Ubuntu14:04+lamp

     1 FROM ubuntu:14.04
     2 
     3 RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
     4 RUN sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
     5 
     6 ENV TZ=Asia/Shanghai
     7 RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
     8 
     9 # Update sources
    10 RUN apt-get update -y
    11 
    12 # install http
    13 RUN apt-get install -y apache2
    14 RUN mkdir -p /var/lock/apache2 /var/run/apache2
    15 RUN sed -i '$aServerName 127.0.0.1'  /etc/apache2/apache2.conf
    16 
    17 RUN apt-get install -y mysql-client mysql-server
    18 RUN apt-get install -y mysql-client mysql-server
    19 RUN find /var/lib/mysql -type f -exec touch {} ; && service mysql start && &&
    20 mysqladmin -u root password root &&
    21  mysql -u root -proot -e "create database mydata;" &&
    22     mysql -u root -proot mydata < /etc/mydata.sql   
    23 
    24 RUN apt-get install -y software-properties-common
    25 RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
    26 RUN apt-get update && apt-get install -y --no-install-recommends 
    27         libapache2-mod-php5.6 
    28         php5.6 
    29         php5.6-cli 
    30         php5.6-curl 
    31         php5.6-dev 
    32         php5.6-gd 
    33         php5.6-imap 
    34         php5.6-mysql 
    35     && apt-get clean 
    36     && rm -fr /var/lib/apt/lists/*
    37 
    38 COPY ./start.sh /start.sh
    39 RUN chmod +x /start.sh
    40 EXPOSE 80 
    41 
    42 CMD ["/start.sh"]

    start.sh

    #!/bin/bash
    
    /etc/init.d/apache2 restart
    find /var/lib/mysql -type f -exec touch {} ; && service mysql start 
    /usr/bin/tail -f /dev/null

     全部放到一个目录执行即可

    sudo docker build -t="lamp" .
    sudo docker run -d -p 80:80 -it lamp /start.sh

     (2)Ubuntu16:04+lamp 

     1 FROM ubuntu:16.04
     2  
     3 ENV DEBIAN_FRONTEND noninteractive
     4 RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
     5 RUN sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
     6 
     7 RUN apt-get update
     8 
     9 
    10 # install http
    11 RUN apt-get install -y apache2
    12 RUN mkdir -p /var/lock/apache2 /var/run/apache2
    13 RUN sed -i '$aServerName 127.0.0.1'  /etc/apache2/apache2.conf
    14 
    15 #install mysql
    16 COPY ./db.sql /db.sql
    17 RUN apt-get install -y mysql-server && 
    18 chown -R mysql:mysql /var/lib/mysql &&
    19 find /var/lib/mysql -type f -exec touch {} ; && service mysql start &&
    20 mysqladmin -u root password "password" &&
    21     mysql -u root -ppassword -e "create database test;" &&
    22     mysql -u root -ppassword test < /db.sql
    23 
    24 RUN apt-get install -y software-properties-common
    25 RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
    26 RUN apt-get update && apt-get install -y --no-install-recommends 
    27         libapache2-mod-php5.6 
    28         php5.6 
    29         php5.6-cli 
    30         php5.6-curl 
    31         php5.6-dev 
    32         php5.6-gd 
    33         php5.6-imap 
    34         php5.6-mysql 
    35     && apt-get clean 
    36     && rm -fr /var/lib/apt/lists/*
    37 
    38 ADD  ./web.tar.gz  /var/www/html/
    39 RUN chmod +x /root/start.sh
    40 
    41 EXPOSE 80
    42 ENTRYPOINT cd /root ; ./start.sh

    start.sh

    #!/bin/bash
    
    /etc/init.d/apache2 restart
    find /var/lib/mysql -type f -exec touch {} ; && service mysql start 
    /usr/bin/tail -f /dev/null #或者/bin/bash

    (3)ubuntu16.04+lnmp

     1 FROM  ubuntu:16.04
     2 
     3 RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
     4 RUN sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
     5 
     6 # Update sources
     7 RUN apt-get update -y
     8 
     9 RUN apt-get install -y  bash-completion unzip build-essential gcc g++ autoconf libiconv-hook-dev wget
    10 
    11 # nginx php
    12 RUN apt-get install -y nginx php7.0-fpm  
    13 RUN rm -rf /var/lib/apt/lists/*
    14 
    15 COPY ./default /etc/nginx/sites-available/default
    16 COPY ./src/index.php /usr/share/nginx/html/index.php
    17 
    18 RUN chown -R www-data:www-data /usr/share/nginx/html 
    19     && ln -s /usr/share/nginx/html /html
    20 COPY ./start.sh /start.sh
    21 RUN chmod a+x /start.sh
    22 
    23 EXPOSE 80 
    24 CMD ["/start.sh"]

    start.sh

    1 #!/bin/bash
    2 
    3 
    4 service nginx restart
    5 service php7.0-fpm start
    6 find /var/lib/mysql -type f -exec touch {} ; && service mysql start 7 
    8 
    9 /usr/bin/tail -f /dev/null
  • 相关阅读:
    Enterprise Library 3.0 – April 2007 Released
    Static methods can not be called remotely
    BizTalk: 提高 BizTalk 编程能力的 8 点技巧和窍门(MSDN Magazine)
    Calling Stored Procedures Using the SQL Adapter
    BizTalk: Difference between (PassThruReceive, PassThruSend) and (XmlReceive, XmlSend) Biztalk Pipelines (For Beginners)
    BizTalk Exception: Cannot access a disposed object && Failed to serialize the message part "BankQuoteRequest" into the type "BankQuoteRequest"
    Oneway web service call in BizTalk Orchestration
    Create route in crossserver SSB
    BizTalk: PublisherSubscriber model and Binding
    BizTalk Exception: Calling web services with Mixed OneWay and SolicitResponse Operations
  • 原文地址:https://www.cnblogs.com/kagari/p/9286555.html
Copyright © 2020-2023  润新知