Deploy a docker container running Apache (HTTP) with Ansible

Source: Internet
Author: User
Tags http 2 docker ps docker run saltstack

In the field of automation operations, in addition to Saltstack, there are ansible this batch installation Deployment tool, in writing the specific content first, first of all I used two tools after the sentiment.

Saltstack is the C/s framework, in order to install software in the client, and start the service to manage, ansible is connected via SSH to the client, that is, the key must be passed to the client to be managed, although the authentication method can be turned off, but the production environment must not be shut down, From this point of view, the ansible is easier to configure, and it is entirely possible to iterate through all the hosts with a script to add the encryption key.

In terms of Use, command complexity is similar, it should be easier to understand that saltstack, command is basically a combination of English, Ansible is based on a variety of patterns, parameters to perform management.

Script programming aspect, the programming complexity aspect is different, Ansible provides the group way to provide the management to the multi-host, when pushes the operation to the client needs to give the explicit execution file (YML), does not have the clear stipulation to the file storage location, Saltstack need to provide the host information on the command line, When you push a file (SLS) in the directory provided in the configuration file, the pushed file must also be in this directory.

These are some of my personal views, the following start the actual operation

Environment Introduction:

Centos 6.5

Ansible 2.3.0

Docker 1.7.1

Http 2.4.6-67


One. Docker operations

1.docker Installation

This section skips, if you do not know about Docker installation and some common commands, please refer to "Docker Common commands and actions"

2. Pull Ansible image

Docker Pull Ansible/centos7-ansible #推荐使用这个镜像

After a successful pull, you can see this image in images Richard

650) this.width=650; "Src=" Https://s3.51cto.com/oss/201711/02/c377af828d2f30e421712f6107f5e281.png-wh_500x0-wm_3 -wmp_4-s_2887752629.png "title=" D1 "alt=" C377af828d2f30e421712f6107f5e281.png-wh_ "/>

3. Run the image to open a container

Docker run-it--name ansible 6883

4. Modify the configuration file

Vi/etc/ansible/hosts

[Local]\nlocalhost\n >>>>>

[Local]

localhost

These two carriage returns are not escaped at run time and will be error-

5. Submit the Modified Container

Docker Commit 1277 Ansiblev2

6. Write the image building method

Vim Dockerfile

from Ansiblev2maintainer bin [email protected]workdir/opt/ansible #定义工作区RUN mkdir/opt/ansible/files #创建必须目录RUN MKD Ir/opt/ansible/templateadd httpd.conf/opt/ansible/files #把需要的文件复制到指定位置ADD index.html.j2/opt/ansible/template ADD  Web.yml/opt/ansible/run ansible-playbook/opt/ansible/web.yml-c local #运行ansible进行自动部署VOLUME/var/www/html #定义数据卷CMD ["/USR/SBIN/HTTPD", "-X"] #让httpd服务在前台运行EXPOSE #开放80端口, the httpd.conf configuration file must also be 80 ports

Two. Ansible operation

1. Writing a service deployment method

Vim Web.yml #ansible和saltstack在文字格式方面要求同样严格, if the error, please check the format is correct!

- name: configure webserver with http         #在运行时的提示信息   hosts: local         #对本地主机组进行操作    sudo: true         #运行时切换用户   tasks:              #建立任务     - name: install  http      yum: name=httpd update_cache=yes      #安装httpd, update cache     - name: copy http config file       copy: src=files/httpd.conf dest=/etc/httpd/conf     #文件复制     - name: enable configuration      file:  >         #竖版写法, establishing a soft connection           dest=/etc/httpd/configure         src=/etc/httpd/conf/          state=link    - name: copy index.html      #拷贝主页文件       template: src=template/index.html.j2  dest=/var/www/html/index.html mode=0644

2.index.html.j2 's Content

Three. Build the image and start the container

1. Build the image

Current directory structure

650) this.width=650; "Src=" Https://s2.51cto.com/oss/201711/02/29ff6b324870a0f86c3ea73276e47769.png-wh_500x0-wm_3 -wmp_4-s_2253792231.png "title=" D2 "alt=" 29ff6b324870a0f86c3ea73276e47769.png-wh_ "/>

Docker build-t ansible/httpd.

2. Start the container

Docker run-d-P 8000:80--name httpd ansible/httpd #把容器的80端口映射到本地的8000端口

3. View the operation of the container

Docker Ps-a

650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/02/f760e6540716cacf2d5e1b34751d3ac7.png-wh_500x0-wm_3 -wmp_4-s_3374539356.png "title=" D3 "alt=" F760e6540716cacf2d5e1b34751d3ac7.png-wh_ "/>

4. Because the data volume is specified when the image is built, the data volume can be mounted when the container is started, which facilitates the code modification.

Docker run-d-P 8001:80-v/var/www/html:/var/www/html--name HTTP2 ansible/httpd

Four. Testing the home page

http://192.168.6.10:8000

650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/02/10081d6d6171b340575d4888f9945c5c.png-wh_500x0-wm_3 -wmp_4-s_2237130030.png "title=" D4 "alt=" 10081d6d6171b340575d4888f9945c5c.png-wh_ "/>

Appears as shown on the deployment as normal

This article is from "Linux Operations Technology" blog, please be sure to keep this source http://forall.blog.51cto.com/12356505/1978507

Deploy a docker container running Apache (HTTP) with Ansible

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.