How to build your own ubuntu image with docker?

本文介绍了如何使用Docker构建包含sshd服务的Ubuntu镜像,并安装额外软件如vim。还讲解了创建数据卷、挂载主机目录、保存容器为新镜像以及查看容器日志的方法,适合Docker初学者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. Build a ubuntu image and install sshd

1. Pull ubuntu

 docker pull ubuntu:14.04

2. Create Dockerfile

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.

$ 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

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.

$ 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

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:

$  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

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.

 

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.

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

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

/world/data.txt

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

/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,实现了所有的数据同步。


参考文献:

https://yeasy.gitbooks.io/docker_practice/introduction/

五. 保存容器到镜像

基于已有的docker容器,做一新的dokcer image.

$ docker commit <container_id> <image_name>

如何看启动容器日志: 
$ sudo docker logs -f -t 容器名

六. FAQ

mac下docker的镜像保存位置:/Users/wangyachang/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值