1. 程式人生 > >阿里雲ECS+CentOS 7.0+Docker+Redmine環境搭建

阿里雲ECS+CentOS 7.0+Docker+Redmine環境搭建

前言

  搭建Redmine環境可以選擇使用分別安裝Ruby+Rails+Redmine+MySQL的方式,

  但是過程中需要解決各種擾人的依賴問題。Docker為我們提供的Plan B,方便,快捷。

  

安裝環境

  當然可以選擇Ruby+Rails+Redmine+MySQL的安裝方式,請參考:

阿里雲ECS安裝

  因為是實際生產需要,所以,直接購買阿里雲的ECS

  安裝和使用方法參考:

安裝CentOS 7.0

  Docker官方的說明:

    “系統的要求跟 Ubuntu 情況類似,64 位作業系統,核心版本至少為 3.10。

    Docker 目前支援 CentOS 6.5 及以後的版本,推薦使用 CentOS 7 系統。”

  所以購買ECS後,請選擇CentOS 7系統。

CentOS 7核心升級

  更新核心兩種方法,編譯安裝和非編譯安裝:

  編譯安裝

  非編譯安裝

  我選擇的是非編譯安裝方式,很方便。

  升級流程

    1,檢視核心版本

      uname -r

      3.8.0-44-generic

    2匯入 Public Key

    3安裝 ELRepo

    4升級 Kernel

      yum --enablerepo=elrepo-kernel install kernel-lt -y

    5更改 Grub

      vim/etc/grub.conf

  根據安裝好以後的核心位置,修改 default 的值,一般是修改為0,因為 default 從 0 開始,一般新安裝的核心在第一個位置,

  所以設定default=0。就是檔案最上面的那個default

    # grub.conf generated by anaconda

    #

    # Note that you do not have to rerun grub after making changes to this file

    # NOTICE:  You do not have a /boot partition.  This means that

    #          all kernel and initrd paths are relative to /, eg.

    #          root (hd0,0)

    #          kernel /boot/vmlinuz-version ro root=/dev/sda1

    #          initrd /boot/initrd-[generic-]version.img

    #boot=/dev/sda

    default=0

    timeout=5

    splashimage=(hd0,0)/boot/grub/splash.xpm.gz

    hiddenmenu

    title CentOS (3.10.28-1.el6.elrepo.x86_64)

        root (hd0,0)

        kernel /boot/vmlinuz-3.10.28-1.el6.elrepo.x86_64 ro root=UUID=0a05411f-16f2-4d69-beb0-2db4cefd3613 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=en_US.UTF-8

  6,重啟CentOS

    init 6

  7,檢視核心版本

    uname -r

    3.10.0-327.18.2.el7.x86_64

  8,核心升級完成

安裝Docker

  1,新增 yum 軟體源

    sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'

    [dockerrepo]

    name=Docker Repository

    baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/

    enabled=1

    gpgcheck=1

    gpgkey=https://yum.dockerproject.org/gpg

    EOF

  2,更新 yum 軟體源快取

    yum update

  3,安裝 docker-engine

    yum install -y docker-engine

  4,配置讓 Docker 服務在系統啟動後自動啟動。

    CentOS 7中支援的新式 systemd 語法,如下:

    systemctl  start docker.service

    systemctl  enable docker.service

    另附舊語法,如下:

    service docker start

    chkconfig docker on

  5Docker安裝完成

  讓我們測試一下,Docker是否安裝成功:

  1,檢視Docker版本

    # docker version

    Client:

     Version:         1.9.1

     API version:     1.21

     Package version: docker-common-1.9.1-40.el7.centos.x86_64

     Go version:      go1.4.2

     Git commit:      ab77bde/1.9.1

     Built:          

     OS/Arch:         linux/amd64

    Server:

     Version:         1.9.1

     API version:     1.21

     Package version: docker-common-1.9.1-40.el7.centos.x86_64

     Go version:      go1.4.2

     Git commit:      ab77bde/1.9.1

     Built:          

     OS/Arch:         linux/amd64

  2,輸入Docker然後回車,可以列印Docker的所有命令

    # docker

    Usage: docker [OPTIONS] COMMAND [arg...]

       docker daemon [ --help | ... ]

       docker [ --help | -v | --version ]

    A self-sufficient runtime for containers.

    Options:

      --config=~/.docker                 Location of client config files

      -D, --debug=false                  Enable debug mode

      --disable-legacy-registry=false    Do not contact legacy registries

      -H, --host=[]                      Daemon socket(s) to connect to

      -h, --help=false                   Print usage

      -l, --log-level=info               Set the logging level

      --tls=false                        Use TLS; implied by --tlsverify

      --tlscacert=~/.docker/ca.pem       Trust certs signed only by this CA

      --tlscert=~/.docker/cert.pem       Path to TLS certificate file

      --tlskey=~/.docker/key.pem         Path to TLS key file

      --tlsverify=false                  Use TLS and verify the remote

      -v, --version=false                Print version information and quit

    Commands:

        attach    Attach to a running container

        build     Build an image from a Dockerfile

        commit    Create a new image from a container's changes

        cp        Copy files/folders between a container and the local filesystem

        create    Create a new container

        diff      Inspect changes on a container's filesystem

        events    Get real time events from the server

        exec      Run a command in a running container

        export    Export a container's filesystem as a tar archive

        history   Show the history of an image

        images    List images

        import    Import the contents from a tarball to create a filesystem image

        info      Display system-wide information

        inspect   Return low-level information on a container or image

        kill      Kill a running container

        load      Load an image from a tar archive or STDIN

        login     Register or log in to a Docker registry

        logout    Log out from a Docker registry

        logs      Fetch the logs of a container

        network   Manage Docker networks

        pause     Pause all processes within a container

        port      List port mappings or a specific mapping for the CONTAINER

        ps        List containers

        pull      Pull an image or a repository from a registry

        push      Push an image or a repository to a registry

        rename    Rename a container

        restart   Restart a container

        rm        Remove one or more containers

        rmi       Remove one or more images

        run       Run a command in a new container

        save      Save an image(s) to a tar archive

        search    Search the Docker Hub for images

        start     Start one or more stopped containers

        stats     Display a live stream of container(s) resource usage statistics

        stop      Stop a running container

        tag       Tag an image into a repository

        top       Display the running processes of a container

        unpause   Unpause all processes within a container

        version   Show the Docker version information

        volume    Manage Docker volumes

        wait      Block until a container stops, then print its exit code

    Run 'docker COMMAND --help' for more information on a command.

  測試成功。

Docker的使用

  三個重要概念

  1image映象

    官方解釋:

      “Docker 映象(Image)就是一個只讀的模板。例如:一個映象可以包含一個完整的 ubuntu 作業系統環境,

      裡面僅安裝了 Apache 或使用者需要的其它應用程式。”

    把image理解為一個超級應用程式,與通常應用程式不同的地方是,它包含了應用程式執行的所有環境,如,包括一個作業系統環境。

  2container容器

    容器是從映象建立的執行例項。它可以被啟動、開始、停止、刪除。每個容器都是相互隔離的、保證安全的平臺。

    由同一映象可以啟動不同的容器(例項),不同的容器由擁有不同的【container ID】【NAMES】等資訊,由Docker維護。

    可以使用命令docker ps檢視正在執行的container;使用docker ps -a檢視已經退出的container。

   1,檢視正在執行的容器

      docker ps

      CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                            NAMES

      43b6756d9a6e        sameersbn/redmine             "/sbin/entrypoint.sh "   11 hours ago        Up 10 hours         443/tcp, 0.0.0.0:10083->80/tcp   redmine

      8f8f4f81321e        sameersbn/postgresql:9.4-21   "/sbin/entrypoint.sh"    11 hours ago        Up 10 hours         5432/tcp                         postgresql-redmine

   2,檢視所有容器

      docker ps -a

      CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                      PORTS                            NAMES

      43b6756d9a6e        sameersbn/redmine             "/sbin/entrypoint.sh "   11 hours ago        Up 10 hours                 443/tcp, 0.0.0.0:10083->80/tcp   redmine

      8f8f4f81321e        sameersbn/postgresql:9.4-21   "/sbin/entrypoint.sh"    11 hours ago        Up 10 hours                 5432/tcp                         postgresql-redmine

      d3fd852f511c        training/webapp               "python app.py"          11 hours ago        Exited (137) 11 hours ago                                    web

      1f0664fda251        training/webapp               "python app.py"          11 hours ago        Exited (137) 11 hours ago                                    thirsty_heisenberg

      e9500a923b5f        training/webapp               "app.py"                 11 hours ago        Created                                                      clever_mirzakhani

      94592c0dad84        training/webapp               "app.py"                 11 hours ago        Created                                                      compassionate_meitner

      d66bdf427b7e        training/webapp               "python app.py"          12 hours ago        Exited (137) 11 hours ago                                    lonely_mcnulty

      5a1a8abdd530        ubuntu:14.04                  "/bin/sh -c 'while tr"   12 hours ago        Exited (0) 12 hours ago

      。。。

  可以利用容器ID的唯一性,實現資料的儲存。如,當你執行中的Remine異常停機,或者伺服器重啟導致Remine停止執行,

  如果是重新通過映象啟動例項的話,原來Redmine的資料就丟失了。此時,可以通過,查詢所有容器,並通過Docker start [CONTAINER ID]重新啟動容器即可,

  資料不會丟失。操作方法,後面詳細說明。

  另一種方法是,使用docker commit命令把被停止的容器製作成映象,然後建立改映象的容器,也可以保證資料不丟失。

  3,Repository倉庫

    存放映象的地方。最大的公開倉庫是Docker Hub,存放了數量龐大的映象供使用者下載。

    可以通過命令,docker search [image name]查詢你想要的映象。

Docker常用操作

  1docker search #在倉庫中查詢映象

    檢視docker hubUbuntu的映象。

      docker search ubuntu

      INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED

      docker.io   docker.io/ubuntu                            Ubuntu is a Debian-based Linux operating s...   3995      [OK]      

      docker.io   docker.io/ubuntu-upstart                    Upstart is an event-based replacement for ...   63        [OK]      

      docker.io   docker.io/rastasheep/ubuntu-sshd            Dockerized SSH service, built on top of of...   27                   [OK]

      docker.io   docker.io/torusware/speedus-ubuntu          Always updated official Ubuntu docker imag...   26                   [OK]

      docker.io   docker.io/ubuntu-debootstrap                debootstrap --variant=minbase --components...   24        [OK]      

      docker.io   docker.io/nickistre/ubuntu-lamp             LAMP server on Ubuntu                           7  

  2docker pull #下載指定映象

    使用查詢結果中的映象名稱,下載指定的Ubuntu映象,當然選擇星星數最多的。

    docker pull docker.io/ubuntu

    Using default tag: latest

    Trying to pull repository docker.io/library/ubuntu ...

  3docker images #列出本地的所有映象

    docker images

    REPOSITORY                       TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

    docker.io/ubuntu                 14.04               0e7d4a488bcc        34 hours ago        187.9 MB

    docker.io/sameersbn/redmine      latest              2d92be2b784a        3 weeks ago         653.4 MB

    docker.io/sameersbn/postgresql   latest              460997f40d74        3 weeks ago         231.3 MB

    docker.io/sameersbn/postgresql   9.4-21              6aa1daa9f8ce        3 weeks ago         231.3 MB

    docker.io/training/webapp        latest              54bb4e8718e8        12 months ago       348.7 MB

  4dockerrmi #刪除映象

  5dockerps #列出當前執行中的容器

    docker ps

    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                            NAMES

    43b6756d9a6e        sameersbn/redmine             "/sbin/entrypoint.sh "   11 hours ago        Up 10 hours         443/tcp, 0.0.0.0:10083->80/tcp   redmine

    8f8f4f81321e        sameersbn/postgresql:9.4-21   "/sbin/entrypoint.sh"    11 hours ago        Up 10 hours         5432/tcp                         postgresql-redmine

  6docker ps -a #列出所有容器,包換已退出的容器

    docker ps -a

    CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                      PORTS                            NAMES

    43b6756d9a6e        sameersbn/redmine             "/sbin/entrypoint.sh "   11 hours ago        Up 10 hours                 443/tcp, 0.0.0.0:10083->80/tcp   redmine

    8f8f4f81321e        sameersbn/postgresql:9.4-21   "/sbin/entrypoint.sh"    11 hours ago        Up 11 hours                 5432/tcp                         postgresql-redmine

    d3fd852f511c        training/webapp               "python app.py"          12 hours ago        Exited (137) 12 hours ago                                    web

    1f0664fda251        training/webapp               "python app.py"          12 hours ago        Exited (137) 12 hours ago                                    thirsty_heisenberg

    e9500a923b5f        training/webapp               "app.py"                 12 hours ago        Created                                                      clever_mirzakhani

    94592c0dad84        training/webapp               "app.py"                 12 hours ago        Created                                                      compassionate_meitner

    d66bdf427b7e        training/webapp               "python app.py"          12 hours ago        Exited (137) 12 hours ago                                    lonely_mcnulty

    5a1a8abdd530        ubuntu:14.04                  "/bin/sh -c 'while tr"   12 hours ago        Exited (0) 12 hours ago

  7docker rm#刪除容器,注意加docker rm -f強制刪除容器,可以刪除執行中的容器

  8dockerrun #使用映象啟動容器,其中,-t 選項讓Docker分配一個偽終端(pseudo-tty)並繫結到容器的標準輸入上, -i 則讓容器的標準輸入保持開啟;-d 選項來實現後天執行;

    docker run -t -i ubuntu:14.04 /bin/echo 'Hello World'

    Hello World

    此外,如果run一個本地沒有的imagedocker會先從倉庫中下載(pull)該映象,再執行run命令。

  9docker stop#終止一個執行中的容器。

    當Docker容器中指定的應用終結時,容器也自動終止。 例如對於上一章節中只啟動了一個終端的容器,

    使用者通過 exit 命令或 Ctrl+d 來退出終端時,所建立的容器立刻終止。

  10docker start #重新啟動終止狀態的容器

    docker restart 命令會將一個執行態的容器終止,然後再重新啟動它。

安裝MySQL

  redmine需要使用資料庫,所以需要先安裝MySQL

  1,查詢mysql

    docker search mysql

    INDEX       NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED

    docker.io   docker.io/mysql                        MySQL is a widely used, open-source relati...   2403      [OK]      

    docker.io   docker.io/mysql/mysql-server           Optimized MySQL Server Docker images. Crea...   147                  [OK]

    docker.io   docker.io/centurylink/mysql            Image containing mysql. Optimized to be li...   45                   [OK]

    docker.io   docker.io/sameersbn/mysql                                                              35

2,下載指定映象

docker pull docker.io/sameersbn/mysql

安裝Redmine

1,查詢Redmine

docker search redmine

INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED

docker.io   docker.io/sameersbn/redmine                                                                  199                  [OK]

docker.io   docker.io/redmine                            Redmine is a flexible project management w...   190       [OK]      

docker.io   docker.io/bitnami/redmine                    Bitnami Docker Image for Redmine                7                    [OK]

docker.io   docker.io/74th/redmine-all-in-one            Redmine includes hosting SVN & Git , backl...   5 

2,下載Redmine

docker pull docker.io/sameersbn/redmine

啟動Redmine

1,啟動MySQL

docker run --name=postgresql-redmine-d\

  --env='DB_NAME=redmine_production' \

  --env='DB_USER=redmine' --env='DB_PASS=password' \

  --volume=/srv/docker/redmine/postgresql:/var/lib/postgresql \

  sameersbn/postgresql:9.4-21

redmine提供的資料庫容器名稱為postgresql-redmine,-d選項,指明資料庫後天執行

2,啟動Redmine

docker run --name=redmine -d\

  --link=postgresql-redmine:postgresql --publish=10083:80 \

  --env='REDMINE_PORT=10083' \

  --volume=/srv/docker/redmine/redmine:/home/redmine/data \

  sameersbn/redmine

redmine容器的名稱為redmine-d選項,指明資料庫後天執行;埠號為10083

伺服器重啟後,redmine和資料庫都停止了,需要重新啟動,此時,

只要找到上次停止的container名字,重新開啟即可。這樣可以保障資料不丟失。

查詢停止前的container name

docker ps -a

CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                           PORTS                            NAMES

43b6756d9a6e        sameersbn/redmine             "/sbin/entrypoint.sh "   29 minutes ago      Up 3 minutes                     443/tcp, 0.0.0.0:10083->80/tcp   redmine

8f8f4f81321e        sameersbn/postgresql:9.4-21   "/sbin/entrypoint.sh"    33 minutes ago      Up 5 minutes                     5432/tcp                         postgresql-redmine

重新開啟資料庫(使用名稱開啟)

docker start postgresql-redmine

postgresql-redmine

重新開啟redmine

docker start redmine

redmine

3,檢視當前容器執行狀態

docker ps

CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                            NAMES

43b6756d9a6e        sameersbn/redmine             "/sbin/entrypoint.sh "   11 hours ago        Up 11 hours         443/tcp, 0.0.0.0:10083->80/tcp   redmine

8f8f4f81321e        sameersbn/postgresql:9.4-21   "/sbin/entrypoint.sh"    11 hours ago        Up 11 hours         5432/tcp                         postgresql-redmine

4,安裝完成,登入伺服器,檢視是否正常執行,注意埠號為10083

首次登入,

使用者名稱:admin

密碼:admin

 

登入後,選擇我的賬戶,修改語言為[簡體中文]

 

安裝完成。

 

參考文章

Docker從入門到實踐

阿里雲Ubuntu環境搭建Docker服務

Docker實踐

sameersbn/redmine

使用Yum快速更新升級CentOS核心

使用yum快速升級CentOS 6.5核心到 3.10.28

CentOS 6.5搭建Redmine

ECS雲伺服器零基礎教程linux版

CentOS 6.x 核心升級(2.6.32 -> 3.10.58)過程記錄