Advertisement

How to build your own ubuntu image with docker?

阅读量:

一. 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 require additional software within your system, you may opt to create more Dockerfiles and construct updated versions of the 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 running This command, we can install a new version of ubuntu-ssh-admin, and this allows us to verify.

复制代码
 $  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

We can try rebooting the container to observe whether vim is installed on your Ubuntu system.

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 initiate the Ubuntu image by assigning it to the volume previously created.

复制代码
 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 标识符同样可以配置将本地已有的目录挂载到容器中作为存储卷:

启动容器并在指定端口上运行映射本地端口到容器中将文件夹 /Users/wangyachang/Documents/docker/ubuntu/ubuntu_data 挂载到 /world_data Ubuntu SSH代理服务版本号为ubuntu-sshd-admin 的版本号为0.1版(截至发布前)

通过指定的命令操作,本机位于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...中

全部评论 (0)

还没有任何评论哟~