by Ryan Gordon
通过瑞安·戈登(Ryan Gordon)
在Raspberry Pi上设置Docker的简单方法 (The easy way to set up Docker on a Raspberry Pi)
Docker is a very useful tool for running containerized versions of popular applications (such as databases) or setting up some IoT service on an internet-connected device.
Docker是运行常用应用程序的容器化版本(例如数据库)或在连接互联网的设备上设置某些IoT服务的非常有用的工具。
But installing Docker can sometimes be a hassle if it needs to be done a number of times across different computers. The silver lining, however, is that there is a handy trick hidden away in the Docker docs detailing how to install Docker with just two lines in the terminal.
但是,如果需要在不同的计算机上多次安装Docker,有时可能会很麻烦。 然而,一线希望是,在Docker文档中隐藏了一个方便的技巧,详细介绍了如何在终端中仅用两行安装Docker。
Yes, you heard right! With just two lines you can load and install Docker.
是的,您没听错! 仅需两行,您就可以加载和安装Docker。
Installing Docker can be handled by a bash script which will automate the entire installation. Docker provides such a script at get.docker.com
. The first command will be consuming this URL, looking for a file called get-docker.sh
. Once gotten, we just run the script. The two commands can be chained together to form a statement like:
可以通过bash脚本处理Docker的安装,该脚本将自动完成整个安装过程。 Docker在get.docker.com
提供了这样的脚本。 第一个命令将使用此URL,查找名为get-docker.sh
的文件。 一旦获得,我们就运行脚本。 这两个命令可以链接在一起形成如下语句:
curl -fsSL get.docker.com -o get-docker.sh && sh get-docker.sh
Now you have Docker installed, and the installation only took two lines.
现在您已经安装了Docker,并且安装仅需两行。
As you just saw, the two commands above are chained together using the ‘&&’ operator. This means that the commands will run one after the other, but can be typed on the same line.
如您所见,上面的两个命令使用'&&'运算符链接在一起。 这意味着命令将一个接一个地运行,但可以在同一行上键入。
One small issue, though, is that you might experience difficulty running Docker commands without sudo. This can be fixed, but it’ll take a few more lines.
但是,一个小问题是,如果没有sudo,运行Docker命令可能会遇到困难。 可以解决此问题,但需要多花费几行。
如何设置Docker始终不使用sudo运行 (How to set up Docker to run without using sudo all the time)
I discovered this solution on AskUbuntu after encountering the problem. Let’s go through it now.
遇到问题后,我在AskUbuntu上发现了此解决方案。 让我们现在来看一下。
共有3个步骤: (There are 3 steps:)
- Add the Docker group if it doesn’t already exist: 添加Docker组(如果尚不存在):
sudo groupadd docker
2. Add the connected user “$USER” to the docker group. Change the user name to match your preferred user if you do not want to use your current user:
2.将连接的用户“ $ USER”添加到泊坞窗组。 如果您不想使用当前用户,请更改用户名以匹配您的首选用户:
sudo gpasswd -a $USER docker
3. From here you have two options: either logout and then log back in, or run newgrp docker
for the changes to take effect.
3.在这里,您有两个选择:注销然后重新登录,或者运行newgrp docker
使更改生效。
You should now be able to run Docker without sudo. To test, try this:
您现在应该可以在没有sudo的情况下运行Docker。 要测试,请尝试以下操作:
docker run hello-world
If it worked, you should see a lovely message from Docker:
如果可行,您应该会从Docker看到一条可爱的消息:
Again, all credit for this solution goes to this great AskUbuntu answer I found. Without typing sudo all the time, commands will be much easier to work with.
同样,此解决方案的全部功劳归功于我找到的这个出色的AskUbuntu答案。 无需一直输入sudo,命令将更容易使用。
但是,等等,还有更多! (But wait, there’s more!)
What if you want docker-compose also? You could try and install the docker-compose source similarly to how we installed Docker. One interesting approach I found on the Google Cloud Engines docs is that you can actually run docker-compose as a container itself!
如果您还想要docker-compose怎么办? 您可以尝试像安装Docker一样安装docker-compose源代码。 我在Google Cloud Engines文档中发现的一种有趣的方法是,您实际上可以将docker-compose作为容器本身运行!
Doing so means you have a disposable installation of docker-compose which will be used to compose your services. At any point, you can throw it away and repeat the steps for a fresh docker-compose.
这样做意味着您可以一次性安装docker-compose,它将用于组成您的服务。 在任何时候,您都可以将其丢弃,并重复执行新的docker-compose的步骤。
First step will be running docker-compose as a container and giving it access to volumes.
第一步将作为容器运行docker-compose,并授予其访问卷的权限。
docker run \ -v /var/run/docker.sock:/var/run/docker.sock \ -v "$PWD:/rootfs/$PWD" \ -w="/rootfs/$PWD" \ docker/compose:1.13.0 up
Next, make an alias to docker compose:
接下来,为docker compose做一个别名:
echo alias docker-compose="'"'docker run \ -v /var/run/docker.sock:/var/run/docker.sock \ -v "$PWD:/rootfs/$PWD" \ -w="/rootfs/$PWD" \ docker/compose:1.13.0'"'" >> ~/.bashrc
Then reload bash:
然后重新加载bash:
source ~/.bashrc
Now you have full access to docker-compose. The alias defined above means that rather than having to type out docker commands when you want to use the compose container, you can just use ‘docker-compose’ as you normally would.
现在您可以完全访问docker-compose。 上面定义的别名意味着,当您要使用compose容器时,不必不必键入docker命令,而可以像平常一样使用“ docker-compose”。
关于RPi上Docker的重要通知 (Important Notice about Docker on RPi)
Raspberry Pi’s use ARM archetecture, and as a result wont be compatible with all containers out of the box. Images will need to be built from an ARM base image.
Raspberry Pi使用ARM架构,因此无法与所有开箱即用的容器兼容。 映像将需要从ARM基本映像构建。
You can see this in action by running a containerized Redis instance on a Raspberry Pi (which is quite relevant to an upcoming series I’m writing). Doing so will require working with a base image. Provided we use an ARM-compatible image, no problems should arise. The issue is finding a well-maintained one.
您可以通过在Raspberry Pi上运行容器化的Redis实例(这与我正在编写的下一个系列非常相关)来看到实际的效果。 这样做将需要使用基础图像。 只要我们使用兼容ARM的映像,就不会出现问题。 问题是找到一个维护良好的设备。
If you enjoyed this article, give it a clap.
如果您喜欢这篇文章,请拍一下。
I have other posts on my page related to Microservices, Ionic, and more.
我的页面上还有其他与微服务,Ionic等相关的帖子。
翻译自: https://www.freecodecamp.org/news/the-easy-way-to-set-up-docker-on-a-raspberry-pi-7d24ced073ef/