docker容器與容器云2_使用Docker run運(yùn)行容器?
使用Docker run命令可以創(chuàng)建并運(yùn)行一個(gè)新的容器,該命令后面通常跟著要運(yùn)行的鏡像的名稱(chēng)和一個(gè)或多個(gè)參數(shù)。 如果容器狀態(tài)改變,發(fā)送HTTP請(qǐng)求到指定的URL(僅適用于Docker API v2.1或更高版本)
使用Docker run運(yùn)行容器

Docker run命令的基本用法
Docker run命令用于創(chuàng)建并啟動(dòng)一個(gè)新的容器,其基本語(yǔ)法如下:
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
各選項(xiàng)的含義如下:
選項(xiàng) 含義 d 以后臺(tái)模式運(yùn)行容器 i 以交互模式運(yùn)行容器,通常與t一起使用 t 為容器重新分配一個(gè)偽輸入終端 name 為容器指定一個(gè)名稱(chēng) p 將容器內(nèi)部的端口映射到宿主機(jī)的一個(gè)端口 v 將宿主機(jī)的一個(gè)目錄或文件掛載到容器內(nèi)部 rm 在容器退出后自動(dòng)刪除容器 link 鏈接到另一個(gè)容器 net 指定容器的網(wǎng)絡(luò)連接類(lèi)型 e 設(shè)置環(huán)境變量 env 設(shè)置環(huán)境變量,格式為KEY=VALUE volumesfrom 從其他容器掛載卷 capadd 添加容器的特權(quán)能力 device 添加主機(jī)設(shè)備到容器中 restart 設(shè)置容器重啟策略 privileged 給容器添加特權(quán),如設(shè)置IPC、內(nèi)存等限制 tmpfs 添加一個(gè)臨時(shí)文件系統(tǒng)掛載點(diǎn) readonly 設(shè)置容器為只讀模式 securityopt 設(shè)置SELinux安全選項(xiàng) cpushares 設(shè)置容器CPU使用權(quán)重 memoryswap 設(shè)置容器內(nèi)存交換區(qū)大小 oomkilldisable 禁用OOM Killer(內(nèi)存溢出殺手) pidslimit 設(shè)置容器PID數(shù)量限制 cpus 設(shè)置容器可以使用的CPU核數(shù) ulimit 設(shè)置容器的資源限制,如文件描述符、最大線(xiàn)程數(shù)等 userns 設(shè)置容器使用的用戶(hù)命名空間 ipc 設(shè)置容器的IPC機(jī)制,如共享內(nèi)存、信號(hào)量等 uts 設(shè)置容器的UTS命名空間 netalias 設(shè)置容器的網(wǎng)絡(luò)別名 macaddress 設(shè)置容器的MAC地址 label 設(shè)置容器的標(biāo)簽(metadata) workdir 設(shè)置容器的工作目錄 entrypoint 覆蓋容器的入口點(diǎn)(默認(rèn)為鏡像的入口點(diǎn)) cmd 覆蓋鏡像的默認(rèn)命令(ENTRYPOINT指令)和參數(shù)(CMD指令) healthcmd 設(shè)置健康檢查命令,用于檢查容器是否正常運(yùn)行 healthinterval 設(shè)置健康檢查的時(shí)間間隔(秒) healthtimeout 設(shè)置健康檢查的超時(shí)時(shí)間(秒) healthretries 設(shè)置健康檢查重試次數(shù) healthstartperiod 設(shè)置健康檢查開(kāi)始前的等待時(shí)間(秒) logdriver 設(shè)置日志驅(qū)動(dòng)(如jsonfile、syslog等) logopt 設(shè)置日志選項(xiàng),如日志級(jí)別、輸出格式等 storagedriver 設(shè)置存儲(chǔ)驅(qū)動(dòng)(如aufs、overlay2等) storageopt 設(shè)置存儲(chǔ)選項(xiàng),如存儲(chǔ)后端的URL、認(rèn)證信息等 selinuxoptions 設(shè)置SELinux選項(xiàng),如上下文、策略等 securityopts 設(shè)置安全選項(xiàng),如seccomp、apparmor等 stopsignal 設(shè)置停止容器的信號(hào)(如SIGTERM、SIGKILL等) initpath 設(shè)置初始化進(jìn)程路徑(僅適用于自定義init鏡像) initargs 設(shè)置初始化進(jìn)程參數(shù)(僅適用于自定義init鏡像) busyboxoptions 設(shè)置busybox選項(xiàng),如共享庫(kù)路徑、環(huán)境變量等(僅適用于busybox鏡像) capabilities capabilities.txt文件的內(nèi)容添加到容器中(僅適用于自定義鏡像) w, webhook常用示例
1、后臺(tái)運(yùn)行一個(gè)Nginx容器,并將宿主機(jī)的80端口映射到容器的80端口:
docker run d p 80:80 nginx
2、以交互模式運(yùn)行一個(gè)MySQL容器,并指定用戶(hù)名和密碼:
docker run it e MYSQL_ROOT_PASSWORD=mypassword p 3306:3306 mysql:latest /bin/bash
3、創(chuàng)建一個(gè)名為mycontainer的容器,并在其中運(yùn)行一個(gè)簡(jiǎn)單的shell命令:

docker run d mycontainer /bin/echo "Hello, World!" > /tmp/hello.txt && cat /tmp/hello.txt && sleep infinity & echo $! > /var/run/mycontainer.pid && chmod +x /var/run/mycontainer.pid && chown root:root /var/run/mycontainer.pid && chown root:root /tmp/hello.txt && chown root:root /var/run/mycontainer.pid && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp(本文來(lái)源:www.KengNiao.Com)/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp/hello.txt && chmod u+rwx /var/run/mycontainer.pid && chmod u+rwx /tmp與回答:解答與問(wèn)題1:如何查看正在運(yùn)行的Docker容器?

