使用Dozzle升级本地Docker-Compose开发

I’m a huge fan of docker-compose for local development. Whether professionally or personal projects, most of my work these days is building micro-services that run in Docker containers. In addition, most of these containers at some point talk to each other or with a database, so docker-compose is always in the mix.

我非常喜欢docker-compose进行本地开发。 无论是专业项目还是个人项目,这些天我的大部分工作都是构建在Docker容器中运行的微服务。 另外,这些容器中的大多数在某些时候会互相通信或与数据库通信,因此docker-compose总是混在一起的。

If you’re not using docker-compose for your local Docker development, let me start with, you’re doing it all wrong! Compose offers so much for local development and there will be a whole other post about that subject later. What makes compose so great is its ability to allow me to spin up multiple containers at once, and test my code in a more real-world environment. If my stack consists of 5 APIs and a database, I can use compose to spin all those up and test locally before deploying, its fantastic.

如果您不使用docker-compose进行本地Docker开发,那么让我开始吧,您做错了一切! Compose为本地开发提供了很多东西,以后还会有其他有关该主题的文章。 使compose如此出色的原因在于它具有使我能够一次启动多个容器并在更真实的环境中测试代码的能力。 如果我的堆栈由5个API和一个数据库组成,那么我可以使用compose旋转所有这些API并在部署之前进行本地测试,这太棒了。

A drawback, however, is looking at logs. To be fair, there is nothing wrong with how Docker and Compose spit out logs. It's simple and does the job, but as you’ll see it can be hard to sift through them. Especially when you have multiple containers running at the same time. This is where Dozzle comes to the rescue. Dozzle is a simple, lightweight application that provides you with a web-based interface to monitor your Docker container logs live.

但是,缺点是查看日志。 公平地说,Docker和Compose吐出日志没有错。 它很简单并且可以完成工作,但是正如您将看到的,很难对它们进行筛选。 特别是当您有多个容器同时运行时。 这是Dozzle进行救援的地方。 Dozzle是一个简单,轻量级的应用程序,为您提供基于Web的界面,以实时监控Docker容器日志。

For now, we’re going to look at a very small project that uses compose to spin up a single Go API, as well as a Postgres database for that API to talk to. We’ll then see how Dozzle makes your life easier with its simple and easy to use interface.

现在,我们将看一个非常小的项目,该项目使用compose旋转一个Go API,以及一个与该API进行通信的Postgres数据库。 然后,我们将看到Dozzle如何通过其简单易用的界面使您的生活更轻松。

一个简单的设置 (A Simple Setup)

Let’s take a look at how Compose would normally spit out logs for you to view. In this example, I’ve got a very simple setup: a Golang REST API using Gorilla MUX and a Postgres database. My Docker Compose file is very simple a straight-forward.

让我们看一下Compose通常如何吐出日志供您查看。 在此示例中,我有一个非常简单的设置:使用Gorilla MUX和Postgres数据库的Golang REST API。 我的Docker Compose文件非常简单明了。

version: "3.7"
services:
api:
container_name: go-mux-api
image: github.com/atkinsonbg/go-gmux-proper-unit-testing/api:latest
environment:
DBHOST: go-mux-db
DBNAME: go-mux-db
DBUSER: postgres
ports:
- 80:80
database:
container_name: go-mux-db
image: postgres:11.6-alpine
environment:
POSTGRES_DB: go-mux-db
volumes:
- ./scripts/init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- 5432:5432

As you can see, there is not much going on here. This file will just spin up the API and the database in their respective containers and we’re good to go. I run this via a Makefile command which is shown below.

如您所见,这里没有太多活动。 该文件只会在各自的容器中启动API和数据库,我们很高兴。 我通过一个Makefile命令运行它,如下所示。

up:
docker-compose down
COMPOSE_DOCKER_CLI_BUILD=1 docker-compose up --build --detach
docker-compose logs -f

Again, nothing fancy going on here. I just docker-compose downfirst to clean anything up, docker-compose upto start up the stack, and then start following the logs. The resulting log output is shown in the following screenshot.

同样,这里没有任何幻想。 我只是先docker-compose down清理所有内容,然后docker-compose up启动堆栈,然后再按照日志进行操作。 结果的日志输出显示在以下屏幕截图中。

Image for post

The containers start spitting out logs that are shown in the terminal, and they are even color-coded by container, which is nice. However, this is only two containers, one of which isn’t very chatty and it's still a lot to sift through. If you’re looking for something specific, like an error for say, it’s not going to stand out quickly. Now imagine you have five or six containers running!

容器开始吐出显示在终端中的日志,它们甚至由容器进行颜色编码,这很好。 但是,这只是两个容器,其中一个容器不是很健谈,仍然需要大量筛选。 如果您正在寻找特定的东西,例如说错了,那将不会很快脱颖而出。 现在,假设您正在运行五个或六个容器!

输入喷嘴 (Enter Dozzle)

As stated earlier, Dozzle is a simple, lightweight application that provides you with a web-based interface to monitor your Docker container logs live. It comes via its own Docker container that runs alongside your containers. Once it's running, you can open up a browser on a “http://localhost:8888/” to see your logs. That port is arbitrary, as it runs on 8080 internally, so update that to whatever works for your setup.

如前所述,Dozzle是一个简单,轻量级的应用程序,为您提供了一个基于Web的界面来实时监控Docker容器日志。 它通过与容器一起运行的自己的Docker容器来实现。 运行后,您可以在“ http:// localhost:8888 / ”上打开浏览器以查看日志。 该端口是任意的,因为它在内部运行于8080,因此请将其更新为适用于您的设置的任何端口。

Since Dozzle runs in a Docker container, all you need to do to get started is pull the image: docker pull amir20/dozzle:latest, then run it with: docker run --name dozzle -d --volume=/var/run/docker.sock:/var/run/docker.sock -p 8888:8080 amir20/dozzle:latest

由于Dozzle在Docker容器中运行,因此您需要做的就是拉镜像: docker pull amir20/dozzle:latest ,然后使用以下命令运行它: docker run --name dozzle -d --volume=/var/run/docker.sock:/var/run/docker.sock -p 8888:8080 amir20/dozzle:latest

Image for post

In this instance, I only have a Dozzle container running, but that’s ok as you can already see it in action. The Dozzle container is showing its own logs! Now let’s launch the stack from before and really see Dozzle in action!

在这种情况下,我只运行了一个Dozzle容器,但是没关系,因为您已经可以看到它在运行。 Dozzle容器正在显示其自己的日志! 现在,让我们从之前启动堆栈,并真正看到Dozzle的行动!

Image for post

This is a much better view of my container logs! We get a simplified view of all our running containers, in this case, everything my compose file spun up. You can click each container and see just its logs. This alone is already a time-saver, but Dozzle offers a couple more surprises to make your life even easier!

这是我的容器日志的更好的视图! 我们得到了所有正在运行的容器的简化视图,在这种情况下,我撰写的文件都旋转了。 您可以单击每个容器,然后仅查看其日志。 仅此一项已节省时间,但是Dozzle带来了更多惊喜,使您的生活更加轻松!

Image for post

固定容器日志 (Pinning Container Logs)

A simple but powerful feature is the ability to pin logs and see containers side by side. This is especially powerful if you have two containers that talk to each other. Being able to easily see the logs of that interaction side by side in real-time is an amazing feature. Just select the container you want to view, then click the “pin” icon on the second container to view its logs as well.

一个简单而强大的功能是能够固定日志并排查看容器。 如果您有两个相互通信的容器,则此功能特别强大。 能够实时轻松地并排查看该交互的日志是一个了不起的功能。 只需选择要查看的容器,然后单击第二个容器上的“固定”图标即可查看其日志。

Image for post

You aren’t limited to just two containers, you can pin as many as your screen real estate will allow!

您不仅限于两个容器,还可以固定尽可能多的屏幕空间!

Image for post

搜索日志 (Searching Logs)

Looking for a specific log statement, or keywords. Not an issue, Dozzle supports real time searching of your logs! Just “Ctrl + f” or “Cmd + f” to open the search input and start typing.

寻找特定的日志语句或关键字。 没问题,Dozzle支持实时搜索日志! 只需按“ Ctrl + f”或“ Cmd + f”即可打开搜索输入并开始输入。

Image for post

The search applies to all open log windows, which is either a really great feature or a miss. It all depends on your use case. If you’re looking at cross container communication that is throwing errors, this may come is very handy.

该搜索适用于所有打开的日志窗口,这是一项非常有用的功能,也可能是一项遗漏。 这完全取决于您的用例。 如果您正在查看引发错误的跨容器通信,则这可能非常方便。

Image for post

放在一起 (Putting It All Together)

Now that we have the Dozzle container pulled locally, a simple tweak to our Docker Compose file will spin up Dozzle for us automatically!

现在我们已经将Dozzle容器拉到本地,对Docker Compose文件的简单调整将自动为我们启动Dozzle!

version: "3.7"
services:
api:
container_name: go-mux-api
image: github.com/atkinsonbg/go-gmux-proper-unit-testing/api:latest
environment:
DBHOST: go-mux-db
DBNAME: go-mux-db
DBUSER: postgres
ports:
- 80:80
database:
container_name: go-mux-db
image: postgres:11.6-alpine
environment:
POSTGRES_DB: go-mux-db
volumes:
- ./scripts/init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- 5432:5432
dozzle:
container_name: go-mux-dozzle
image: amir20/dozzle:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 8888:8080

Depending on how you want to use Dozzle this could totally be optional. You may want to have it running all the time, depending on your needs. For me, I prefer to only have it running when I’m doing local development. Also, by having it as part of my Compose file, it will get cleaned up with I issue a docker-compose downcommand.

根据您要使用Dozzle的方式,这可能完全是可选的。 根据您的需要,您可能希望它一直运行。 对我来说,我更喜欢只在进行本地开发时才运行它。 另外,通过将其作为Compose文件的一部分,可以通过发出docker-compose down命令来清理它。

结语 (Wrapping Up)

By now you should see the power of having Dozzle in your toolkit. Whether you’re using Compose or not, if you’re doing Docker development locally, you need this container! This has been an amazing boost to my productivity, especially when working with several running containers at once. It should be noted that this is a local development tool, and is not meant for a production environment. From their website: “While dozzle should work for most, it is not meant to be a full logging solution. For enterprise applications, products like Loggly, Papertrail or Kibana are more suited.”

现在,您应该看到在工具包中包含Dozzle的功能。 无论您是否使用Compose,如果您在本地进行Docker开发,都需要这个容器! 这极大地提高了我的生产率,尤其是在同时使用多个运行中的容器时。 应该注意的是,这是本地开发工具,并非针对生产环境。 从他们的网站上:“虽然喷嘴最适合大多数情况,但这并不意味着它是完整的日志记录解决方案。 对于企业应用程序, LogglyPapertrailKibana之类的产品更合适。”

翻译自: https://medium.com/swlh/upgrade-your-local-docker-compose-development-with-dozzle-4d3db1082220

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值