Docker Compose

2018/06/06 14:28
阅读数 82

一. 运行多个服务app.

1. docker-compose.yml文件内容如下:

[root@c720120 docker]# cat docker-compose.yml

version: "2"
services:
   web:
     image: fundamentalsofdocker/ch08-web:1.0
     ports:
       - 3000:3000
   db:
     image: fundamentalsofdocker/ch08-db:1.0
     volumes:
       - pets-data:/var/lib/postgresql/data

volumes:
   pets-data:


2. 进行构建。

[root@c720120 docker]# docker-compose up
Creating docker_web_1
Creating docker_db_1
Attaching to docker_web_1, docker_db_1
db_1   | 2018-06-06 03:31:29.456 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2018-06-06 03:31:29.456 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2018-06-06 03:31:29.473 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-06-06 03:31:29.658 UTC [19] LOG:  database system was interrupted; last known up at 2018-05-28 14:36:46 UTC
web_1  | Listening at 0.0.0.0:3000
db_1   | 2018-06-06 03:31:30.312 UTC [19] LOG:  database system was not properly shut down; automatic recovery in progress
db_1   | 2018-06-06 03:31:30.317 UTC [19] LOG:  redo starts at 0/15E7788
db_1   | 2018-06-06 03:31:30.317 UTC [19] LOG:  invalid record length at 0/15E7868: wanted 24, got 0
db_1   | 2018-06-06 03:31:30.317 UTC [19] LOG:  redo done at 0/15E7830
db_1   | 2018-06-06 03:31:30.370 UTC [1] LOG:  database system is ready to accept connections


3. 然后访问刚才创建的服务,进行校验

image



4. 可以依后端模式方式运行

[root@c720120 ~]# docker-compose up –d


5. 停止和清除应用。

[root@c720120 docker]# docker-compose down
Removing docker_db_1 ... done
Removing docker_web_1 ... done
Removing network docker_default


6. 移除在本实验中创建的卷

docker volume rm ch08_pets-data


二. 扩容服务

1. 扩容web服务为3个实例

[root@c720120 docker]# docker-compose up --scale web=3
Creating network "docker_default" with the default driver
WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash.
Creating docker_web_1 ... error
Creating docker_web_2 ... error
Creating docker_web_3 ... done
Creating docker_db_1  ...

ERROR: for docker_web_2  Cannot start service web: b'driver failed programming external connectivity on endpoint docker_web_2 (08577c93fc049638fe034a078c0beaea7e43aca280fb1101046f914969d6e22d): Bind for 0.0.0.0:3000 failed: port is already allocated'

ERROR: for docker_web_1  Cannot start service web: b'driver failed programming external connectivity on endpoint docker_web_1 (1b839b479360b96fcd83aa69948ede428792089544c369b0d7a41f93f876c050): Bind for 0.0
Creating docker_db_1  ... done

ERROR: for web  Cannot start service web: b'driver failed programming external connectivity on endpoint docker_web_2 (08577c93fc049638fe034a078c0beaea7e43aca280fb1101046f914969d6e22d): Bind for 0.0.0.0:3000 failed: port is already allocated'
ERROR: Encountered errors while bringing up the project.


注意:在扩容服务时,报错,这是由于web服务使用3000端口,而第二个容器使用相同端口的话就会报错。


2. 关闭1步创建的服务

[root@c720120 docker]# docker-compose down
Stopping docker_db_1  ... done
Stopping docker_web_3 ... done
Removing docker_db_1  ... done
Removing docker_web_1 ... done
Removing docker_web_2 ... done
Removing docker_web_3 ... done
Removing network docker_default


3. 修改后docker-compoe.yml文件内容如下:

[root@c720120 docker]# cat docker-compose.yml
version: "2"
services:
   web:
     image: fundamentalsofdocker/ch08-web:1.0
     ports:
       - 3000
   db:
     image: fundamentalsofdocker/ch08-db:1.0
     volumes:
       - pets-data:/var/lib/postgresql/data

volumes:
   pets-data:


4. 再一次的启动服务并扩容

[root@c720120 docker]# docker-compose up -d
Creating network "docker_default" with the default driver
Creating docker_web_1 ... done
Creating docker_db_1  ... done
[root@c720120 docker]# docker-compose scale web=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting docker_web_1 ... done
Creating docker_web_2 ... done
Creating docker_web_3 ... done


5. 显示创建的的应用

[root@c720120 docker]# docker-compose ps
     Name                  Command              State            Ports        
------------------------------------------------------------------------------
docker_db_1    docker-entrypoint.sh postgres   Up      5432/tcp              
docker_web_1   /bin/sh -c node src/server.js   Up      0.0.0.0:32769->3000/tcp
docker_web_2   /bin/sh -c node src/server.js   Up      0.0.0.0:32771->3000/tcp
docker_web_3   /bin/sh -c node src/server.js   Up      0.0.0.0:32770->3000/tcp


三. 使用docker-compose构建并推送应用到registry

1. docker-compose.dev.yml文件内容信息如下:

[root@c720120 docker]# cat docker-compose.dev.yml
version: "3.5"
serives:
   web:
     build: web
     image: fundamentalsofdocker/ch08-web:1.0
     ports:
       - 3000:3000
   db:
     build: database
     image: fundamentalsofdocker/ch08-db:1.0
     volumes:
       - pets-data:/var/lib/postgresql/data

volumes:
   pets-data:


2. 构建应用


[root@c720120 docker]# docker-compose -f docker-compose.dev.yml build
Building web
Step 1/8 : FROM node:9.6-alpine
9.6-alpine: Pulling from library/node
605ce1bd3f31: Already exists
d9c1bb40879c: Already exists
d610e8516793: Already exists
Digest: sha256:7422a2653c781eb8280a9b811f0c5b605b54851fe458b09d9ccd6e04fbbca2ab
Status: Downloaded newer image for node:9.6-alpine
  ---> a88ff852e3d4
Step 2/8 : RUN mkdir /app
  ---> Running in 8682b617ceaf
Removing intermediate container 8682b617ceaf
  ---> 9a4a9446037f
Step 3/8 : WORKDIR /app
Removing intermediate container a32f8a2a0e50
  ---> ccf3294d5b31
Step 4/8 : COPY package.json /app/
  ---> b8168f45d652
Step 5/8 : RUN npm install
  ---> Running in 4c02c4300c8f
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN pets@1.0.0 No repository field.

added 72 packages in 7.547s
Removing intermediate container 4c02c4300c8f
  ---> a15517e4c50f
Step 6/8 : COPY ./src /app/src
  ---> d0aadafe8907
Step 7/8 : EXPOSE 3000
  ---> Running in dd2cc2425dbe
Removing intermediate container dd2cc2425dbe
  ---> 1d9d1fa98ed2
Step 8/8 : CMD node src/server.js
  ---> Running in 5983df0ea47a
Removing intermediate container 5983df0ea47a
  ---> f0617a7838f1

Successfully built f0617a7838f1
Successfully tagged fundamentalsofdocker/ch08-web:1.0
Building db
Step 1/5 : FROM postgres:10.2-alpine
10.2-alpine: Pulling from library/postgres
ff3a5c916c92: Already exists
a503b44e1ce0: Already exists
211706713093: Already exists
8df57d533e71: Already exists
7858f71c02fb: Already exists
55a8ef17ba59: Already exists
3fb44f23d323: Already exists
65cad41156b3: Already exists
5492a5bead70: Already exists
Digest: sha256:2cdf8430d7c1f59b3d3808f672944491aabf0fdf15da5b5e158e9fa4162453bf
Status: Downloaded newer image for postgres:10.2-alpine
  ---> 400ef39e0dbf
Step 2/5 : COPY init-db.sql /docker-entrypoint-initdb.d/
  ---> 02a6cc6e59b0
Step 3/5 : ENV POSTGRES_USER dockeruser
  ---> Running in de0ed866ac90
Removing intermediate container de0ed866ac90
  ---> d05dbfe63994
Step 4/5 : ENV POSTGRES_PASSWORD dockerpass
  ---> Running in e399bd4c3345
Removing intermediate container e399bd4c3345
  ---> fbf42b96b732
Step 5/5 : ENV POSTGRES_DB pets
  ---> Running in 4c811d6c99b9
Removing intermediate container 4c811d6c99b9
  ---> baaaf8ce22ba

Successfully built baaaf8ce22ba
Successfully tagged fundamentalsofdocker/ch08-db:1.0


3. 上传镜像前进行认证

[root@c720120 docker]# docker login -u fundamentalsofdocker -p <password>

FGOU(EE$X6GN(XF3PRIO6D7


4. 登陆成功后,进行上传

[root@c720120 docker]# docker-compose -f docker-compose.dev.yml  push
Pushing web (fundamentalsofdocker/ch08-web:1.0)...
The push refers to repository [docker.io/fundamentalsofdocker/ch08-web]
f457d7916b75: Preparing
564bf5c38714: Preparing

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部