您好,登錄后才能下訂單哦!
本篇內容主要講解“Docker Compose的基本使用方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Docker Compose的基本使用方法”吧!
默認情況下在mac或者Windows安裝docker的時候會自動安裝docker-compose。對于Linux來說是需要獨立安裝docker compose。
前提是已經安裝好docker。
去github上下載docker-compose-Linux-x86_64 。
然后將下載的compose文件 "docker-compose-Linux-x86_64“ 改名為 “docker-compose”,移動到 /usr/local/bin
sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
添加可執(zhí)行權限:
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時,往往會聯合使用docker-compose.yml文件。
這個命令可以將docker-compose.yml文件中的services全部啟動。
這個命令執(zhí)行的文件名是docker-compose.yml。如果要指定文件,需要加上-f參數。例如:
docker-compose -f docker-compose-2.yml up
-f 的默認值是docker-compose.yml,所以可以不用寫。
version: '3' services: wordpress: image: wordpress ports: - 8080:80 environment: WORDPRESS_DB_HOST: mysql WORDPRESS_DB_PASSWORD: root networks: - my-bridge mysql: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: wordpress volumes: - mysql-data:/var/lib/mysql networks: - my-bridge volumes: 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,然后接下來是各自的日志輸出。
查看當前有那些service在啟動
docker-compose ps Name 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
這個命令會停止當前運行的services
docker-compose stop Stopping docker-compose-dir_mysql_1 ... done Stopping docker-compose-dir_wordpress_1 ... done
啟動services
docker-compose start Starting wordpress ... done Starting mysql ... done
先停止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
會在后臺啟動container,但是不會打印輸出log.
列舉出docker中的container所使用的image
docker-compose images Container 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 exec 類似,比如執(zhí)行docker-compose.yml中定義的mysql service,那么命令如下:
docker-compose exec mysql /bin/bash root@3669e2265f7d:/#
這樣就進入了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也同理,雖然顯示的網絡名稱是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
我們還是使用之前的flask-redis項目:
新建文件夾flask-redis,文件夾下新建app.py,內容如下:
from flask import Flask from redis import Redis import os import socket app = 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,內容如下:
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,內容如下:
version: "3" services: redis: image: redis web: build: context: . dockerfile: Dockerfile ports: - 8080:5000 environment: REDIS_HOST: redis
這次我們不是使用拉取image的方式,而是使用Dockerfile方式去構建。分別指定:context是Dockerfile的位置,以及dockerfile的名字。
輸出結果:
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.
成功!
到此,相信大家對“Docker Compose的基本使用方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。