docker容器构建



If you are not a Docker expert, like most people, the best possible way to learn is to start up a terminal and mess around in an empty container. One example is to open up an Ubuntu image, install python, and write the iconic “Hello World” script. Doing this all within the Ubuntu image, Docker will start to make sense.

如果您像大多数人一样不是Docker专家,那么最好的学习方法是启动终端并在一个空容器中乱扔。 一个示例是打开Ubuntu映像,安装python,然后编写标志性的“ Hello World”脚本。 在Ubuntu映像中完成所有这些操作,Docker将变得有意义。

(This is the fun way to learn Docker: Start from a base image and from within, install all necessary dependencies to get your program to work.)

(Story)

Say you have a simple Golang application that imports other Go files. As expected, we will always import Go files via the import statement with respect to our set GOPATH:

假设您有一个简单的Golang应用程序,可以导入其他Go文件。 如预期的那样,我们将始终通过导入语句针对我们设置的GOPATH导入Go文件:

github.com/keithkfield/pg_api/lp_routes

github.com/keithkfield/pg_api/lp_routes

With the database package imported, we can easily import exportable functions. Here, I will define a route to add orders to the database(Postgres):

导入数据库包后,我们可以轻松导入可导出功能。 在这里,我将定义一条将订单添加到数据库的路线(Postgres):

http.HandleFunc(“/order”, lp_routes.AddOrder)

http.HandleFunc(“ / order”,lp_routes.AddOrder)

With this basic application created, its time to place it into a Dockerfile, so we can easily ensure that anyone that clones my repo can run the server without dependency errors.

创建了这个基本应用程序之后,就可以将其放入Dockerfile中了,因此我们可以轻松地确保任何克隆我的repo的人都可以运行服务器而不会出现依赖项错误。

For such an easy application like this, the straightforward Dockerfile would just start the Golang image, copy the content of the directory into the container, and then run go run main.go. Many languages do not worry about where we place our working directory. Both Javascript and Python can run the main file anywhere we decide, usually we just place all of our files in an arbitrary ‘/app’ directory. So coming from other languages will harm us is making a successful Dockerfile for Golang applications.

对于这样的简单应用程序,简单的Dockerfile只会启动Golang映像,将目录的内容复制到容器中,然后运行go runmain.go。 许多语言不担心我们将工作目录放置在何处。 Javascript和Python都可以在我们决定的任何位置运行主文件,通常我们只是将所有文件放在任意的“ / app”目录中。 因此,来自其他语言的危害将危害我们,为Golang应用程序制作成功的Dockerfile。

(The quick: a Golang Dockerfile differs from other languages because placing all code in an arbitrary directory will not allow your app to correctly import other Go packages. To fix this issue, we must place all of our code in the newly created GOPATH —)

WORKDIR github.com/keithkfield/pg_api

WORKDIR github.com/keithkfield/pg_api

The way that I figured this out was by playing with an empty Golang container. The Docker command is —

我发现这个问题的方法是玩一个空的Golang容器。 Docker命令是-

docker run -dit — name dev golang

docker run -dit —名称为dev golang

The flag -d allows the container to run in the background, -it is so that there is an interactive terminal always up because if these flags were not called, the empty container will just stop. We named this new container dev, also. To actively play within this container we need the command —

标志-d允许容器在后台运行,-d则始终有一个交互式终端,因为如果不调用这些标志,则空容器将停止。 我们也命名了这个新的容器开发者。 要在此容器中积极发挥作用,我们需要以下命令-

docker exec -it dev bash

docker exec -it开发bash

This allows us to enter the container and work within its file system. Again, -it beings out the terminal.

这使我们可以进入容器并在其文件系统中工作。 再次,-它是终端。

I was able to solve my issued of my files not being able to locate other file by running the command ‘echo $GOPATH’ that returned the simple ‘/go’. Although this was an obvious fact from go applications, this problem stumped me for weeks. I mistakenly imported all of my files outside of the GOPATH, so my files were not able to be found and compiled.

通过运行返回简单的'/ go'的命令'echo $ GOPATH',我能够解决我的文件发布无法定位其他文件的问题。 尽管这对于go应用程序来说是显而易见的事实,但这个问题困扰了我数周。 我错误地将所有文件导入了GOPATH之外,因此无法找到和编译我的文件。

From within the container, its pretty easy to use the linux’s package manager (yum, apt-get, etc.) to install needed packages. Once you are able to run your program within this sandbox container, the last task is to just recount all of your steps into a Dockerfile. Although this Dockerfile was not made with efficiency in mind, it serves as the base script to get your program off the ground and leaves room to make efficient changes that can drastically reduce the size of the image.

从容器内部,使用Linux的软件包管理器(yum,apt-get等)安装所需的软件包非常容易。 一旦能够在此沙箱容器中运行程序,最后的任务就是将所有步骤重新计入Dockerfile中。 尽管此Dockerfile并非出于效率考虑,但它是使程序启动的基本脚本,并留有空间进行有效更改,从而可以极大地减小映像大小。



翻译自: https://medium.com/@kirtfieldk/the-fun-way-to-build-docker-containers-ae671ebe83d5

docker容器构建