Docker Compose
链接和运行docker容器集群, 我们使用Docker Compose.
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.
Docker compose is usually installed when you install docker. So to simply check if you have it installed, run
$ docker-compose
You should see a list of commands from docker-compose. If not, you can go through the installationhere
Note: Ensure that you have docker-compose version 1.6 and above by runningdocker-compose -v
在根目录下创建文件docker-compose.yml.
$ touch docker-compose.yml
Our directory tree should now look like this.
.
├── angular-client
├── docker-compose.yml
└── express-server
Then edit thedocker-compose.ymlfile
mean-docker/docker-compose.yml
version: '2' # specify docker-compose version
# Define the services/containers to be run
services:
angular: # name of the first service
build: angular-client # specify the directory of the Dockerfile
ports:
- "4200:4200" # specify port forewarding
express: #name of the second service
build: express-server # specify the directory of the Dockerfile
ports:
- "3000:3000" #specify ports forewarding
database: # name of the third service
image: mongo # specify image to build container from
ports:
- "27017:27017" # specify port forewarding
Thedocker-compose.ymlfile is a simple configuration file telling docker compose which continers to build. That's pretty much it.
Now, to run containers based on the three images, simply run
$ docker-compose up
This will build the images if not already built, and run them. Once it's running, and your terminal looks something like this.
You can visit all the three apps,http://localhost:4200,http://localhost:3000ormongodb://localhost:27017, and you'll see that all the three containers are running.