Docker 中搭建开发环境

发布于 2024-07-10 14:57:31 字数 3989 浏览 21 评论 0

由于 docker 有很好的隔离性,所以开发使用最适合不过。windows 下配合 boot2docker 轻松击败重量级虚拟机。

由于是开发环境,我们首先需要将 docker container 开启 ssh 服务,以便随时登录到上面查看修改配置,之后再去搭建真正的开发环境。所以我们可以先创建一个开启了 ssh 服务的 image 来作为 base image,然后在它的基础之上搭建不通的开发环境。

创建镜像需要使用到 Dockerfile,Dockerfile 的具体语法可以到 官网 去查看,这里不详细介绍。

先上 Dockerfile 内容:

# sshd
# VERSION 1.0.0

FROM ubuntu:14.04
MAINTAINER yaxin <yaxin.me@gmail.com>

# make sure the package repository is up to date
RUN sed -i "s/archive.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y openssh-server
# Config ssh server
RUN mkdir -p /var/run/sshd
RUN sed -i "s/^PermitRootLogin without-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
RUN sed -i "s/^#GSSAPIAuthentication no/GSSAPIAuthentication no/g" /etc/ssh/sshd_config
RUN echo "UseDNS no" >> /etc/ssh/sshd_config
RUN echo 'root:root' | chpasswd

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

新建文件夹并将上面的代码保存为以Dockerfile命名的文本文件。

然后执行

[yaxin@ubox sshd]$ls
Dockerfile
[yaxin@ubox sshd]$docker build -t ubuntu:base .

来创建镜像。下面来稍微解释一下 Dockerfile 的内容

FROM

必须是 Dockerfile 的第一句,之后的操作都是在这个镜像之上进行的。如果你没有,那要先下载, docker pull ubuntu ,如果无法下载,翻墙吧。如果你有国外 vps,那么你可以在 vps 上 docker pull ubuntu 下载该镜像然后 docker save ubuntu:14.04 | bzip2 -9 -c > ubuntu_14.04.tar.bz2 ,然后下载 ubuntu_14.04.tar.bz2 到本机,运行 bzip2 -d -c < ubuntu_14.04.tar.bz2 | docker load 导入镜像,我就是这么干的。

RUN

执行 shell 命令。

ADD

将外部文件导入到镜像中。

EXPOSE

将 22 端口‘暴漏’出来,以便在外部访问该端口。没有的话,将无法从外部(非运行 docker 的主机)ssh。

CMD

RUN 一样,也是执行 shell 命令,不同的是 RUN 是在创建镜像的时候执行,而 CMD 是在每次执行 docker run image 的时候执行。

build 完镜像后,我们执行 docker images 会发现多了一个镜像

[yaxin@ubox sshd]$docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu base 615ea4d4c1e0 2 weeks ago 396.4 MB
ubuntu 14.04 ad892dd21d60 4 weeks ago 275.5 MB

然后启动镜像

[yaxin@ubox sshd]$docker run -d -P --name="sshd" ubuntu:base
c2518b7cf9289567c258171e93462bdb7fffe873bf2251d7086cd97e03e3ee57
[yaxin@ubox sshd]$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2518b7cf928 ubuntu:base /usr/sbin/sshd -D 4 seconds ago Up 3 seconds 0.0.0.0:49153->22/tcp sshd

然后 ssh 49153 端口

[yaxin@ubox sshd]$ssh root@localhost -p 49153
The authenticity of host '[localhost]:49153 ([::1]:49153)' can't be established.
ECDSA key fingerprint is c4:a5:26:07:8b:72:97:40:f2:42:fa:35:9a:7c:d0:79.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:49153' (ECDSA) to the list of known hosts.
root@localhost's password:
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-58-generic x86_64)

* Documentation: https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@c2518b7cf928:~#

自此,一个装有 ssh 服务的可以运行的 base image 就创建成功了。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

烟凡古楼

暂无简介

文章
评论
26 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文