文章目录
顶级 services 配置
一、基本配置
vim docker-compose.yml
version: "3.8"
services:
h1:
image: "nginx"
container_name: docker-myhosts
[root@Docker1 compose]# docker-compose up -d
[root@Docker1 compose]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------
docker-myhosts /docker-entrypoint.sh ngin ... Up 80/tcp
二、Build
a.简单使用
[root@Docker-1 compose]# cat docker-compose.yml
version: "3.8"
services:
webapp:
build: .
[root@Docker-1 compose]# cat Dockerfile
FROM nginx
COPY ./index.html /usr/share/nginx/html/index.html
[root@Docker-1 compose]# echo Hello,neko > index.html
[root@Docker-1 compose]# ls
docker-compose.yml Dockerfile index.html
[root@Docker-1 compose]# docker-compose up -d
WARNING: Found orphan containers (docker-myhosts, host1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Building webapp
Step 1/2 : FROM nginx
---> f6d0b4767a6c
Step 2/2 : COPY ./index.html /usr/share/nginx/html/index.html
---> 236d0b8f3cf2
Successfully built 236d0b8f3cf2
Successfully tagged compose_webapp:latest
WARNING: Image for service webapp was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating compose_webapp_1 ... done
[root@Docker-1 compose]# docker images compose_webapp:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_webapp latest 236d0b8f3cf2 57 seconds ago 133MB
[root@Docker-1 compose]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------
compose_webapp_1 /docker-entrypoint.sh ngin ... Up 80/tcp
b.构建lnmp环境
[root@Docker-1 neko]# mkdir mysql nginx php
[root@Docker-1 neko]# ls
docker-compose.yml Dockerfile mysql nginx php
[root@Docker-1 neko]# cp Dockerfile nginx/
[root@Docker-1 neko]# cp Dockerfile mysql/
[root@Docker-1 neko]# cp Dockerfile php/
[root@Docker-1 neko]# tree .
.
├── docker-compose.yml
├── Dockerfile
├── mysql
│ └── Dockerfile
├── nginx
│ ├── Dockerfile
│ └── index.html
└── php
└── Dockerfile
[root@Docker-1 neko]# cat docker-compose.yml
version: "3.8"
services:
webapp:
build: ./nginx
db:
build: ./mysql
php:
build: ./php
销毁之前的compose
docker-compose down
[root@Docker-1 neko]# cat nginx/Dockerfile
FROM nginx
COPY ./index.html /usr/share/nginx/html/index.html
[root@Docker-1 neko]# cat mysql/Dockerfile
FROM mysql
[root@Docker-1 neko]# cat php/Dockerfile
FROM php:7.4-fpm
构建 不启动
docker-compose build
查看项目
[root@Docker-1 neko]# docker images |grep neko
neko_webapp latest 236d0b8f3cf2 35 minutes ago 133MB
neko_db latest c8562eaf9d81 2 days ago 546MB
neko_php latest f5460fa2369d 9 days ago 405MB
报错解决
[root@Docker-1 compose]# docker-compose up -d
WARNING: Found orphan containers (myhost) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
解决办法:
docker-compose up -d --remove-orphans
三、Image
指定为镜像名称或镜像 ID。如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。
image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
[root@Docker-1 ~]# mkdir image-compose
[root@Docker-1 ~]# cd image-compose/
[root@Docker-1 image-compose]# cat docker-compose.yml
version: "3.8"
services:
neko-1:
image: nginx
[root@Docker-1 image-compose]# docker-compose up -d
Creating network "image-compose_default" with the default driver
Creating image-compose_neko-1_1