安装
# 安装
[root@localhost ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# 执行权限
[root@localhost ~]# sudo chmod +x /usr/local/bin/docker-compose
# 验证
[root@localhost ~]# docker-compose -v
docker-compose version 1.18.0, build 8dd22a9
语法
version: "3.1"
services: # 服务
web: # 服务名
build: # 包含dockerfile的相对目录, 也可以通过context,dockerfile定义
context: # dockerfile目录
dockerfile: # 指定dockerfile文件名
args: # 指定参数
dockerfile: # 自定义dockerfile
image: # 镜像名
command: # 容器内的CMD命令
environment: # docker run --env
volumes: # docker container run -v 数据卷
networks: # docker container run --network 网络
ports: # docker container run -p 宿主机端口:容器端口
tty: # docker container run -t || --tty, 是否支持终端登录,默认false
redis: # 第二个容器名
image:
volumes: # docker volume create 数据卷
networks: # docker network create 网络
使用
dockerfile
vi ./game/app-api/Dockerfile
FROM php:7.3-cli-alpine
# Start as root
USER root
ARG TZ=PRC
RUN apk --update add --no-cache tzdata \
&& ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apk del tzdata
ARG XEDEBUG_INSTALL=true
ARG CHANGE_SOURCE=false
RUN if [ ${CHANGE_SOURCE} = "true" ]; then sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories; fi
RUN apk --update add --no-cache \
&& apk add --no-cache libzip-dev libpng-dev php-gd git $PHPIZE_DEPS curl-dev c-ares-dev \
&& docker-php-ext-install bcmath gd \
&& php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" \
&& php composer-setup.php && mv composer.phar /usr/bin/composer \
; if [ ${XEDEBUG_INSTALL} = "true" ];then \
pecl install xdebug \ && docker-php-ext-enable xdebug \
&& echo 'xdebug.mode=coverage'>>/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ;\
fi \
&& docker-php-ext-install pcntl \
&& docker-php-ext-install opcache \
&& docker-php-ext-install sockets \
&& pecl install redis \ && docker-php-ext-enable redis \
&& pecl install -D 'enable-sockets="yes" enable-openssl="yes" enable-http2="yes" enable-mysqlnd="yes" enable-swoole-json="yes" enable-swoole-curl="yes" enable-cares="yes"' swoole \
&& docker-php-ext-enable swoole \
&& echo "swoole.use_shortname='Off'" >> /usr/local/etc/php/conf.d/docker-php-ext-swoole.ini \
&& cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini \
&& rm -rf /var/cache/apk/* && rm -rf /tmp/*
# Set default work directory
WORKDIR /var/www
vi ./game/activity/Dockerfile
FROM php:8.0.16-cli-alpine3.15
# Start as root
USER root
ARG TZ=PRC
RUN apk --update add --no-cache tzdata \
&& ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apk del tzdata
ARG XEDEBUG_INSTALL=true
ARG CHANGE_SOURCE=false
RUN if [ ${CHANGE_SOURCE} = "true" ]; then sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories; fi
RUN apk --update add --no-cache \
&& apk add --no-cache libzip-dev libpng-dev php-gd git $PHPIZE_DEPS curl-dev c-ares-dev \
&& docker-php-ext-install bcmath gd \
&& php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" \
&& php composer-setup.php && mv composer.phar /usr/bin/composer \
; if [ ${XEDEBUG_INSTALL} = "true" ];then \
pecl install xdebug \ && docker-php-ext-enable xdebug \
&& echo 'xdebug.mode=coverage'>>/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ;\
fi \
&& docker-php-ext-install pcntl \
&& docker-php-ext-install opcache \
&& docker-php-ext-install sockets \
&& pecl install redis \ && docker-php-ext-enable redis \
&& pecl install -D 'enable-sockets="yes" enable-openssl="yes" enable-http2="yes" enable-mysqlnd="yes" enable-swoole-json="yes" enable-swoole-curl="yes" enable-cares="yes"' swoole \
&& docker-php-ext-enable swoole \
&& echo "swoole.use_shortname='Off'" >> /usr/local/etc/php/conf.d/docker-php-ext-swoole.ini \
&& cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini \
&& rm -rf /var/cache/apk/* && rm -rf /tmp/*
# Set default work directory
WORKDIR /var/www
yaml
创建docker-compose配置文件
# vi ./game/docker-compose.yml
version: "3.1"
services:
app: # app
build:
context: ./app
args:
- TZ=PRC #时区
- CHANGE_SOURCE=true #是否修改镜像构建时所用源,true则会修改为aliyun的linux源
- XEDEBUG_INSTALL=false #是否安装xdebug
volumes:
- ./app:/var/www
ports:
- "8000:8000"
command: ['php', '-S', '0.0.0.0:8000', '-t', 'public/']
activity: # 活动
build:
context: ./activity
dockerfile: Dockerfile
args:
- TZ=PRC #时区
- CHANGE_SOURCE=true # true为aliyun的linux源
- XEDEBUG_INSTALL=false #是否安装xdebug
volumes:
- ./activity/:/var/www
ports:
- "9501:9501"
command: ['php', 'bin/leonsw', 'start']
构建镜像
构建
[root@localhost game]# cd game
[root@localhost game]# docker-compose build
生成镜像格式为 目录_应用
[root@localhost game]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
game_activity latest 8a1c85ca7034 About a minute ago 409MB
game_app latest 6751ce02fa9c 3 hours ago 400MB
容器
启动
如果指定镜像,并且本地没有该镜像的时候,会自动拉取镜像, 如果指定了构建参数,并且本地没有该镜像的时候,会自动构建
docker-compose up
会自动检查yml果有修改会自动重建对应的容器, 在yml文件中移除容器,在启动时候可以增加 --remove-orphans
参数,例: docker-compose run -d --remove-orphans
- 创建自定义网桥: 目录_default
- 创建容器: 当前目录_服务名_1
[root@localhost game]# docker-compose up
Creating network "game_default" with the default driver
Creating game_app_1 ... done
Creating game_activity_1 ... done
Attaching to game_activity_1, game_app_1
activity_1 | Interactive shell
activity_1 |
activity_1 | php > app_1 | Interactive shell
app_1 |
game_activity_1 exited with code 0
app_1 | php > game_app_1 exited with code 0
查看状态
[root@localhost game]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------
game_activity_1 docker-php-entrypoint php ... Up 0.0.0.0:9501->9501/tcp,:::9501->9501/tcp
game_app_1 docker-php-entrypoint php ... Up 0.0.0.0:8000->8000/tcp,:::8000->8000/tcp
[root@localhost game]# docker container ls
CONTAINER ID IMAGE PORTS NAMES
41aa5acf393f game_app 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp game_app_1
6e20a111d779 game_activity 0.0.0.0:9501->9501/tcp, :::9501->9501/tcp game_activity_1
停止
[root@localhost game]# docker-compose stop
Stopping game_app_1 ... done
Stopping game_activity_1 ... done
[root@localhost game]# docker container ls -a
CONTAINER ID IMAGE NAMES
41aa5acf393f game_app game_app_1
6e20a111d779 game_activity game_activity_1
查看状态
[root@localhost game]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------
game_activity_1 docker-php-entrypoint php ... Exit 137
game_app_1 docker-php-entrypoint php ... Exit 137
[root@localhost game]# docker container ls -a
CONTAINER ID IMAGE STATUS PORTS NAMES
41aa5acf393f game_app Exited (137) game_app_1
6e20a111d779 game_activity Exited (137) game_activity_1
重启
[root@localhost game]# docker-compose restart
Restarting game_app_1 ... done
Restarting game_activity_1 ... done
查看状态
[root@localhost game]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------
game_activity_1 docker-php-entrypoint php ... Up 0.0.0.0:9501->9501/tcp,:::9501->9501/tcp
game_app_1 docker-php-entrypoint php ... Up 0.0.0.0:8000->8000/tcp,:::8000->8000/tcp
[root@localhost game]# docker container ls
CONTAINER ID IMAGE PORTS NAMES
41aa5acf393f game_app 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp game_app_1
6e20a111d779 game_activity 0.0.0.0:9501->9501/tcp, :::9501->9501/tcp game_activity_1
删除
只能删除停止运行的容器
[root@localhost game]# docker-compose rm
No stopped containers
[root@localhost game]# docker-compose stop app
Stopping game_app_1 ... done
[root@localhost game]# docker-compose rm
Going to remove game_app_1
Are you sure? [yN] y
Removing game_app_1 ... done
查看状态
[root@localhost game]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------
game_activity_1 docker-php-entrypoint php ... Up 0.0.0.0:9501->9501/tcp,:::9501->9501/tcp
[root@localhost game]# docker container ls -a
CONTAINER ID IMAGE PORTS NAMES
41aa5acf393f game_app 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp game_app_1
日志
[root@localhost game]# docker-compose logs app
Attaching to game_app_1
app_1 | PHP 7.3.33 Development Server started at Thu Mar 10 06:46:39 2022
app_1 | PHP 7.3.33 Development Server started at Thu Mar 10 06:51:06 2022
[root@localhost game]# docker container logs game_app_1
PHP 7.3.33 Development Server started at Thu Mar 10 06:46:39 2022
PHP 7.3.33 Development Server started at Thu Mar 10 06:51:06 2022