本文概述
- 安装Docker Compose
- Docker撰写文件
- Docker撰写工作流程
- 使用Docker Compose容器化MEAN Stack应用程序
Docker是DevOps世界中最流行的容器化工具。但是, 什么是Docker Compose?
Docker Compose用于运行使用YAML文件具有多个容器的应用程序。
在某些情况下, docker应用程序必须针对不同的技术堆栈运行多个容器。现在, 为每个容器构建, 运行, 连接单独的dockerfile可能是一项艰巨的任务。这是docker-compose帮助你的地方。
使用一个简单明了的docker-compose.yml文件, 你可以通过运行一个命令来构建, 连接和启动所有容器。这对于生产中的企业应用程序非常有用, 在生产中, 多个应用程序在容器中运行。通过轻松地在Docker容器中运行100多个应用程序, 可以节省大量时间。
安装Docker Compose 在安装compose之前, 应该已经在系统上安装了Docker。
运行以下命令以安装docker-compose。
[email
protected]:/home/geekflare$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose[sudo] password for geekflare:% Total
% Received % Xferd
Average Speed
Time
Time
Time
CurrentDload
Upload
Total
Spent
Left
Speed100
617
0
617
0
0
1209
0 --:--:-- --:--:-- --:--:--
1209100 11.1M
100 11.1M
0
0
348k
0
0:00:32
0:00:32 --:--:--
476k
运行以下命令来设置文件权限。
[email
protected]:/home/geekflare$ sudo chmod +x /usr/local/bin/docker-compose
检查它是否正确安装, 它应该返回docker-compose版本。
[email
protected]:/home/geekflare$ docker-compose --versiondocker-compose version 1.23.1, build b02f1306
以下是可用于docker-compose的命令列表。
[email
protected]:/home/geekflare$ docker-composeDefine and run multi-container applications with Docker.Usage:docker-compose [-f <
arg>
…] [options] [COMMAND] [ARGS…]docker-compose -h|--helpOptions:-f, --file FILE
Specify an alternate compose file(default: docker-compose.yml)-p, --project-name NAME
Specify an alternate project name(default: directory name)--verbose
Show more output--log-level LEVEL
Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)--no-ansi
Do not print ANSI control characters-v, --version
Print version and exit-H, --host HOST
Daemon socket to connect to--tls
Use TLS;
implied by –tlsverify--tlscacert CA_PATH
Trust certs signed only by this CA--tlscert CLIENT_CERT_PATH
Path to TLS certificate file--tlskey TLS_KEY_PATH
Path to TLS key file--tlsverify
Use TLS and verify the remote--skip-hostname-check
Don’t check the daemon’s hostname against thename specified in the client certificate--project-directory PATH
Specify an alternate working directory(default: the path of the Compose file)--compatibility
If set, Compose will attempt to convert deploykeys in v3 files to their non-Swarm equivalentCommands:build
Build or rebuild servicesbundle
Generate a Docker bundle from the Compose fileconfig
Validate and view the Compose filecreate
Create servicesdown
Stop and remove containers, networks, images, and volumesevents
Receive real time events from containersexec
Execute a command in a running containerhelp
Get help on a commandimages
List imageskill
Kill containerslogs
View output from containerspause
Pause servicesport
Print the public port for a port bindingps
List containerspull
Pull service imagespush
Push service imagesrestart
Restart servicesrm
Remove stopped containersrun
Run a one-off commandscale
Set number of containers for a servicestart
Start servicesstop
Stop servicestop
Display the running processesunpause
Unpause servicesup
Create and start containersversion
Show the Docker-Compose version information
Docker撰写文件 这是一个示例docker-compose文件, 可完成所有操作。
version: '3'services:web:build: .ports:- "5000:5000"redis:image: "redis:alpine"
该文件的第一行指定使用的版本。该数字取决于系统上安装的Docker引擎。我安装了Docker 18.09.6, 它属于docker-compose的版本3。在此处查看有关版本的更多详细信息– https://docs.docker.com/compose/compose-file/compose-versioning/
该Docker文件正在运行两个服务/应用程序, 即Web和Redis。该Web服务通过dockerfile构建, 并在默认的flask Web服务器端口– 5000上运行。Redis服务通过从Docker Hub注册表中拉出Redis映像来运行。
要执行docker-compose.yml文件, 你需要运行一个非常简单的命令:docker-compose up
Docker撰写工作流程 以下是使用docker-compose的三个步骤。
- 创建每个服务的dockerfile
- 创建一个docker-compose.yml文件以连接所有dockerfile
- 运行docker-compose up命令启动系统
my-app|-----web|---------Dockerfile|-----redis|docker-compose.yml
my-app是我的主要项目目录。该目录包含Web和Redis服务目录以及docker-compose YAML文件。 Web服务的Dockerfile位于Web目录中。由于Redis服务是直接从Docker Hub提取的, 因此Redis目录中不需要dockerfile。这就是docker-compose工作流程的样子。
使用Docker Compose容器化MEAN Stack应用程序 现在你已经了解了基本概念。让我展示一个演示如何使用docker-compose容器化MEAN堆栈应用程序。
MEAN代表MongoDB, Express, Angular和Node.js。一起使用这些服务的应用程序也称为MEAN /全栈应用程序。
对于此演示, 我们将运行三个Docker容器:
- 容器1 –角形
- 容器2 – NodeJS和ExpressJS
- 容器3 – MongoDB
这就是我的docker-compose.yml文件将如何运行这三个容器的样子:
version: '3'services:angular:build: angular-clientports:- "4200:4200"volumes:- ./angular-client/:/var/www/appexpress:build: express-serverports:- "3000:3000"volumes:- ./express-server/:/var/www/applinks:- databasedatabase:image: mongoports:- "27017:27017"
- 第一行指定使用的docker-compose版本
- 我们正在运行三种服务–角度服务, 快递服务和数据库服务
- Angular服务将使用dockerfile构建。它将在端口4200上运行, 应用程序卷为/ var / www / app
- Express服务将使用dockerfile构建。 Express服务器将在端口3000上运行, 其卷为/ var / www / app
- 数据库服务将从dockerhub中提取MongoDB映像并在27017之后启动
[email
protected]:~$ cd mean[email
protected]:~/mean$ cd angular-client
如果你的系统上未安装节点软件包管理器(npm), 请运行以下命令(忽略是否已安装)。
[email
protected]:~/mean/angular-client$ sudo apt install npm[sudo] password for geekflare:Reading package lists... DoneBuilding dependency treeReading state information... DoneThe following additional packages will be installed:gyp javascript-common libc-ares2 libhttp-parser2.8 libjs-async libjs-inherits libjs-is-typedarraylibjs-jquery libjs-node-uuid libjs-underscore libssl1.0-dev libuv1 libuv1-dev node-abbrev node-ajvnode-ansi node-ansi-color-table node-ansi-regex node-ansi-styles node-ansistyles node-aproba node-archynode-are-we-there-yet node-async node-aws-sign2 node-balanced-match node-block-stream node-bluebirdnode-boom node-brace-expansion node-builtin-modules node-camelcase node-caseless node-chalk node-cliuinode-clone node-co node-color-convert node-color-name node-combined-stream node-concat-mapnode-config-chain node-console-control-strings node-cookie-jar node-copy-concurrently node-core-util-isnode-yallist node-yargs node-yargs-parser nodejs nodejs-dev nodejs-docSuggested packages:apache2 | lighttpd | httpd node-aws-sign node-oauth-sign node-http-signature debhelperThe following NEW packages will be installed:gyp javascript-common libc-ares2 libhttp-parser2.8 libjs-async libjs-inherits libjs-is-typedarraylibjs-jquery libjs-node-uuid libjs-underscore libssl1.0-dev libuv1 libuv1-dev node-abbrev node-ajvnode-ansi node-ansi-color-table node-ansi-regex node-ansi-styles node-ansistyles node-aproba node-archynode-are-we-there-yet node-async node-aws-sign2 node-balanced-match node-block-stream node-bluebird0 upgraded, 212 newly installed, 0 to remove and 233 not upgraded.Need to get 10.5 MB of archives.After this operation, 53.6 MB of additional disk space will be used.Do you want to continue? [Y/n] YGet:1 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 gyp all 0.1+20180428git4d467626-1 [237 kB]Get:2 http://us.archive.ubuntu.com/ubuntu cosmic/main amd64 javascript-common all 11 [6, 066 B]Get:3 http://us.archive.ubuntu.com/ubuntu cosmic/main amd64 libhttp-parser2.8 amd64 2.8.1-1 [20.8 kB]Get:4 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libjs-async all 0.8.0-3 [25.4 kB]Get:5 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libjs-is-typedarray all 1.0.0-2 [2, 934 B]Get:6 http://us.archive.ubuntu.com/ubuntu cosmic/main amd64 libjs-jquery all 3.2.1-1 [152 kB]Get:7 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libjs-node-uuid all 1.4.7-5 [11.5 kB]Get:8 http://us.archive.ubuntu.com/ubuntu cosmic/main amd64 libjs-underscore all 1.8.3~dfsg-1 [59.9 kB]Get:9 http://us.archive.ubuntu.com/ubuntu cosmic-updates/main amd64 libssl1.0-dev amd64 1.0.2n-1ubuntu6.2 [1, 366 kB]Fetched 10.5 MB in 1min 34s (112 kB/s)Extracting templates from packages: 100%Selecting previously unselected package gyp.(Reading database ... 180130 files and directories currently installed.)Preparing to unpack .../000-gyp_0.1+20180428git4d467626-1_all.deb ...Unpacking gyp (0.1+20180428git4d467626-1) ...Selecting previously unselected package javascript-common.Preparing to unpack .../001-javascript-common_11_all.deb ...Unpacking javascript-common (11) ...Selecting previously unselected package libhttp-parser2.8:amd64.Preparing to unpack .../002-libhttp-parser2.8_2.8.1-1_amd64.deb ...Setting up node-fstream-ignore (0.0.6-2) ...Setting up node-gyp (3.6.2-2) ...Setting up node-yargs (10.0.3-2) ...Setting up npm (5.8.0+ds-2) ...Processing triggers for libc-bin (2.28-0ubuntu1) ...
在angular-client目录中, 运行npm install。
[email
protected]:~/mean/angular-client$ npm install>
[email
protected] install /home/geekflare/mean/angular-client/node_modules/uws>
node-gyp rebuild >
build_log.txt 2>
&
1 || exit 0>
[email
protected] install /home/geekflare/mean/angular-client/node_modules/node-sass>
node scripts/install.jsDownloading binary from https://github.com/sass/node-sass/releases/download/v4.7.2/linux-x64-57_binding.nodeDownload complete
] - :Binary saved to /home/geekflare/mean/angular-client/node_modules/node-sass/vendor/linux-x64-57/binding.nodeCaching binary to /home/geekflare/.npm/node-sass/4.7.2/linux-x64-57_binding.node>
[email
protected] postinstall /home/geekflare/mean/angular-client/node_modules/webpack/node_modules/uglifyjs-webpack-plugin>
node lib/post_install.js>
[email
protected] postinstall /home/geekflare/mean/angular-client/node_modules/node-sass>
node scripts/build.jsBinary found at /home/geekflare/mean/angular-client/node_modules/node-sass/vendor/linux-x64-57/binding.nodeTesting binaryBinary is fineadded 1457 packages from 1250 contributors in 80.009s
现在转到express目录并运行npm install。
[email
protected]:~/mean/angular-client$ cd ..[email
protected]:~/mean$ cd express-server/[email
protected]:~/mean/express-server$ npm install
既然一切都设置好了, 是时候运行docker-compose.yml文件了, 它将启动所有docker容器并运行MEAN堆栈应用程序。
[email
protected]:~/mean/express-server$ cd ..[email
protected]:~/mean$ docker-compose upCreating network "mean_default" with the default driverBuilding angularStep 1/8 : FROM node:88: Pulling from library/nodea4d8138d0f6b: Pull completedbdc36973392: Pull completef59d6d019dd5: Pull completeaaef3e026258: Pull complete6e454d3b6c28: Pull completec717a7c205aa: Pull complete37add8e5ac11: Pull complete0314ab675d31: Pull complete012886364728: Pull completeDigest: sha256:310db2abcff097ef44af205d81833282a6d5471002a1b59d7b7459a74152c856Status: Downloaded newer image for node:8--->
8e45c884a32eStep 2/8 : RUN mkdir -p /var/www/app--->
Running in c70a0cab7994Removing intermediate container c70a0cab7994--->
001c5e840b24Step 3/8 : WORKDIR /var/www/app--->
Running in 622ebdc41b2fRemoving intermediate container 622ebdc41b2f--->
baa2e2347259Step 4/8 : COPY package.json /var/www/app--->
5b97543befabStep 5/8 : RUN npm install--->
Running in 73e3d8b7a701>
[email
protected] install /var/www/app/node_modules/uws>
node-gyp rebuild >
build_log.txt 2>
&
1 || exit 0>
[email
protected] install /var/www/app/node_modules/node-sass>
node scripts/install.jsDownloading binary from https://github.com/sass/node-sass/releases/download/v4.12.0/linux-x64-57_binding.nodeDownload completeBinary saved to /var/www/app/node_modules/node-sass/vendor/linux-x64-57/binding.nodeCaching binary to /root/.npm/node-sass/4.12.0/linux-x64-57_binding.node>
[email
protected] postinstall /var/www/app/node_modules/core-js>
node scripts/postinstall || echo "ignore"The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:>
https://opencollective.com/core-js>
https://www.patreon.com/zloirock>
[email
protected] postinstall /var/www/app/node_modules/webpack/node_modules/uglifyjs-webpack-plugin>
node lib/post_install.js>
[email
protected] postinstall /var/www/app/node_modules/node-sass>
node scripts/build.jsBinary found at /var/www/app/node_modules/node-sass/vendor/linux-x64-57/binding.nodeTesting binaryBinary is fineadded 1606 packages from 1329 contributors and audited 15092 packages in 112.427sRemoving intermediate container 73e3d8b7a701--->
55790d2fae93Step 6/8 : COPY . /var/www/app--->
61537aa487f4Step 7/8 : EXPOSE 4200--->
Running in 632eedc35a45Removing intermediate container 632eedc35a45--->
51e75b0e2ebeStep 8/8 : CMD ["npm", "start"]--->
Running in 36bbb12a0d38Removing intermediate container 36bbb12a0d38--->
9f8d61db600cSuccessfully built 9f8d61db600cSuccessfully tagged mean_angular:latestPulling database (mongo:)...latest: Pulling from library/mongo35b42117c431: Pull completead9c569a8d98: Pull complete293b44f45162: Pull complete0c175077525d: Pull complete4e73525b52ba: Pull completea22695a3f5e9: Pull completec5175bcf2977: Pull complete3e320da07793: Pull complete01c6db6b2b5a: Pull complete3bd6e9d03e78: Pull completee03dcf51513f: Pull completec1956a9e136a: Pull complete4c35cf22b1d5: Pull completeBuilding expressStep 1/9 : FROM node:8--->
8e45c884a32eStep 2/9 : RUN mkdir -p /var/www/app--->
Using cache--->
001c5e840b24Step 3/9 : WORKDIR /var/www/app--->
Using cache--->
baa2e2347259Step 4/9 : COPY package.json /var/www/app--->
0232ad53c679Step 5/9 : RUN npm install--->
Running in c309bf6f218eadded 128 packages from 151 contributors and audited 233 packages in 9.055sRemoving intermediate container c309bf6f218e--->
49e652884562Step 6/9 : RUN npm install -g nodemon--->
Running in 0ed5d7f3642b/usr/local/bin/nodemon ->
/usr/local/lib/node_modules/nodemon/bin/nodemon.js>
[email
protected] postinstall /usr/local/lib/node_modules/nodemon>
node bin/postinstall || exit 0Love nodemon? You can now support the project via the open collective:>
https://opencollective.com/nodemon/donate+ [email
protected]added 221 packages from 128 contributors in 18.856sRemoving intermediate container 0ed5d7f3642b--->
32c55606f35eStep 7/9 : COPY . /var/www/app--->
a618b38a2812Step 8/9 : EXPOSE 3000--->
Running in bea389ab3ef1Removing intermediate container bea389ab3ef1--->
684bbfb31371Step 9/9 : CMD ["npm", "start"]--->
Running in 9aa1b72e4a4eRemoving intermediate container 9aa1b72e4a4e--->
35dcb3df9806Successfully built 35dcb3df9806Successfully tagged mean_express:latestCreating mean_angular_1_de44b3f5b988
... doneCreating mean_database_1_708f8f9c3c33 ... doneCreating mean_express_1_b57a483a72ee
... doneAttaching to mean_angular_1_f257e2233ef1, mean_database_1_ccc5c677e00b, mean_express_1_574f07b045fcangular_1_f257e2233ef1 |angular_1_f257e2233ef1 | >
[email
protected] start /var/www/appangular_1_f257e2233ef1 | >
ng serve -H 0.0.0.0angular_1_f257e2233ef1 |database_1_ccc5c677e00b | 2019-07-20T22:33:25.933+0000 I CONTROL
[main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=f74b56905249database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten] db version v4.0.10database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten] git version: c389e7f69f637f7a1ac3cc9fae843b635f20b766database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten] OpenSSL version: OpenSSL 1.0.2g
1 Mar 2016database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten] allocator: tcmallocdatabase_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten] modules: nonedatabase_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten] build environment:database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten]
distmod: ubuntu1604database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten]
distarch: x86_64database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten]
target_arch: x86_64database_1_ccc5c677e00b | 2019-07-20T22:33:25.937+0000 I CONTROL
[initandlisten] options: { net: { bindIpAll: true } }express_1_574f07b045fc |express_1_574f07b045fc | >
[email
protected] start /var/www/appexpress_1_574f07b045fc | >
nodemon ./bin/wwwexpress_1_574f07b045fc |express_1_574f07b045fc | [nodemon] 1.19.1express_1_574f07b045fc | [nodemon] to restart at any time, enter `rs`express_1_574f07b045fc | [nodemon] watching: *.*express_1_574f07b045fc | [nodemon] starting `node ./bin/www`database_1_ccc5c677e00b | 2019-07-20T22:33:33.543+0000 I NETWORK
[listener] connection accepted from 172.19.0.4:38958 #1 (1 connection now open)database_1_ccc5c677e00b | 2019-07-20T22:33:33.560+0000 I NETWORK
[conn1] received client metadata from 172.19.0.4:38958 conn1: { driver: { name: "nodejs", version: "3.0.1" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.18.0-25-generic" }, platform: "Node.js v8.16.0, LE, mongodb-core: 3.0.1" }express_1_574f07b045fc | mongodb: connectedangular_1_f257e2233ef1 | ** NG Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **angular_1_f257e2233ef1 | Date: 2019-07-21T11:21:03.868Z - Hash: 639d9a968476ed482b5c - Time: 336msangular_1_f257e2233ef1 | 4 unchanged chunksangular_1_f257e2233ef1 | chunk {main} main.bundle.js (main) 19.8 kB [initial] [rendered]angular_1_f257e2233ef1 |angular_1_f257e2233ef1 | webpack: Compiled successfully.angular_1_f257e2233ef1 | webpack: Compiling...angular_1_f257e2233ef1 | Date: 2019-07-21T11:25:15.661Z - Hash: e5a2b1c1afe0deb396c3 - Time: 251msangular_1_f257e2233ef1 | 4 unchanged chunksangular_1_f257e2233ef1 | chunk {main} main.bundle.js (main) 19.8 kB [initial] [rendered]angular_1_f257e2233ef1 |angular_1_f257e2233ef1 | webpack: Compiled successfully.
转到浏览器并检查https:// localhost:4200, 你的应用程序将启动并运行。
文章图片
转到https:// localhost:3000以检查Express服务器是否正在运行。
文章图片
另外, 你可以运行docker images命令来查看docker中存在哪些图像。
[email
protected]:~/mean$ docker imagesREPOSITORY
TAG
IMAGE ID
CREATED
SIZEmean_express
latest
35dcb3df9806
14 hours ago
923MBmean_angular
latest
9f8d61db600c
14 hours ago
1.29GBnode
8
8e45c884a32e
9 days ago
895MBmongo
latest
785c65f61380
2 weeks ago
412MB
运行以下命令以查看在docker内部运行的容器。
[email
protected]:~/mean$ docker psCONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES681c9c34bee2
mean_express
"docker-entrypoint.s…"
14 hours ago
Up 14 hours
0.0.0.0:3000->
3000/tcp
mean_express_1_574f07b045fcf74b56905249
mongo
"docker-entrypoint.s…"
14 hours ago
Up 14 hours
0.0.0.0:27017->
27017/tcp
mean_database_1_ccc5c677e00b260ef1e52dab
mean_angular
"docker-entrypoint.s…"
14 hours ago
Up 14 hours
0.0.0.0:4200->
4200/tcp
mean_angular_1_f257e2233ef1
因此, 现在, 所有三个Docker容器均已启动并正在运行。
【如何安装Docker Compose和安装程序()】Docker-compose负责轻松运行所有容器。这是运行三个容器的简单示例。你可以想象如果必须在100个容器上启动应用程序, 这将有多大用处。继续尝试一下, 看看它是如何工作的。
推荐阅读
- 如何在Linux服务器之间交换SSH密钥以进行无密码身份验证()
- 26个带有示例的Docker命令
- 如何安装Puppet Bolt自动执行Sysadmin任务()
- 使用这些顶级工具自动化应用程序测试
- 什么是Dockerfile以及如何创建Docker镜像()
- Android开发—错误记录1(W/System.err: java.net.ConnectException: Connection refused)
- 刷题42. Trapping Rain Water
- Azure App Service-多语言/高可用/自动缩放的Web托管服务
- mybatis框架(入门方法,dao层原始开发方法,mapper代理开发)(sqlserver数据库)