docker-compse实践一

docker-compose的第一次实践,首先是要现有docker-compose组件,(CentOS7)安装pip,之后pip install docker-compose。当然为了提高安装速度,使用国内源加速,我使用清华源https://mirrors.tuna.tsinghua.edu.cn/help/pypi/,具体方式是:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

安装完之后编写docker-compose.yml文件,注意缩进问题。第一次实践内容是安装wordpress,具体docker-compose.yml文件内容是:

version: '3.4'

services:
    mysql:
        image: mysql:5.7
        hostname: mysql
        volumes:
            - ./mysql_data:/var/lib/mysql:rw
            - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro
    
        networks:
            wordpress:
                aliases:
                    - mysql

        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: wordpress
            MYSQL_USER: wordpress
            MYSQL_PASSWORD: mywordpress
    
        logging:
            driver: json-file
            options:
                max-file: "3" 
                max-size: "100m"

    redis:
        image: redis:4.0.6
        hostname: redis
        volumes:
            - ./redis:/data:rw
            - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro

        networks:
            wordpress:
                aliases:
                     - redis

        logging:
             driver: json-file
             options:
                 max-file: "3"
                 max-size: "100m"

    wordpress:
        image: wordpress:4.9.1-php5.6-apache
        hostname: wordpress
        depends_on:
            - redis
            - mysql
        ports:
            - "4000:80/tcp"
        environment:
            WORDPRESS_DB_HOST: mysql
            WORDPRESS_DB_USER: wordpress
            WORDPRESS_DB_PASSWORD: mywordpress
            WORDPRESS_TABLE_PREFIX: wp_
        networks:
            wordpress:
                aliases:
                    - wordpress
        logging:
           driver: json-file
           options:
               max-file: '3'
               max-size: 100m


networks:
    wordpress:
        external: true

编写好docker-compose文件之后,使用

docker-compose up -d

就可以运行整个单机的服务了。

测试的时候environment后面的变量在wordpress这里因为加了-出现了错误,看来应该是和上面的mysql的environment冲突,要使用一致的格式。

简要介绍几个指令:

image是确定使用的镜像,volumes是挂载的内容,networks是使用的网络,logging是日志文件类型,最下面的networks是给出上面使用的网络,external是允许外部服务加入这个网络。

当然第一次运行时错误的,因为默认没有wordpress这个网络,需要自己创建这个网络,使用命令:

docker network create wordpress

这样就可以运行了。对了,别忘记修改自己的docker镜像点,毕竟有个伟大的墙,我使用的是阿里云的镜像加速,提高自己的下载速度。

Pulling redis (redis:4.0.6)...
4.0.6: Pulling from library/redis
c4bb02b17bb4: Pull complete
58638acf67c5: Pull complete
f98d108cc38b: Pull complete
83be14fccb07: Pull complete
5d5f41793421: Pull complete
ed89ff0d9eb2: Pull complete
Digest: sha256:0e773022cd6572a5153e5013afced0f7191652d3cdf9b1c6785eb13f6b2974b1
Status: Downloaded newer image for redis:4.0.6
Pulling mysql (mysql:5.7)...
5.7: Pulling from library/mysql
f49cf87b52c1: Pull complete
78032de49d65: Pull complete
837546b20bc4: Pull complete
9b8316af6cc6: Pull complete
1056cf29b9f1: Pull complete
86f3913b029a: Pull complete
4cbbfc9aebab: Pull complete
8ffd0352f6a8: Pull complete
45d90f823f97: Pull complete
ca2a791aeb35: Pull complete
Digest: sha256:1f95a2ba07ea2ee2800ec8ce3b5370ed4754b0a71d9d11c0c35c934e9708dcf1
Status: Downloaded newer image for mysql:5.7
Pulling wordpress (wordpress:4.9.1-php5.6-apache)...
4.9.1-php5.6-apache: Pulling from library/wordpress
f49cf87b52c1: Already exists
185616061386: Pull complete
5fc132db2e0d: Pull complete
00c1c323341a: Pull complete
ff3701349211: Pull complete
faab1d6ad70f: Pull complete
eae8d88d75e0: Pull complete
7fbd4c732645: Pull complete
b2e59bdd1208: Pull complete
62fe0c2fe033: Pull complete
2c46dbb5c363: Pull complete
8717ca404f3f: Pull complete
a085634ab1c7: Pull complete
15bfe1e101b9: Pull complete
e44c878b275e: Pull complete
2f228666e90a: Pull complete
115c35aad3d5: Pull complete
20f396b1b206: Pull complete
abf62418bd1f: Pull complete
Digest: sha256:11092f98d6967c897f310907aeda8da1619d2cd2e4d538913b8f3d707f76fbf1
Creating dockertest_redis_1     ... done
Creating dockertest_mysql_1     ... done
Creating dockertest_mysql_1     ... 
Creating dockertest_wordpress_1 ... done

创建完成之后使用

docker ps -a

查看运行的服务。

之后在自己的浏览器查看你的4000端口就可以了。

docker-compse实践一_第1张图片

不过现在刚学docker,以后运行之后的数据库内容咋整,wordpress怎么把源文件搞出来以便下次的移植是个问题。

你可能感兴趣的:(docker)