Style:Machtml
Series:Javamysql
Since:2018-09-10linux
End:2018-09-10nginx
Total Hours:1git
Degree Of Diffculty:5web
Degree Of Mastery:5sql
Practical Level:5docker
Desired Goal:5shell
Archieve Goal:3json
Gerneral Evaluation:3
Writer:kingdelee
Related Links:
http://www.cnblogs.com/kingdelee/
1.
centos6.8安裝:
yum install -y epel-release
安裝出現問題:
Error: docker-ce conflicts with 2:docker-1.13.1-75.git8633870.el7.centos.x86_64
yum remove -y docker-ce.x86_64 docker-ce-cli.x86_64 containerd.io.x86_64
根據文檔描述的去作
https://yq.aliyun.com/articles/110806?spm=5176.8351553.0.0.2e381991PnZGy9
推薦安裝1.10.0以上版本的Docker客戶端,參考文檔 docker-ce
其餘io版本的,是有/etc/sysconfig/docker,須要在裏邊修改鏡像,最新版的,只須要按照下面的方式修改就好了,不須要管那個docker以及其餘多餘的修改
針對Docker客戶端版本大於 1.10.0 的用戶
您能夠經過修改daemon配置文件/etc/docker/daemon.json來使用加速器
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://mm9zx6r4.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --enable docker-ce-edge
啓動docker
sudo systemctl start docker
經過服務啓動docker
sudo systemctl restart docker
安裝後,出現啓動問題,能夠從新刪除後,再次安裝
rm -rf /var/lib/docker
提示:
rm: cannot remove ‘/var/lib/docker/aufs’: Device or resource busy
緣由:
在創建容器的時候作了相應目錄的掛載,沒有卸載,因此Device or resource busy
解決方法:
查找掛載的目錄cat /proc/mounts | grep "docker"
卸載umount /var/lib/docker/aufs
rm -rf /var/lib/docke
若是沒法刪除,就重啓再刪
https://www.cnblogs.com/anliven/p/6218741.html
https://success.docker.com/article/using-systemd-to-control-the-docker-daemon
vim /etc/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --config-file=/etc/docker/daemon.json -H unix://
重載配置
sudo systemctl daemon-reload
重啓鏡像
sudo systemctl restart docker
查看
ps -ef | grep docker
拉鏡像:
docker run hello-world
docker命令
docker images 列出本地主機上的景象
各個選項說明:
REPOSITORY:表示鏡像的倉庫源 TAG:鏡像的標籤 IMAGE ID:鏡像ID CREATED:鏡像建立時間 SIZE:鏡像大小 |
同一倉庫源能夠有多個 TAG,表明這個倉庫源的不一樣個版本,咱們使用 REPOSITORY:TAG 來定義不一樣的鏡像。
若是你不指定一個鏡像的版本標籤,例如你只使用 ubuntu,docker 將默認使用 ubuntu:latest 鏡像
docker search 搜索某個鏡像
docker pull mysql 下載mysql鏡像
docker rmi mysql 刪除某個鏡像
docker rmi -f hello-world
docker rmi -f $(docker images -qa) 級聯刪除全部鏡像
OPTIONS說明(經常使用):有些是一個減號,有些是兩個減號
--name="容器新名字": 爲容器指定一個名稱;
-d: 後臺運行容器,並返回容器ID,也即啓動守護式容器;
-i:以交互模式運行容器,一般與 -t 同時使用;
-t:爲容器從新分配一個僞輸入終端,一般與 -i 同時使用;
-P: 隨機端口映射;
-p: 指定端口映射,有如下四種格式
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort
指定容器名字
docker run --name centos -it 75835a67d134
在容器內
exit,退出並中止容器
ctrl+p+g,退出但不終止容器,若是是iterm下,能夠直接關閉標籤便可
經過 docker ps -n 10,找到前10個建立的容器
儘管是用的同一個鏡像,可是每一個容器都是獨立的,這些獨立的容器,是經過剛剛docker run -it 建立的:
centos,admiring_kalam
能夠經過docker的啓動命令將其啓動
修改容器名字,容器不能重名
docker 容器(服務)重命名只要一個命令就能夠:
docker rename 原容器名 新容器名
docker top 容器id,查看容器內正在運行的進程
docker inspect 6647e43f5912,查看容器內部細節
[ { "Id": "6647e43f5912ce8945e29143f4059afaf3c50f1853a67ca6833bdc2a8da10202", "Created": "2018-10-27T04:07:38.596144727Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 9554, "ExitCode": 0, "Error": "", "StartedAt": "2018-10-27T05:22:04.957537315Z", "FinishedAt": "2018-10-27T05:19:02.144550316Z" }, "Image": "sha256:75835a67d1341bdc7f4cc4ed9fa1631a7d7b6998e9327272afea342d90c4ab6d", "ResolvConfPath": "/var/lib/docker/containers/6647e43f5912ce8945e29143f4059afaf3c50f1853a67ca6833bdc2a8da10202/resolv.conf", "HostnamePath": "/var/lib/docker/containers/6647e43f5912ce8945e29143f4059afaf3c50f1853a67ca6833bdc2a8da10202/hostname", "HostsPath": "/var/lib/docker/containers/6647e43f5912ce8945e29143f4059afaf3c50f1853a67ca6833bdc2a8da10202/hosts", "LogPath": "/var/lib/docker/containers/6647e43f5912ce8945e29143f4059afaf3c50f1853a67ca6833bdc2a8da10202/6647e43f5912ce8945e29143f4059afaf3c50f1853a67ca6833bdc2a8da10202-json.log", "Name": "/centos", "RestartCount": 0, "Driver": "devicemapper", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "shareable", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/asound", "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "DeviceId": "25", "DeviceName": "docker-253:0-34571929-ac887ffa3045a9e8c0146009778996bb55a34e2e730e2baa6ad71dd1cc1f666f", "DeviceSize": "10737418240" }, "Name": "devicemapper" }, "Mounts": [], "Config": { "Hostname": "6647e43f5912", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "ArgsEscaped": true, "Image": "75835a67d134", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20181006", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "d23327c79fd6c87969d0aa190f2e465f5be2fd6a0179a70d7e7c70ef0f13b71d", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/d23327c79fd6", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "03e81113086826176c03190b95513cf19860b3dee30f4b340831630a24ca6baf", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "ca2d34924a01911c8bde199f3850427b3973b6fce3c00fc0ee47f79285c35c80", "EndpointID": "03e81113086826176c03190b95513cf19860b3dee30f4b340831630a24ca6baf", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ]
docker cp 6647e43f5912:t1/a.txt . ,docker cp 容器ID:容器內路徑 目的主機路徑, 將容器的文件拷貝到主機上

attach Attach to a running container # 當前 shell 下 attach 鏈接指定運行鏡像
build Build an image from a Dockerfile # 經過 Dockerfile 定製鏡像
commit Create a new image from a container changes # 提交當前容器爲新的鏡像
cp Copy files/folders from the containers filesystem to the host path #從容器中拷貝指定文件或者目錄到宿主機中
create Create a new container # 建立一個新的容器,同 run,但不啓動容器
diff Inspect changes on a container's filesystem # 查看 docker 容器變化
events Get real time events from the server # 從 docker 服務獲取容器實時事件
exec Run a command in an existing container # 在已存在的容器上運行命令
export Stream the contents of a container as a tar archive # 導出容器的內容流做爲一個 tar 歸檔文件[對應 import ]
history Show the history of an image # 展現一個鏡像造成歷史
images List images # 列出系統當前鏡像
import Create a new filesystem image from the contents of a tarball # 從tar包中的內容建立一個新的文件系統映像[對應export]
info Display system-wide information # 顯示系統相關信息
inspect Return low-level information on a container # 查看容器詳細信息
kill Kill a running container # kill 指定 docker 容器
load Load an image from a tar archive # 從一個 tar 包中加載一個鏡像[對應 save]
login Register or Login to the docker registry server # 註冊或者登錄一個 docker 源服務器
logout Log out from a Docker registry server # 從當前 Docker registry 退出
logs Fetch the logs of a container # 輸出當前容器日誌信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口對應的容器內部源端口
pause Pause all processes within a container # 暫停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server # 從docker鏡像源服務器拉取指定鏡像或者庫鏡像
push Push an image or a repository to the docker registry server # 推送指定鏡像或者庫鏡像至docker源服務器
restart Restart a running container # 重啓運行的容器
rm Remove one or more containers # 移除一個或者多個容器
rmi Remove one or more images # 移除一個或多個鏡像[無容器使用該鏡像纔可刪除,不然需刪除相關容器纔可繼續或 -f 強制刪除]
run Run a command in a new container # 建立一個新的容器並運行一個命令
save Save an image to a tar archive # 保存一個鏡像爲一個 tar 包[對應 load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像
start Start a stopped containers # 啓動容器
stop Stop a running containers # 中止容器
tag Tag an image into a repository # 給源中鏡像打標籤
top Lookup the running processes of a container # 查看容器中運行的進程信息
unpause Unpause a paused container # 取消暫停容器
version Show the docker version information # 查看 docker 版本號
wait Block until a container stops, then print its exit code # 截取容器中止時的退出狀態值
-------------------------------------------
attach Attach to a running container # 當前 shell 下 attach 鏈接指定運行鏡像
build Build an image from a Dockerfile # 經過 Dockerfile 定製鏡像
commit Create a new image from a container changes # 提交當前容器爲新的鏡像
cp Copy files/folders from the containers filesystem to the host path #從容器中拷貝指定文件或者目錄到宿主機中
create Create a new container # 建立一個新的容器,同 run,但不啓動容器
diff Inspect changes on a container's filesystem # 查看 docker 容器變化
events Get real time events from the server # 從 docker 服務獲取容器實時事件
exec Run a command in an existing container # 在已存在的容器上運行命令
export Stream the contents of a container as a tar archive # 導出容器的內容流做爲一個 tar 歸檔文件[對應 import ]
history Show the history of an image # 展現一個鏡像造成歷史
images List images # 列出系統當前鏡像
import Create a new filesystem image from the contents of a tarball # 從tar包中的內容建立一個新的文件系統映像[對應export]
info Display system-wide information # 顯示系統相關信息
inspect Return low-level information on a container # 查看容器詳細信息
kill Kill a running container # kill 指定 docker 容器
load Load an image from a tar archive # 從一個 tar 包中加載一個鏡像[對應 save]
login Register or Login to the docker registry server # 註冊或者登錄一個 docker 源服務器
logout Log out from a Docker registry server # 從當前 Docker registry 退出
logs Fetch the logs of a container # 輸出當前容器日誌信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口對應的容器內部源端口
pause Pause all processes within a container # 暫停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server # 從docker鏡像源服務器拉取指定鏡像或者庫鏡像
push Push an image or a repository to the docker registry server # 推送指定鏡像或者庫鏡像至docker源服務器
restart Restart a running container # 重啓運行的容器
rm Remove one or more containers # 移除一個或者多個容器
rmi Remove one or more images # 移除一個或多個鏡像[無容器使用該鏡像纔可刪除,不然需刪除相關容器纔可繼續或 -f 強制刪除]
run Run a command in a new container # 建立一個新的容器並運行一個命令
save Save an image to a tar archive # 保存一個鏡像爲一個 tar 包[對應 load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像
start Start a stopped containers # 啓動容器
stop Stop a running containers # 中止容器
tag Tag an image into a repository # 給源中鏡像打標籤
top Lookup the running processes of a container # 查看容器中運行的進程信息
unpause Unpause a paused container # 取消暫停容器
version Show the docker version information # 查看 docker 版本號
wait Block until a container stops, then print its exit code # 截取容器中止時的退出狀態值
1.run和start的關係:
實踐中須要注意的幾個地方,建立並啓動容器時候,經過docker run -it -p 主機端口:容器端口,這個端口一旦指定,容器就沒法再去修改。
若是想換端口,就必須從新再run一遍。
每次run都會建立新的容器,因此,若是不須要修改端口的狀況下,想再次開啓容器,須要執行start而不是run。
若是是tomcat,在主機狀況下,經過 localhost:主機端口 能夠訪問到docker的容器端口
好比:指定 docker run -it -p 8088:8080 tomcat,那麼就能夠經過 localhost:8088訪問tomcat,由於docker中tomcat的默認端口是8080
刪除nginx相關的容器
docker rm -f $(docker ps -a | grep nginx | awk '{print $1}')
嘗試改了ip
來源:https://www.cnblogs.com/zhangzeyu/p/6427249.html
進入後修改 nginx.conf
沒有vim時要安裝vim
vim /etc/nginx/nginx.conf
鏡像操做實驗---------------------------
[root@localhost vagrant]# docker exec -it 5475cb7c3fa1 /bin/bash root@5475cb7c3fa1:/usr/local/tomcat/webapps# rm -rf docs/ [root@localhost vagrant]# docker commit -a "lee" -m "remove a docs" 5475cb7c3fa1 tomcat:v1 sha256:bbfa74687c1f9161fcfbe14ee8247445f7ac1a52fc47df46065c625ea8797069 [root@localhost vagrant]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat v1 bbfa74687c1f About a minute ago 463MB tomcat latest 05af71dd9251 10 days ago 463MB nginx latest dbfc48660aeb 11 days ago 109MB centos latest 75835a67d134 2 weeks ago 200MB [root@localhost vagrant]#
nginx啓動-------------------------------------------
參考:https://blog.csdn.net/kim_weir/article/details/80403805
裏邊docker映射nginx時,出現沒法映射的狀況
解決思路
1.本身建立映射的目錄及文件,並拷貝conf
2.修改文件訪問權限
3.修改文件全部者權限
docker run -p 80:80 --name nginx_80_80 -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -d nginx
刪除技巧:docker rm -f $(docker ps -a | grep nginx | awk '{print $1}')
設置共享目錄------------------
本質上是建立數據捲進行共享通訊
docker run -it -v /宿主機絕對路徑目錄:/容器內目錄 鏡像名
docker run -it -v /home/vagrant/test/myfile:/myfile centos
查看容器卷,docker inspect 容器id
-----------------------
可在Dockerfile中使用VOLUME指令來給鏡像添加一個或多個數據卷
VOLUME["/dataVolumeContainer","/dataVolumeContainer2","/dataVolumeContainer3"]
構建鏡像
# volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,--------success1"
CMD /bin/bash
docker build -f 鏡像文件 -t 新的鏡像名
docker build -f /home/vagrant/test/myfile/dockerfile -t centos_v1
經過docker inspect 容器id查看
構建繼承關係-----------
以這個爲鏡像centos_v1爲模板,構建容器centos_t1,centos_t2,centos_t3
docker run -it --name centos_t1 centos_v1 /bin/bash
exit
[root@localhost myfile]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9be7c0facf42 centos_v1 "/bin/bash" 6 minutes ago Exited (0) 25 seconds ago centos_t1
啓動
進入確認有這兩個容器卷
在卷1裏邊添加 centos_t1.txt
建立容器繼承, centos_t2 繼承 centos_t1 經過同一個鏡像centos_v1
docker run -it --name centos_t2 --volumes-from centos_t1 centos_v1
[root@localhost myfile]# docker run -it --name centos_t2 --volumes from centos_t1 centos_v1 unknown flag: --volumes See 'docker run --help'. [root@localhost myfile]# docker run -it --name centos_t2 --volumes-from centos_t1 centos_v1 [root@612a7e2a951a /]# exit exit [root@localhost myfile]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 612a7e2a951a centos_v1 "/bin/sh -c /bin/bash" 12 seconds ago Exited (0) 2 seconds ago centos_t2 9be7c0facf42 centos_v1 "/bin/bash" 12 minutes ago Exited (0) 6 minutes ago centos_t1 32943e151a0d centos "/bin/bash" 6 hours ago Exited (0) 31 minutes ago modest_boyd 10ffd9d1d69d nginx "nginx -g 'daemon of…" 8 hours ago Up 8 hours 0.0.0.0:80->80/tcp nginx_80_80 977213f8852b tomcat:v1 "catalina.sh run" 22 hours ago Exited (143) 11 hours ago tomcat_v1 d78bf34cd7c9 tomcat "catalina.sh run" 30 hours ago Exited (143) 13 hours ago tomcat8088 1a9337aa387e centos "/bin/bash" 34 hours ago Exited (0) 34 hours ago awesome_benz e2e9ba157f7b centos "/bin/bash" 34 hours ago Exited (0) 34 hours ago relaxed_bardeen 6647e43f5912 75835a67d134 "/bin/bash" 37 hours ago Exited (255) 31 hours ago centos dc14127dd454 75835a67d134 "/bin/bash" 37 hours ago Exited (137) 36 hours ago centos2 46db1d72bb6e 4ab4c602aa5e "/hello" 3 days ago Exited (0) 3 days ago naughty_nash [root@localhost myfile]# docker start 612a7e2a951a 612a7e2a951a [root@localhost myfile]# docker attach 612a7e2a951a [root@612a7e2a951a /]# cd / .dockerenv dataVolumeContainer1/ etc/ lib64/ opt/ run/ sys/ var/ anaconda-post.log dataVolumeContainer2/ home/ media/ proc/ sbin/ tmp/ bin/ dev/ lib/ mnt/ root/ srv/ usr/ [root@612a7e2a951a /]# cd /dataVolumeContainer1 [root@612a7e2a951a dataVolumeContainer1]# ls centos_t1.txt [root@612a7e2a951a dataVolumeContainer1]#
關於繼承,容器間的信息傳遞,數據卷的生命週期是持續到沒有容器使用它爲止
------------------------------------