Docker instantly builds a local development environment

The public account "Ancient Kite" focuses on back-end technology, especially Java and its surrounding ecology.

Personal blog: www.moonkite.cn

Hello everyone, I am Kite

Sometimes we need to build a development environment locally, such as when learning new technologies. Or sometimes the company's project needs to build a similar set locally to facilitate debugging and modification.

The development environment may include MySQL, Redis, Nginx, MQ, Elasticsearch, etc. Today I use MySQL, and tomorrow I will use PostgreSQL. If you install everything locally, it is not impossible, but it will be very troublesome. , and sometimes the installation will encounter various problems, which is very uncomfortable.

Is there any quick and elegant way?

It is the Docker we are talking about today. Up to now, Docker is no longer a new technology, and it can even be said to be a very common and popular technical solution.

I remember that I first started using Docker in 2015, and it has been almost 8 years now. I can only sigh, how time flies!

During that time, I also gave Docker Amway to many classmates and colleagues. I just told them: "It is really convenient to set up a local development environment for this thing." Thought-of.

If you are a veteran developer, I believe you have already used it. And if you are a novice, you should use it as soon as possible. Not only can you save time in setting up the environment, but you can also learn. After all, containerized deployment methods are very common now, and it is very necessary to know more about it.

Docker principles and basic commands

Fundamental

To use any kind of technology, you must first understand its technical principle and know it well.

Docker uses the Go language launched by Google for development and implementation. Based on technologies such as cgroup and namespace of the Linux kernel, and Union FS such as OverlayFS, it encapsulates and isolates processes, which belongs to the virtualization technology at the operating system level. Because the isolated process is independent of the host and other isolated processes, it is also called a container.

Docker 经常拿来和虚拟机来比较,因为它们两个的用处和用法都很相似,就是在一台实体机上启动多个虚拟系统(暂且这么说)。如果基于使用的层面来讲,你完全可以把 Docker 当做虚拟机来用,然而实际的底层技术原理是完全不一样的。

假设你现在变身了,站在了 Docker 和 虚拟机的内部,从里面向外看,发现虚拟机有自己的 CPU(虚拟CPU)、内存、硬盘,再往外才是宿主机的 CPU、硬盘、内存等。而如果是在Docker内部向外看,发现你无论站在当前实体机的哪个容器里,看到的都是宿主机的 CPU、硬盘、内存等。说明 Dokcer 容器是直接拿宿主机的资源当自己的用,所以每个容器的硬件配置都是一样的,而虚拟机是完全虚拟出来一套。

基础命令

Dokcer 的命令有一大堆,但是常用的就那几个。

镜像相关

获取镜像

docker pull 镜像名称

查看镜像列表

docker image ls

容器相关

查看容器

docker ps -a

新建并启动容器

sudo docker run -t -i ubuntu:12.04  /bin/bash

进入容器

docker exec -it 容器id bash

因为本文不是 Docker 的教程,所以只是抛砖引玉,具体的教程和文档网上到处都是。

安装 Docker Desktop

接下来开始本地实践了,用 Docker 快速搭一套本地环境。

有一些 Docker 的可视化客户端可以安装,帮我们更方便更直观的管理镜像、容器,当然如果你很厉害的话,完全用命令行也不是不行。

我就没这么厉害了,那些参数根本记不住,所以必须要装客户端。我安装的是 Docker Desktop,官方客户端,支持 Mac、Linux、Windows,我大部分时候用 Mac,有时候也要用 Windows,所以Docker Desktop是不二选择。

如果你只用 Mac 的话,也可以安装 OrbStackorbstack.dev/),据说比 Docker Desktop 要流畅一些。

下载安装

访问 Docker Desktop 官网 www.docker.com/products/do…

763shots_so

下载完一键安装就可以了,Docker 环境直接帮你装好了,省心省事儿。下面是它的控制台界面。左侧导航可以查看镜像列表、容器列表、映射的Volumes。

我们在命令行输入 docker -v,如果出现正确的版本号,说明 docker服务已经安装正常了。

Docker version 24.0.2, build cb74dfc

接下来有一件事儿是必须要做的,那就是设置国内镜像源,道理大家都懂,你要是用国外的镜像源,一天都不一定能搞下来一个。

点击「设置」按钮,在左侧选择「Docker Engine」

,然后在右侧的配置框中添加如下配置,加到最下面即可:

"registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://cr.console.aliyun.com/"
  ]

加完后,点击「Apply & restart」,这时候会重启 Docker 服务。

安装一个服务

MySQL、Nginx、Redis、Zookeeper 这些都是服务,每一个都可以按照这种方式安装,不管你的项目需要多少个服务,一个个安装就好了,很快的呦。

正好我最进要学一下 PostgreSQL,我就直接启动一个 docker 容器了,不在本地安装了。当然了,像数据库这种I/O型的应用是不建议容器化的,但是本地开发测试无所谓。

在这个搜索框输入关键词,查找对应的镜像,我在这里输入关键词 PostgreSQL。如果安装 Nginx 那就是输入 Nginx,以此类推。

search

看,出来了一大堆,我一般都是找第一个,也就是下载量最大的这个。

可以 pull 或者 run,pull 是拉取镜像,run 是拉取镜像+启动容器。

我一般都是直接 run,点一下 run,等一会儿,因为前面已经设置了国内镜像源,所以速度很快,十几秒钟。(根据网速快慢不同,因为要下载镜像文件,有的应用有上百M)

下载完之后,因为选的是 run,所以直接弹出了启动参数。

设置镜像名称

容器名称(Container name)就是为了我们一看到它就知道是干什么的,用来方便管理的,我就直接命名为 PostgreSQL了。

设置端口映射(Ports)

服务本身有自己的端口,例如 MySQL 是3306,这是服务本身在容器内的启动端口,但最终我们要通过宿主机去访问服务,所以要将这个容器内的端口绑定到一个宿主机端口,这叫做端口映射,这样一来,我们就可以通过宿主机的端口访问到容器内的端口了。比如我将 MySQL 容器的 3306 端口映射到宿主机的 13306 端口,之后我在设置数据库连接的时候,就用本机 IP:13306就能访问 MySQL 容器了。

在我这个 PostgreSQL 容器上,我将本机的 15432和容器的 5432端口绑定,之后就可以用 15432做连接端口了。

目录映射(Volumes)

很多服务都会用到存储目录,但是容器本身就在宿主机上,所以需要将服务在容器内的目录映射到宿主机的目录上,这叫目录映射。例如将 Nginx 容器的 /etc/nginx目录映射到宿主机的 /apps/nginx/目录上,那之后我在宿主机访问 /apps/nginx目录时,就能看到 Nginx 容器的配置文件了。

在我这个PostgreSQL 容器上,我将 /etc/postgresql/postgresql.conf配置文件映射到了我本地的一个目录上。

环境变量

一个服务启动可能会用到启动变量,这些变量可以通过环境变量的方式进行配置。例如,我们启动一个 Java jar 包,要配置 JVM 相关的参数,这些参数就可以放到环境变量中,供启动的时候使用。

因为 PostgreSQL 需要密码,所以这里就设置一个密码的环境变量 POSTGRES_PASSWORD

都设置好之后,点击run按钮,启动容器。

Then we see that the container has been started, and corresponding logs are printed out.

Then click on the left Containersto see the started container and some basic configurations.

Then you can connect through the client.

In addition, you can also view the internal files, status, configuration information, logs, etc. of the container in the container details, and enter the command line.

at last

It may be slow for the first time, but it will be very fast after you get familiar with it. It is much faster than installing and configuring directly locally, and some software installation and configuration are very cumbersome and will waste a lot of unnecessary time.

There are also many open source frameworks and applications that provide docker versions. After mastering this method, you can quickly install these frameworks and applications for verification and use, and the user experience will directly increase by an order of magnitude.

Guess you like

Origin juejin.im/post/7260148000906756153