設(shè)置容器hostname
容器在創(chuàng)建之后不能通過(guò)修改hosts文件改變hostname,hostname命令也不可用,需要重新創(chuàng)建容器,在創(chuàng)建容器時(shí)(使用docker run
命令),使用-h
參數(shù)來(lái)表明hostname。
如我創(chuàng)建slave節(jié)點(diǎn)容器時(shí),sudo docker run -h slave -it --name slave ubuntu /bin/bash
docker start
命令沒(méi)有-h/-hostname參數(shù),因此不能修改正在運(yùn)行的容器的hostname。
保存容器狀態(tài)(commit)
查看正在運(yùn)行的容器sudo docker ps
,查到要保存的container id,commit之:
sudo docker commit <container id> <image name>
如sudo docker commit 641b8af48c92 slave
再次啟動(dòng)容器,sudo docker run -h slave -it -v /home/zdt/Downloads/:/opt/downloads --name slave1 slave /bin/bash
環(huán)境變量的改動(dòng)要放在
~/.bashrc
中。若放在/etc/profile
里重啟容器會(huì)失效,需要再source一邊。
容器掛載本地目錄
貌似在docker run中有什么寫(xiě)錯(cuò)了就還是直接刪除實(shí)例好 = =
為了把本機(jī)文件放到container里,我嘗試了用ssh鏈接它。但是有人說(shuō)ssh container不好,更好的方案是和本機(jī)共享文件夾,語(yǔ)法如下
sudo docker run -it -v /home/zdt/Downloads/:/opt/downloads --name slave ubuntu /bin/bash
(把本機(jī)的/home/zdt/Downloads文件夾和容器的/opt/downloas共享)
欣然去文件夾下ls的我遇到了permission denied錯(cuò)誤無(wú)情打臉,原來(lái)是本機(jī)的selinux防火墻從中作梗(fedora),于是su -c "setenforce 0"
,臨時(shí)關(guān)閉防火墻,容器中即可訪問(wèn)本機(jī)文件夾。(永久關(guān)閉防火墻見(jiàn)參考鏈接)
運(yùn)行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學(xué)習(xí)---掛載本地目錄
CentOS7中Docker文件掛載,容器中沒(méi)有執(zhí)行權(quán)限
Fedora關(guān)閉/禁用SELinux三種方法
docker hostname
Docker的save和export命令的區(qū)別