對於第2版搬運工,譜寫你.yml
或.yaml
可以是這樣的:
version: '2'
volumes:
dbdata:
services:
mysql:
image: mysql:5.7
container_name: mysql
volumes:
- dbdata:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=homestead
- MYSQL_USER=root
- MYSQL_PASSWORD=secret
ports:
- "33061:3306"
與docker-compose up -d
啓動,並檢查:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3567fb78d0d mysql:5.7 "docker-entrypoint..." 2 minutes ago Up 2 minutes 0.0.0.0:33061->3306/tcp mysql
docker exec -it a3567fb78d0d bash
[email protected]:/# mysql -u root -p homestead
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| homestead |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
你的量將在堅持碼頭容積nameoffolder_dbdata
(/var/lib/docker/volumes/...
)
這不完全是我問,但它很有趣。我想知道envinronment vars是否也會創建數據庫。因爲在docker-compose.yml圖中是數據庫名稱,爲什麼? – Pedrog
我並不完全理解你的回答/問題,但是,env var正在創建一個名爲homestead的數據庫。當我執行演出數據庫時檢查我的答案。你看到了宅基地數據庫,我也在那個數據庫上進行身份驗證(-p) – lvthillo
我已經有了容器。我想爲容器創建數據庫。你能告訴我如何爲容器創建docker-compose文件嗎? –