设置容器hostname
容器在创建之后不能通过修改hosts文件改变hostname,hostname命令也不可用,需要重新创建容器,在创建容器时(使用docker run
命令),使用-h
参数来表明hostname。
如我创建slave节点容器时,sudo docker run -h slave -it --name slave ubuntu /bin/bash
docker start
命令没有-h/-hostname参数,因此不能修改正在运行的容器的hostname。
保存容器状态(commit)
查看正在运行的容器sudo docker ps
,查到要保存的container id,commit之:
sudo docker commit <container id> <image name>
如sudo docker commit 641b8af48c92 slave
再次启动容器,sudo docker run -h slave -it -v /home/zdt/Downloads/:/opt/downloads --name slave1 slave /bin/bash
环境变量的改动要放在
~/.bashrc
中。若放在/etc/profile
里重启容器会失效,需要再source一边。
容器挂载本地目录
貌似在docker run中有什么写错了就还是直接删除实例好 = =
为了把本机文件放到container里,我尝试了用ssh链接它。但是有人说ssh container不好,更好的方案是和本机共享文件夹,语法如下
sudo docker run -it -v /home/zdt/Downloads/:/opt/downloads --name slave ubuntu /bin/bash
(把本机的/home/zdt/Downloads文件夹和容器的/opt/downloas共享)
欣然去文件夹下ls的我遇到了permission denied错误无情打脸,原来是本机的selinux防火墙从中作梗(fedora),于是su -c "setenforce 0"
,临时关闭防火墙,容器中即可访问本机文件夹。(永久关闭防火墙见参考链接)
运行ubuntu镜像容器
sudo docker run --name master -i -t ubuntu /bin/bash
To run an interactive shell in the Ubuntu image:
$ docker run -i -t ubuntu /bin/bash
The -i flag starts an interactive container. The-t flag creates apseudo-TTY that attachesstdin andstdout.
To detach thetty without exiting the shell, use the escape sequence Ctrl-p +Ctrl-q . The container will continue to exist in a stopped stateonce exited. To list all containers, stopped and running, use thedocker ps -a command.
参考
Quickstart Docker Engine
Docker学习---挂载本地目录
CentOS7中Docker文件挂载,容器中没有执行权限
Fedora关闭/禁用SELinux三种方法
docker hostname
Docker的save和export命令的区别