Docker - 开发、调试、部署
为什么
如何
概括
由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!
为什么
作为一名参与多个项目的开发人员,我希望找到一种方法——
- 我的设置时间会尽可能快
- 与团队成员分享我的代码和项目设置非常容易。
- 我希望我的开发环境尽可能接近生产环境。
所以我开始寻找实现方法。一开始我想到的是Git,但问题在于Git它的设置时间不够理想——
如何自动配置Environment Variables或同步Git hooks我的项目?如果我的项目使用了新的编程语言或版本怎么办?我不想在本地电脑上安装和管理版本。
我不想为任何解决方案付费。
后来我开始考虑将Docker其用作我的开发环境。我可以将其运行Docker为“个人开发服务器”,并在其中配置我需要的一切Docker Image——编译器/解释器、环境变量、依赖项等等。编码完成后,我可以将相同的配置应用Docker image到我的生产环境中。
什么Docker是
“Docker是一个用于开发、发布和运行应用程序的开放平台。Docker它使您能够将应用程序与基础设施分离,从而快速交付软件。借助Docker,您可以像管理应用程序一样管理基础设施。通过利用Docker’s快速发布、测试和部署代码的方法,您可以显著缩短编写代码到在生产环境中运行代码之间的延迟。”您可以点击此处阅读官方文档了解更多
信息。Docker
如何
建造
我以此为Dockerfile模板构建我自己的系统,我安装它是为了像使用开发服务器一样使用它,并与我的队友共享。Docker Imageopenssh-server
FROM python:3.6
ARG GIT
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:MYSUPERSECRETROOTPASSWORD' | chpasswd
# According to your linux distribution this line my differ
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
RUN echo ' \n\
git clone $GIT ~/code \n\
cd ~/code \n\
git pull \n\
pip install -r requirements.txt' > ./run.sh
# Also configure hooks, environment variables and more
RUN sh ./run.sh
RUN echo '/usr/sbin/sshd -D' >> ./run.sh
CMD ["sh", "./run.sh"]
运行命令:
> docker build --build-arg GIT=GITREPURL -t my_cool_docker .
执行此操作时,docker build它将启动git clone我的项目并安装pip依赖项以及执行我需要做的任何操作。此外,我还定义了./run.sh每次使用时都要执行的同一个文件,docker run以便使我的 Docker 镜像保持最新状态,包含最新的提交等等。
跑步
我使用此命令来运行我的镜像。
> docker run -dP my_cool_docker
37d6e53cb27396467a10c7361d319d28d0197a7b5dc7347bb39c251dff7403dc
> docker port 3
22/tcp -> 0.0.0.0:32768
> ssh root@localhost -p 3276
The authenticity of host '[localhost]:32768 ([::1]:32768)' can't be established.
ECDSA key fingerprint is SHA256:z4x3yWVSJZAoswgEa0utt5jSv0Mt0Ex6sMY8a4CFCnE.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:32768' (ECDSA) to the list of known hosts.
root@localhost's password:
Linux 37d6e53cb273 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
root@37d6e53cb273:~# ls -l
total 4
drwxr-xr-x 1 root root 4096 Dec 14 09:45 code
如您所见,我运行了imageusingdetach标志和expose all端口,
然后使用ssh暴露的端口访问 localhost,输入您的超级秘密 root 密码,我们就拥有了一个个人开发服务器,它实际上docker container运行在您的个人电脑上!
概括
优势
- 一次性设置即可满足我的所有需求
- 开发环境和生产环境相同
- 易于与队友分享
缺点
container如果车辆崩溃,您可能会丢失数据。- 适用于在生产中使用容器的用户
尽情享用😋
文章来源:https://dev.to/eranelbaz/docker-dev-debug-deploy-199l喜欢这篇文章吗?请通过Patreon
支持我 ,并订阅我的YouTube 频道。