提交 689964a0 编写于 作者: J Jeff Fox

Using Docker Compose

上级 b5dcd2df
......@@ -249,3 +249,120 @@ With all of that explained, let's start our dev-ready container!
```sh
docker exec -it <mysql-container-id> mysql -p todos
```
## Using Docker Compose
### Installing Docker Compose
If you installed Docker Desktop/Toolbox for either Windows or Mac, you already have Docker Compose! Play-with-Docker instances already have Docker Compose installed as well. If you are on a Linux machine, you will need to install Docker Compose using the instructions [here](https://docs.docker.com/compose/install/).
After installation, you should be able to run the following and see version information.
```sh
docker-compose version
```
### Creating our Compose File
1. At the root of the app project, create a file named docker-compose.yml.
2. In the compose file, we'll start off by defining the schema version. In most cases, it's best to use the latest supported version. You can look at the Compose file reference for the current schema versions and the compatibility matrix.
```yaml
version: "3.7"
```
3. Next, we'll define the list of services (or containers) we want to run as part of our application.
```yaml
version: "3.7"
services:
```
And now, we'll start migrating a service at a time into the compose file.
### Defining the App Service
```yaml
version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
```
### Defining the MySQL Service
```yaml
version: "3.7"
services:
app:
# The app service definition
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
```
### Running our Application Stack
Now that we have our docker-compose.yml file, we can start it up!
1. Make sure no other copies of the app/db are running first (docker ps and docker rm -f \<ids>).
2. Start up the application stack using the docker-compose up command. We'll add the -d flag to run everything in the background.
```sh
docker-compose up -d
```
When we run this, we should see output like this:
```sh
Creating network "app_default" with the default driver
Creating volume "app_todo-mysql-data" with default driver
Creating app_app_1 ... done
Creating app_mysql_1 ... done
```
You'll notice that the volume was created as well as a network! By default, Docker Compose automatically creates a network specifically for the application stack (which is why we didn't define one in the compose file).
3. Let's look at the logs using the docker-compose logs -f command. You'll see the logs from each of the services interleaved into a single stream. This is incredibly useful when you want to watch for timing-related issues. The -f flag "follows" the log, so will give you live output as it's generated.
If you don't already, you'll see output that looks like this...
```sh
mysql_1 | 2019-10-03T03:07:16.083639Z 0 [Note] mysqld: ready for connections.
mysql_1 | Version: '5.7.27' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
app_1 | Connected to mysql db at host mysql
app_1 | Listening on port 3000
```
The service name is displayed at the beginning of the line (often colored) to help distinguish messages. If you want to view the logs for a specific service, you can add the service name to the end of the logs command (for example, docker-compose logs -f app).
4. At this point, you should be able to open your app and see it running. And hey! We're down to a single command!
### Tearing it All Down
When you're ready to tear it all down, simply run ```docker-compose down``` or hit the trash can on the Docker Dashboard for the entire app. The containers will stop and the network will be removed.
Once torn down, you can switch to another project, run ```docker-compose up``` and be ready to contribute to that project! It really doesn't get much simpler than that!
version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册