日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

从零开始学习docker(十二)介绍Docker Compose基本使用

發(fā)布時(shí)間:2024/9/16 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从零开始学习docker(十二)介绍Docker Compose基本使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

擼了今年阿里、頭條和美團(tuán)的面試,我有一個(gè)重要發(fā)現(xiàn).......>>>

默認(rèn)情況下在mac或者Windows安裝docker的時(shí)候會(huì)自動(dòng)安裝docker-compose。對于Linux來說是需要獨(dú)立安裝docker compose。

linux下安裝docker-compose

前提是已經(jīng)安裝好docker。

去github上下載docker-compose-Linux-x86_64 。

然后將下載的compose文件 "docker-compose-Linux-x86_64“ 改名為 “docker-compose”,移動(dòng)到 /usr/local/bin

sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

添加可執(zhí)行權(quán)限:

sudo chmod +x /usr/local/bin/docker-compose

這樣docker-compose就安裝好了,查看version:

iie4bu@hostdocker:~$ docker-compose --version docker-compose version 1.24.1, build 4667896b

一般情況下,我們使用docker-compose時(shí),往往會(huì)聯(lián)合使用docker-compose.yml文件。

docker-compose up

這個(gè)命令可以將docker-compose.yml文件中的services全部啟動(dòng)。

這個(gè)命令執(zhí)行的文件名是docker-compose.yml。如果要指定文件,需要加上-f參數(shù)。例如:

docker-compose -f docker-compose-2.yml up

-f 的默認(rèn)值是docker-compose.yml,所以可以不用寫。

實(shí)驗(yàn)一

新建docker-compose.yml,內(nèi)容如下:

version: '3'services:wordpress:image: wordpressports:- 8080:80environment:WORDPRESS_DB_HOST: mysqlWORDPRESS_DB_PASSWORD: rootnetworks:- my-bridgemysql:image: mysql:5.7environment:MYSQL_ROOT_PASSWORD: rootMYSQL_DATABASE: wordpressvolumes:- mysql-data:/var/lib/mysqlnetworks:- my-bridgevolumes:mysql-data:networks:my-bridge:driver: bridge

執(zhí)行命令:

docker-compose up Creating docker-compose-dir_wordpress_1 ... done Creating docker-compose-dir_mysql_1 ... done Attaching to docker-compose-dir_mysql_1, docker-compose-dir_wordpress_1 mysql_1 | Initializing database wordpress_1 | WordPress not found in /var/www/html - copying now... wordpress_1 | Complete! WordPress has been successfully copied to /var/www/html mysql_1 | 2019-07-04T14:41:38.299244Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. mysql_1 | 2019-07-04T14:41:38.299306Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server in progress as process 27 mysql_1 | 2019-07-04T14:41:41.616051Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. wordpress_1 | [04-Jul-2019 14:41:43 UTC] PHP Warning: mysqli::__construct(): (HY000/2002): Connection refused in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused mysql_1 | 2019-07-04T14:41:43.800607Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server has completed mysql_1 | Database initialized mysql_1 | MySQL init process in progress... wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused mysql_1 | 2019-07-04T14:41:45.680731Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. mysql_1 | 2019-07-04T14:41:45.680839Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 78 mysql_1 | 2019-07-04T14:41:46.335401Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. mysql_1 | 2019-07-04T14:41:46.338587Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. mysql_1 | 2019-07-04T14:41:46.401763Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL. mysql_1 | 2019-07-04T14:41:46.638569Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it. mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it. wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it. mysql_1 | mysql: [Warning] Using a password on the command line interface can be insecure. mysql_1 | mysql_1 | 2019-07-04T14:41:52.014266Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.16) MySQL Community Server - GPL. mysql_1 | mysql_1 | MySQL init process done. Ready for start up. mysql_1 | wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused mysql_1 | 2019-07-04T14:41:52.395589Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. mysql_1 | 2019-07-04T14:41:52.395700Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 1 mysql_1 | 2019-07-04T14:41:52.795509Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. mysql_1 | 2019-07-04T14:41:52.797900Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. mysql_1 | 2019-07-04T14:41:52.831221Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL. mysql_1 | 2019-07-04T14:41:52.902234Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060 wordpress_1 | [04-Jul-2019 14:41:55 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:41:55 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:41:58 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:41:58 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:42:01 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:42:01 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:42:04 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:42:04 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:42:07 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:42:07 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:42:10 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:42:10 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | wordpress_1 | WARNING: unable to establish a database connection to 'mysql' wordpress_1 | continuing anyways (which might have unexpected results) wordpress_1 | wordpress_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message wordpress_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message wordpress_1 | [Thu Jul 04 14:42:10.695076 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.3.6 configured -- resuming normal operations wordpress_1 | [Thu Jul 04 14:42:10.696660 2019] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' mysql_1 | mbind: Operation not permitted

先是生成了docker-compose-dir_wordpress_1和docker-compose-dir_mysql_1,然后接下來是各自的日志輸出。

docker-compose ps

查看當(dāng)前有那些service在啟動(dòng)

docker-compose psName Command State Ports ---------------------------------------------------------------------------------------------- docker-compose-dir_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp docker-compose-dir_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcp

docker-compose stop

這個(gè)命令會(huì)停止當(dāng)前運(yùn)行的services

docker-compose stop Stopping docker-compose-dir_mysql_1 ... done Stopping docker-compose-dir_wordpress_1 ... done

docker-compose start

啟動(dòng)services?

docker-compose start Starting wordpress ... done Starting mysql ... done

docker-compose down

先停止container,然后刪除container,然后刪除network,但是并沒有刪除本地的image。

docker-compose down Stopping docker-compose-dir_mysql_1 ... done Stopping docker-compose-dir_wordpress_1 ... done Removing docker-compose-dir_mysql_1 ... done Removing docker-compose-dir_wordpress_1 ... done Removing network docker-compose-dir_my-bridge

docker-compose up -d

會(huì)在后臺啟動(dòng)container,但是不會(huì)打印輸出log.

docker-compose images

列舉出docker中的container所使用的image

docker-compose imagesContainer Repository Tag Image Id Size ---------------------------------------------------------------------------- docker-compose-dir_mysql_1 mysql 5.7 a1aa4f76fab9 356 MB docker-compose-dir_wordpress_1 wordpress latest a541a1a59631 427 MB

docker-compose exec?

作用與docker exec 類似,比如執(zhí)行docker-compose.yml中定義的mysql service,那么命令如下:

docker-compose exec mysql /bin/bash root@3669e2265f7d:/#

這樣就進(jìn)入了mysql service中去了。

注意,雖然我們使用docker contaienr ls 看到的container name:

docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3669e2265f7d mysql:5.7 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 3306/tcp, 33060/tcp docker-compose-dir_mysql_1 1e882117f6a1 wordpress "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:8080->80/tcp docker-compose-dir_wordpress_1

但是我們的service name 并不是docker-compose-dir_mysql_1,而是我們在docker-compose.yml中定義的mysql

docker network ls也同理,雖然顯示的網(wǎng)絡(luò)名稱是docker-compose-dir_my-bridge,但真正的名稱是在docker-compose.yml中定義的my-bridge

docker network ls NETWORK ID NAME DRIVER SCOPE 52a131a54732 bridge bridge local 3ba28f3c55c6 docker-compose-dir_my-bridge bridge local 8212167bd2d1 host host local 089cabdcfa5a none null local

實(shí)驗(yàn)二

我們還是使用之前的flask-redis項(xiàng)目:

新建文件夾flask-redis,文件夾下新建app.py,內(nèi)容如下:

from flask import Flask from redis import Redis import os import socketapp = Flask(__name__) redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379)@app.route('/') def hello():redis.incr('hits')return 'Hello Container World! I have bean seen %s times and my hostname is %s. \n' % (redis.get('hits'), socket.gethostname())if __name__ == "__main__":app.run(host="0.0.0.0", port=5000, debug=True)

新建Dockerfile,內(nèi)容如下:

FROM python:3.5 LABEL maintainer="vincent" COPY . /app WORKDIR /app RUN pip install flask redis EXPOSE 5000 CMD [ "python", "app.py" ]

然后新建docker-compose.yml,內(nèi)容如下:

version: "3"services:redis:image: redisweb:build:context: .dockerfile: Dockerfileports:- 8080:5000environment:REDIS_HOST: redis

這次我們不是使用拉取image的方式,而是使用Dockerfile方式去構(gòu)建。分別指定:context是Dockerfile的位置,以及dockerfile的名字。

輸出結(jié)果:

Building web Step 1/7 : FROM python:3.5---> 6da24b346f6e Step 2/7 : LABEL maintainer="vincent"---> Using cache---> 06d9f068cf9e Step 3/7 : COPY . /app---> 7685dbbee266 Step 4/7 : WORKDIR /app Removing intermediate container d47965af9324---> acbb4ee85216 Step 5/7 : RUN pip install flask redis---> Running in 46f5a5fc67ef Collecting flask Installing collected packages: Werkzeug, click, itsdangerous, MarkupSafe, Jinja2, flask, redis Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.4 click-7.0 flask-1.0.3 itsdangerous-1.1.0 redis-3.2.1 Removing intermediate container 46f5a5fc67ef---> 393d35fbafa6 Step 6/7 : EXPOSE 5000---> Running in 64a952eff901 Removing intermediate container 64a952eff901---> 067f011bf9ae Step 7/7 : CMD [ "python", "app.py" ]---> Running in 9511e8b91db4 Removing intermediate container 9511e8b91db4---> 3a601628a9b9 Successfully built 3a601628a9b9 Successfully tagged flask-redis_web:latest

訪問:

curl 192.168.0.109:8080 Hello Container World! I have bean seen b'5' times and my hostname is 1282ec5ae336.

成功!

總結(jié)

以上是生活随笔為你收集整理的从零开始学习docker(十二)介绍Docker Compose基本使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。