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