前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >How to build your own ubuntu image with docker?

How to build your own ubuntu image with docker?

作者头像
王亚昌
发布2018-08-03 15:55:55
3270
发布2018-08-03 15:55:55
举报
文章被收录于专栏:王亚昌的专栏王亚昌的专栏

一. Build a ubuntu image and install sshd

1. Pull ubuntu

代码语言:javascript
复制
 docker pull ubuntu:14.04

2. Create Dockerfile

代码语言:javascript
复制
FROM       ubuntu:14.04

#update
RUN apt-get update

#install gcc
RUN apt-get install -y gcc

#install vim
RUN apt-get install -y vim

#install sshd
RUN apt-get install -y openssh-server
RUN mkdir /var/run/sshd

#create user
RUN useradd admin
RUN echo 'admin:admin' |chpasswd 

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

3. Build image

docker build -t ubuntu-sshd-admin .

Build then check image.

代码语言:javascript
复制
$ docker image ls
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
ubuntu-sshd-admin      latest              82a9040e58aa        14 minutes ago      380MB

4. Run 

docker run -d -p 222:22 ubuntu-sshd-admin

代码语言:javascript
复制
docker ps
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                 NAMES
d87645ff3fa2        ubuntu-sshd-admin   "/usr/sbin/sshd -D"   7 minutes ago       Up 7 minutes        0.0.0.0:222->22/tcp   unruffled_mirzakhani

5. Client test

ssh admin@127.0.0.1 -p 222

Use admin:admin to login, then you will find sshd is already started.

代码语言:javascript
复制
$ ps -elf | grep sshd

4 S root         1     0  0  80   0 - 15346 -      04:05 ?        00:00:00 /usr/sbin/sshd -D

4 S root        22     1  0  80   0 - 23138 -      04:13 ?        00:00:00 sshd: admin [priv]  

5 S admin       33    22  0  80   0 - 23138 -      04:13 ?        00:00:00 sshd: admin@pts/0   

0 S admin       37    34  0  80   0 -  2219 -      04:14 pts/0    00:00:00 grep sshd

$ 

二. Install more software

1. Create new Dockerfile

If you need more software in your own system, you can create more Dockerfile and build update version of image.

For example, if you want to install vim, you can write a new Dockerfile.vim

代码语言:javascript
复制
FROM      ubuntu-sshd-admin 

#install vim
RUN apt-get install -y vim

And now we have two dockerfiles.

2. Build a new version

docker build -t ubuntu-sshd-admin:0.1 .  -f Dockerfile.vim

By exec the cmd above, we can build a new version of ubuntu-ssh-admin, we can check by this:

代码语言:javascript
复制
$  docker image ls
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
ubuntu-sshd-admin      0.1                 dbea46204d20        8 minutes ago       414MB
ubuntu-sshd-admin      latest              82a9040e58aa        3 hours ago         380MB

Then we can restart the container and see vim is already installed in the ubuntu.

docker run -d -p 222:22 ubuntu-sshd-admin:0.1

三. Create a volume for ubuntu

1. create a volume

代码语言:javascript
复制
wangyachangdeMacBook-Pro:docker wangyachang$ docker volume create ubuntu
ubuntu
wangyachangdeMacBook-Pro:docker wangyachang$ docker volume inspect ubuntu
[
    {
        "CreatedAt": "2018-06-21T07:31:06Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/ubuntu/_data",
        "Name": "ubuntu",
        "Options": {},
        "Scope": "local"
    }
]

2. start ubuntu

Then we can start the ubuntu image by mount /world to the volume created before.

代码语言:javascript
复制
wangyachangdeMacBook-Pro:ubuntu wangyachang$ docker run -d -p 222:22 --mount type=volume,source=ubuntu,target=/world ubuntu-sshd-admin:0.2

da637a55cb71589f58397a446f00bd5703e7d219e6f12250062c7b13d1dbba48

 We can can creat a file in the /world directory.

代码语言:javascript
复制
root@1c67af3a9a6e:/# touch /world/data.txt

root@1c67af3a9a6e:/# ls /world/data.txt

/world/data.txt

因为作者的docker是安装在mac上的,mac上的docker是在一个虚拟机中运行的,所以这里访问下面这个目录是不存在的。

代码语言:javascript
复制
/var/lib/docker/volumes/ubuntu/_data

/Applications/Docker.app/Contents/MacOS/com.docker.driver.amd64-linux

四. 挂载一个主机目录作为数据卷

使用 -v 标记也可以指定挂载一个本地的已有目录到容器中去作为数据卷:

docker run -d -p 222:22 -v /Users/wangyachang/Documents/docker/ubuntu/ubuntu_data:/world_data ubuntu-sshd-admin:0.2

使用上面的命令,会把本机ubuntu_data目录挂载到容器的world_data,实现了所有的数据同步。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年06月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档