Docker Basics: Docker Compose

您所在的位置:网站首页 docker-compose与docker Docker Basics: Docker Compose

Docker Basics: Docker Compose

#Docker Basics: Docker Compose| 来源: 网络整理| 查看: 265

Image title

Docker Compose is a tool that allows you to run multi-container applications. With compose we can use yaml files to configure our application' services and then using a single command to create and start all of the configured services. I use this tool a lot when it comes to local development in a microservice environment. It is also lightweight and needs just a small effort. Instead of managing how to run each service while developing, you can have the environment and services needed preconfigured and focus on the service that you currently develop.

With docker compose , we can configure a network for our services, volumes, mount-points, environmental variables — just about everything.

To showcase this we are going to solve a problem. Our goal would be to extract data from MongoDB using Grafana. Grafana does not have out-of-the-box support for MongoD, therefore,e we will have to use a plugin.

The first step is to create our networks. Creating a network is not necessary since your services, once started, will join the default network. We will make a showcase of using custom networks, and have a network for backend services and a network for frontend services. Apparently, network configuration can get more advanced and specify custom network drivers or even configure static addresses.

version: '3.5' networks: frontend: name: frontend-network backend: name: backend-network internal: true

The backend network is going to be internal so there won't be any outbound connectivity to the containers attached to it.

Then we will setup our MongoDB instance.

version: '3.5' services: mongo: image: mongo restart: always environment: MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD} volumes: - ${DB_PATH}:/data/db networks: - backend

As you see, we specified a volume. Volumes can also be specified separately and attached to a service. We used environmental variables for the root account, and you might also have noticed that the password is going to be provided through environmental variables. The same applies for the volume path, too. You can have a more advanced configuration for volumes in your compose configuration and reference them from your service.

Our next goal is to set up the proxy server which will be in the middle of our Grafana and MongoDB server. Since it needs a custom Dockerfile to create it, we will do it through docker-compose. Compose has the capability to spin up a service by specifying the docker file.

So let's start with the Dockerfile.

FROM node WORKDIR /usr/src/mongografanaproxy COPY . /usr/src/mongografanaproxy EXPOSE 3333 RUN cd /usr/src/mongografanaproxy RUN npm install ENTRYPOINT ["npm","run","server"]

Then let's add it to compose.

version: '3.5' services: mongo-proxy: build: context: . dockerfile: ProxyDockerfile restart: always networks: - backend

And the same will be done to the Grafana image that we want to use. Instead of using a ready Grafana image, we will create one with the plugin preinstalled.

FROM grafana/grafana COPY . /var/lib/grafana/plugins/mongodb-grafana EXPOSE 3000 version: '3.5' services: grafana: build: context: . dockerfile: GrafanaDockerfile restart: always ports: - 3000:3000 networks: - backend - frontend

Let's wrap them all together:

version: '3.5' services: mongo: image: mongo restart: always environment: MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD} volumes: - ${DB_PATH}:/data/db networks: - backend mongo-proxy: build: context: . dockerfile: ProxyDockerfile restart: always networks: - backend grafana: build: context: . dockerfile: GrafanaDockerfile restart: always ports: - 3000:3000 networks: - backend - frontend networks: frontend: name: frontend-network backend: name: backend-network internal: true

So let's run them all together.

docker-compose -f stack.yaml build MONGO_USER=root MONGO_PASSWORD=root DB_PATH=~/grafana-mongo docker-compose -f stack.yaml up

This code can be found on Github, and for more, check out the Docker Images, Docker Containers, and Docker registry posts.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3