2016-09-09 65 views
1

我想要使用gitlab跑步者部署成功構建的docker鏡像,但我不確定如何使用.gitlab-ci.yml中的部署階段來執行此操作。構建日誌顯示在構建過程中數據庫在Docker鏡像上正確創建。在gitlab跑步者完成構建之後部署

我在Mac上本地使用docker(OSX 10.11.6)來構建我的docker容器。 Gitlab正在遠程運行。我註冊了一個特定的本地運動員來處理構建。當我將更改推送到我的項目時,gitlab CI將運行構建腳本來創建測試數據庫。圖像生成後會發生什麼?在本地機器上列出的已完成的版本沒有碼頭圖像。 gitlab-runner-prebuilt-x86_64是一個準系統的linux映像,與構建不相關。

https://docs.gitlab.com/ce/ci/docker/using_docker_build.html

http://container-solutions.com/running-docker-in-jenkins-in-docker/

>gitlab-ci-multi-runner list 
Listing configured runners       ConfigFile=/Users/username/.gitlab-runner/config.toml 
local-docker-executor        Executor=docker Token=[token] URL=http://gitlab.url/ci 

>docker images 
REPOSITORY      TAG     IMAGE ID   CREATED    SIZE 
gitlab-runner-prebuilt-x86_64 f6fdece    [id1]  25 hours ago  50.87 MB 
php7       latest    [id2]  26 hours ago  881.8 MB 
ubuntu       latest    [id3]  13 days ago   126.6 MB 
docker       latest    [id4]  2 weeks ago   104.9 MB 

.gitlab-ci.yml:

image: php7:latest 

# build_image: 
# script: 
#  - docker build -t php7 . 

# Define commands that run before each job's script 
# before_script: 
# - docker info 

# Define build stages 
    # First, all jobs of build are executed in parallel. 
    # If all jobs of build succeed, the test jobs are executed in parallel. 
    # If all jobs of test succeed, the deploy jobs are executed in parallel. 
    # If all jobs of deploy succeed, the commit is marked as success. 
    # If any of the previous jobs fails, the commit is marked as failed and no jobs of further stage are executed. 
stages: 
    - build 
    - test 
    - deploy  

variables: 
    db_name: db_test 
    db_schema: "db_test_schema.sql" 

build_job1: 
    stage: build 
    script: 
    - service mysql start 
    - echo "create database $db_name" | mysql -u root 
    - mysql -u root $db_name < $db_schema 
    - mysql -u root -e "show databases; use $db_name; show tables;" 
    #- echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');" | mysql -u root 
    #- echo "run unit test command here" 
    #Defines a list of tags which are used to select Runner 
    tags: 
    - docker 

deploy_job1: 
    stage: deploy 
    #this script is run inside the docker container 
    script: 
    - whoami 
    - pwd 
    - ls -la 
    - ls/
    #Usage: docker push [OPTIONS] NAME[:TAG] 
    #Push an image or a repository to a registry 
    - docker push deploy:latest 
    #gitlab runners will look for and run jobs with these tags 
    tags: 
    - docker 

config.toml

concurrent = 1 
check_interval = 0 

[[runners]] 
    name = "local-docker-executor" 
    url = "http://gitlab.url/ci" 
    token = "[token]" 
    executor = "docker" 
    builds_dir = "/Users/username/DOCKER_BUILD_DIR" 
    [runners.docker] 
    tls_verify = false 
    image = "ubuntu:latest" 
    privileged = false 
    disable_cache = false 
    volumes = ["/cache"] 
    [runners.cache] 

Dockerfile

FROM ubuntu:latest 

#https://github.com/sameersbn/docker-mysql/blob/master/Dockerfile 
ENV DEBIAN_FRONTEND noninteractive          
ENV MYSQL_USER mysql              
ENV MYSQL_DATA_DIR /var/lib/mysql          
ENV MYSQL_RUN_DIR /run/mysqld           
ENV MYSQL_LOG_DIR /var/log/mysql           
ENV DB_NAME "db_test"            
ENV DB_IMPORT "db_test_schema.sql"         

# RUN apt-get update && \ 
#  apt-get -y install sudo 
# RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo 
# USER docker 
# CMD /bin/bash 

RUN apt-get update              \ 
&& DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server  
# \ 
# && rm -rf ${MYSQL_DATA_DIR}           \ 
# && rm -rf /var/lib/apt/lists/* 

ADD ${DB_IMPORT} /tmp/${DB_IMPORT} 


# #RUN /usr/bin/sudo service mysql start             \ 
# RUN service mysql start             \ 
# && mysql -u root -e "CREATE DATABASE $DB_NAME"       \ 
# && mysql -u root $DB_NAME < /tmp/$DB_IMPORT 

RUN locale-gen en_US.UTF-8 \ 
    && export LANG=en_US.UTF-8 \ 
    && apt-get update \ 
    && apt-get -y install apache2 libapache2-mod-php7.0 php7.0 php7.0-cli php-xdebug php7.0-mbstring php7.0-mysql php-memcached php-pear php7.0-dev php7.0-json vim git-core libssl-dev libsslcommon2-dev openssl libssl-dev \ 
    && a2enmod headers 

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 
RUN ln -sf /dev/stdout /var/log/apache2/access.log && \ 
    ln -sf /dev/stderr /var/log/apache2/error.log 
RUN mkdir -p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR 


#VOLUME [ "/var/www/html" ] 
WORKDIR /var/www/html 

EXPOSE 80 3306 

#ENTRYPOINT [ "/usr/sbin/apache2" ] 
#CMD ["-D", "FOREGROUND"] 

#ENTRYPOINT ["/bin/bash"] 
+0

我可能是錯的,但是你的問題與Docker比Gitlabs更相關,對吧? –

+0

我想讓GitLab跑步者部署我的碼頭鏡像。要做到這一點,它看起來像我需要通過套接字或通過在圖像上安裝docker來將docker作爲服務在圖像上運行。 – maogenc

回答

1

您不在CI上構建任何泊塢窗圖像。

您正在使用DockerHub的php7圖像執行所有作業。這包括正在嘗試使用docker二進制推送不在該容器內的圖像(deploy:latest)的作業deploy_job1。另外,我認爲docker二進制文件不包含在php7圖片上。

我猜你想要在Mac上本地生成的圖像,不是嗎?在這種情況下,你需要使用另一個亞軍,其執行者應該是shell。在這種情況下,您將有2名參賽者,一名使用docker運行build_job1作業,另一名參賽者推送本地構建的圖像。但是有一個更好的解決方案可以手動構建Docker鏡像,並且可以讓GitLab CI構建它。

所以,對矯正你的.gitlab-ci.yml(刪除你的意見,並補充地雷解釋):

# Removed global image definition 

stages: 
    - build 
    - test 
    - deploy  

variables: 
    db_name: db_test 
    db_schema: "db_test_schema.sql" 

build_job1: 
    stage: build 
    # Use image ONLY in docker runner 
    image: php7:latest 
    script: 
    - service mysql start 
    - echo "create database $db_name" | mysql -u root 
    - mysql -u root $db_name < $db_schema 
    - mysql -u root -e "show databases; use $db_name; show tables;" 
    # Run on runner with docker executor, this is ok 
    tags: 
    - docker 

deploy_job1: 
    stage: deploy 
    script: 
    # Build the docker image first, and then push it 
    - docker build -t deploy:latest . 
    - docker push deploy:latest 
    # Run on runner with shell executor, set proper tag 
    tags: 
    - docker_builder 

當您註冊新轉輪,設置執行爲shell和標籤docker_builder。我假裝你已經在你的Mac上安裝了docker引擎。

另一方面,這個例子是沒有意義的,至少對我而言。由於容器是短暫的,因此build階段什麼也不做。我想你應該在Dockerfile上做到這一點。

相關問題