用于 nginx、php、mysql、golang 的 Docker-compose

服务器现在正在使用此配置。


version: '3'

services:

    nginx:

        image: nginx:latest

        ports:

            - "80:80"

            - "443:443"

        volumes:

            - ./www:/var/www

        depends_on:

            - php

    php:

        build: ./docker/images/php

        volumes:

            - ./www:/var/www

    mysql:

        image: mysql       

        ports:

            - "3306:3306"

        volumes:

            - ./docker/mysql:/var/lib/mysql

            - ./docker/import:/docker-entrypoint-initdb.d

        environment:

            MYSQL_USER: ${MYSQL_USER}

            MYSQL_PASSWORD: ${MYSQL_PASSWORD}

            MYSQL_DATABASE: ${MYSQL_DATABASE}

            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

用于 php 映像的 Dockerfile


# Main image

FROM php:7.3-fpm


# Update and install modules for php and other

RUN apt-get update && apt-get install -y \      

        libfreetype6-dev \

        libjpeg62-turbo-dev \

        libpng-dev \

    && apt-get install -y wget zip unzip git \

    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \

    && docker-php-ext-install -j$(nproc) gd pdo mysqli pdo_mysql \

    && docker-php-ext-enable mysqli


# Workdir for php

WORKDIR /var/www


# Run container

CMD ["php-fpm"]

我想将golang容器添加到此配置中,以便我可以golang从cron.


问题是:


做这个的最好方式是什么?

如何将其专门添加到docker-compose/Dockerfile并从中运行cron?


莫回无
浏览 169回答 1
1回答

慕姐4208626

== 选项 1:添加docker-compose到crontab==* * * * * cd <project dir>/docker-compose && command docker-compose -f docker-compose.yml run --rm -T -w /go/bin <go_app_container> <go_app_name>== 选项 2:使用cron-docker==这是在 Docker 容器内运行 cron 的 Docker 映像Debian-基于:Dockerfile,start-cronAlpine-基于:Dockerfile,start-cron添加 cron 作业 cron-docker假设您有cron.d包含 cron 脚本的文件夹。您唯一需要做的就是将此文件夹复制到 Docker 映像中:# DockerfileFROM renskiy/cron:debianCOPY cron.d /etc/cron.d然后构建并创建容器:docker build --tag my_cron .docker run --detach --name cron my_cron查看日志要查看日志,请使用 Docker logs命令:docker logs --follow cron您所有的 cron 脚本都应该将日志写入/var/log/cron.log. 否则您将无法使用这种方式查看任何日志。通过参数传递 cron 作业start-cron此外,您可以在创建容器时使用自定义命令通过参数传递任何 cron 作业(为可选用户提供-u/--user选项):docker run --detach --name cron renskiy/cron:debian start-cron --user www-data \&nbsp; &nbsp; "0 1 \\* \\* \\* echo '01:00 AM' &gt;&gt; /var/log/cron.log 2&gt;&1" \&nbsp; &nbsp; "0 0 1 1 \\* echo 'Happy New Year!!' &gt;&gt; /var/log/cron.log 2&gt;&1"环境变量您传递给 Docker 的几乎所有环境变量都将对您的 cron 脚本可见。$SHELL, $PATH, $PWD,$USER等除外。docker run --tty --rm --interactive --env MY_VAR=foo renskiy/cron:debian start-cron \&nbsp; &nbsp; "\\* \\* \\* \\* \\* env &gt;&gt; /var/log/cron.log 2&gt;&1"== 选项 3:cron-docker与docker-compose==一起使用对于您的情况:version: '3'services:&nbsp; &nbsp; nginx:&nbsp; &nbsp; &nbsp; &nbsp; image: nginx:latest&nbsp; &nbsp; &nbsp; &nbsp; ports:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - "80:80"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - "443:443"&nbsp; &nbsp; &nbsp; &nbsp; volumes:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - ./www:/var/www&nbsp; &nbsp; &nbsp; &nbsp; depends_on:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - php&nbsp; &nbsp; php:&nbsp; &nbsp; &nbsp; &nbsp; build: ./docker/images/php&nbsp; &nbsp; &nbsp; &nbsp; volumes:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - ./www:/var/www&nbsp; &nbsp; mysql:&nbsp; &nbsp; &nbsp; &nbsp; image: mysql&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ports:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - "3306:3306"&nbsp; &nbsp; &nbsp; &nbsp; volumes:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - ./docker/mysql:/var/lib/mysql&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - ./docker/import:/docker-entrypoint-initdb.d&nbsp; &nbsp; &nbsp; &nbsp; environment:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MYSQL_USER: ${MYSQL_USER}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MYSQL_PASSWORD: ${MYSQL_PASSWORD}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MYSQL_DATABASE: ${MYSQL_DATABASE}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}&nbsp; &nbsp; cron_go_app:&nbsp; &nbsp; &nbsp; image: renskiy/cron:alpine&nbsp; &nbsp; &nbsp; volumes:&nbsp; &nbsp; &nbsp; &nbsp; - crontabs:/etc/crontabs&nbsp; &nbsp; &nbsp; &nbsp; - my_go_app:/app/my_go_app&nbsp; &nbsp; &nbsp; run: start-cron
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go