ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

2. 容器常用操作

2026/8/21 14:52:28 拓冰建站 浏览量
2. 容器常用操作 容器常用操作一、创建容器1、格式2、容器的运行是依赖于一个持续的进程的3、查看镜像定义的默认执行的指令二、容器管理常用操作1、查看容器2、查看容器的详细信息3、查看日志4、连接登录容器5、删除容器6、启动/停止/重启容器7、杀死容器8、导出/导入容器三、容器常用选项1、后台运行 -d2、指定容器名称、主机名3、设置容器开机自启动4、发布容器服务5、传递环境变量6、持久化存储7、容器中应用的配置文件8、定义容器的通信别名 --link容器名称:别名9、容器的资源限制 cpu/内存四、容器平台案例——部署wordpress论坛1、部署wordpress的主库2、登录主库创建论坛需要的库、用户3、部署wordpress论坛4、再次使用相同的项目文件部署论坛5、部署haproxy实现论坛的负载均衡6、扩展MySQL为主从复制一、创建容器1、格式手动上传镜像[rootlocalhost ~]# docker load -i centos7.tar174f56854903: Loading layer[]211.7MB/211.7MB Loaded image: centos:7[rootlocalhost ~]#[rootlocalhost ~]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZE centos7eeb6ee3f44bd2years ago 204MB# docker run [选项] 镜像名称 [命令]创建容器[rootmartin-host ~]# docker run -tid centos:7 /bin/bashe4610c96d2a06645eed9e0b1fe5cc1cb1fb38c5b4258515590e37be72d4130fa -t, -t提供操作终端 -d后台运行[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e4610c96d2a0 centos:7/bin/bash25seconds ago Up24seconds nervous_elion2、容器的运行是依赖于一个持续的进程的[rootmartin-host ~]# docker run -tid centos:7 lsca40ba6b332421da4ecf41ce0f25e05c48e24fd365501d36241b69fef6796e7a[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ca40ba6b3324 centos:7ls12seconds ago Exited(0)11seconds ago agitated_chatelet e4610c96d2a0 centos:7/bin/bash5minutes ago Up5minutes nervous_elion[rootmartin-host ~]# docker run -tid centos:7 sleep 1060ab18232b89e65adffa0f4f0e2c1321ef531249bbfa86b7fa0e93033851bf35[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 60ab18232b89 centos:7sleep 104seconds ago Up3seconds quirky_stonebraker ca40ba6b3324 centos:7ls3minutes ago Exited(0)3minutes ago agitated_chatelet e4610c96d2a0 centos:7/bin/bash8minutes ago Up8minutes nervous_elion3、查看镜像定义的默认执行的指令[rootmartin-host ~]# docker run -tid centos:717ff1296507d1636de43b228c1ec2c3838767ec7ea2175dfa3e3be892e05db4c[rootmartin-host ~]#系统级别镜像: /bin/bash 应用级别镜像启动对应的服务查看镜像的详细信息找Cmd的关键字[rootmartin-host ~]# docker image inspect 镜像名称二、容器管理常用操作1、查看容器[rootmartin-host ~]# docker ps -a[rootmartin-host ~]# docker ps2、查看容器的详细信息[rootmartin-host ~]# docker inspect e4610c3、查看日志[rootmartin-host ~]# docker logs cbf301f4、连接登录容器[rootmartin-host ~]# docker exec -ti e4610 bash[roote4610c96d2a0 /]# mkdir abc[roote4610c96d2a0 /]# touch file01[roote4610c96d2a0 /]# exitexit[rootmartin-host ~]# docker exec -ti e4610 ls /tmp[rootmartin-host ~]# docker exec -ti e4610 ping 10.11.0.2545、删除容器[rootmartin-host ~]# docker rm f781 60ab ca40 e461[rootmartin-host ~]# docker rm -f 7f9c29[rootmartin-host ~]# docker stop 17ff[rootmartin-host ~]# docker rm 17ff6、启动/停止/重启容器[rootmartin-host ~]# docker {start|stop|restart} 容器ID/名称7、杀死容器[rootmartin-host ~]# docker kill cbf3cbf3[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cbf301fcbe8e nginx:1.18/docker-entrypoint.…15minutes ago Exited(137)2seconds ago dazzling_williamson8、导出/导入容器// 导出容器[rootmartin-host ~]# docker export -o cbf3.tar cbf3[rootmartin-host ~]# lsanaconda-ks.cfg cbf3.tar centos7.tar mysql57.tar nginx18.tar tomcat.tar[rootmartin-host ~]#// 导入容器[rootmartin-host ~]# docker import cbf3.tar三、容器常用选项1、后台运行 -d[rootmartin-host ~]# docker run -ti centos:7[rootbfd7a16ca556 /]# exit2、指定容器名称、主机名[rootmartin-host ~]# docker run -tid --nametest1 --hostnametest1 centos:7009be2d89fff1fc5bad453083af54648a87e8cab3e36a3b5bdf8557128509c4d[rootmartin-host ~]# docker exec -ti test1 bash[roottest1 /]#[roottest1 /]# exit3、设置容器开机自启动[rootmartin-host ~]# docker run -tid --nametest2 --hostnametest2 --restartalways centos:74b07cce84d6c53dc01b46ab8e4f56146954053487cbe5bb50c5d7cf8a10571e14、发布容器服务-p 物理机端口:容器端口[rootmartin-host ~]# docker run -tid --nametest4 --hostnametest4 -p 80:80 nginx:1.186cbdfdb366289b1081b0078efd809ff3edccb4bf13c74a6378fa34b1dff965bd[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6cbdfdb36628 nginx:1.18/docker-entrypoint.…3seconds ago Up3seconds0.0.0.0:80-80/tcp, :::80-80/tcp test4-P 随机发布端口[rootmartin-host ~]# docker run -tid --nametest6 --hostnametest6 -P nginx:1.1817186784a1ad15db9c6446bb61a34b45974a154a6841a4dfd49a545fa0602b1d[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 17186784a1ad nginx:1.18/docker-entrypoint.…3seconds ago Up2seconds0.0.0.0:32768-80/tcp, :::32768-80/tcp test65、传递环境变量https://hub.docker.com/-e 变量名称值[rootmartin-host ~]# docker run -tid --nametest7 --hostnametest7 -e NAMEmartin centos:7a79d29b19892231eefe0428bd2c38778ce1cbaaacc170cb1999de072bf124043[rootmartin-host ~]# docker exec -ti test7 bash[roottest7 /]#[roottest7 /]# echo $NAMEmartin[roottest7 /]# exitexit[rootmartin-host ~]# docker run -tid --nametest8 --hostnametest8 \-eMYSQL_ROOT_PASSWORDredhat mysql:5.7 ecb6b7a83da1445c29a3bf6527baaa1bddf284936068272d9042693af764b2fb[rootmartin-host ~]# docker run -tid --nametest9 --hostnametest9 \-eMYSQL_ROOT_PASSWORDredhat\-eMYSQL_DATABASEit\-eMYSQL_USERadmin\-eMYSQL_PASSWORDredhat\mysql:5.76、持久化存储-v 物理机目录:容器目录[rootmartin-host ~]# docker run -tid --nametest1 --hostnametest1 -v /opt/test1:/test1 centos:739103dfa0553136e16da5486a3e3fa75b6ee7ce954a4345e31be7177e9ee012a[rootmartin-host ~]# docker exec -ti test1 bash[roottest1 /]# touch /test1/{1..5}[roottest1 /]# ls /test1/12345[roottest1 /]# exit查看镜像对应的持久化目录 使用docker image inspect查看详细信息找Volumes关键字[rootmartin-host ~]# docker run -tid --nametest2 --hostnametest2 \-eMYSQL_ROOT_PASSWORDredhat\-v/opt/test2/:/var/lib/mysql\mysql:5.7 d509d17f13de8e6616600bc1c1dda80cfa2c9299b2febff193e3f8334860c624[rootmartin-host ~]#多个容器挂载同一个目录[rootmartin-host ~]# docker run -tid --nametest3 --hostnametest3 -v /opt/test1/:/test3 centos:7b6ff36c2d3e30fa827e7efc7e2856cc86c6bdd6bc481c444c0ebacc8dfc7abc6[rootmartin-host ~]#[rootmartin-host ~]# docker run -tid --nametest4 --hostnametest4 -v /opt/test1/:/test4 centos:7d09cb6472eea3e2ea7daadb9f90bea5d905f1e63eb10e4b7e32b49e060640b7d[rootmartin-host ~]#[rootmartin-host ~]#[rootmartin-host ~]# docker exec -ti test3 bash[roottest3 /]# ls /test3/11.jpg22.jpg33.jpg44.jpg55.jpg[roottest3 /]# exitexit[rootmartin-host ~]#[rootmartin-host ~]# docker exec -ti test4 bash[roottest4 /]# ls /test4/11.jpg22.jpg33.jpg44.jpg55.jpg[roottest4 /]# exitexit7、容器中应用的配置文件[rootmartin-host ~]# cat /opt/my.cnf[mysqld]server_id11log_binmaster[rootmartin-host ~]# docker run -tid --nametest5 --hostnametest5 -e MYSQL_ROOT_PASSWORDredhat -v /opt/my.cnf:/etc/my.cnf mysql:5.77d6486c7fb56c2d2e86ef1a914bc7e1036bde3fa1af60b8ddc07f2a89ab8381e 在物理机上修改配置重启容器8、定义容器的通信别名 --link容器名称:别名容器不允许使用IP通信[rootmartin-host ~]# docker run -tid --nametest6 centos:77596c3b5554193d0529ba76039d3c4ecd947813756551829354cd6d8a29d495f[rootmartin-host ~]# docker run -tid --nametest7 --linktest6:test6 centos:7da5b2bf9416c70aad5d06dae265eba518534e147a187a04f15afa34fb6e8f7b9[rootmartin-host ~]#[rootmartin-host ~]# docker exec -ti test7 bash[rootda5b2bf9416c /]# ping test6PING test6(172.17.0.3)56(84)bytes of data.64bytes from test6(172.17.0.3):icmp_seq1ttl64time0.146ms64bytes from test6(172.17.0.3):icmp_seq2ttl64time0.046ms64bytes from test6(172.17.0.3):icmp_seq3ttl64time0.047ms ^C --- test6pingstatistics ---3packets transmitted,3received,0% packet loss,time2002ms rtt min/avg/max/mdev0.046/0.079/0.146/0.048 ms[rootda5b2bf9416c /]#[rootda5b2bf9416c /]# cat /etc/hosts127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters172.17.0.3 test6 7596c3b55541172.17.0.4 da5b2bf9416c[rootda5b2bf9416c /]#9、容器的资源限制 cpu/内存[rootmartin-host ~]# docker run -tid --nametest8 --cpus2 --memory800M centos:7c31b2583f8ad9954e8bf7ae63edac04def6ccabe9c599fd064c091a8de1dc278[rootmartin-host ~]# docker inspect test8 | grep -i memoryMemory:838860800,MemoryReservation:0,MemorySwap:1677721600,MemorySwappiness:null,[rootmartin-host ~]# docker run -tid --nametest9 --cpus2 --memory1G polinux/stress bash99808428499f719fd440722245e27f1787667bf6c45e80fb8a979f0e0855fd6a[rootmartin-host ~]#// 登录容器模拟产生进程[rootmartin-host ~]# docker exec -ti test9 bashbash-5.0#bash-5.0# stress --cpu 2 --io 4 --vm 2 --vm-bytes 128M --timeout 60s 在物理机上top查看资源消耗四、容器平台案例——部署wordpress论坛1、部署wordpress的主库[rootmartin-host ~]# mkdir /opt/wordpress[rootmartin-host ~]# cat /opt/wordpress/master_my.cnf[mysqld]server_id10log_binmaster[rootmartin-host ~]# docker run -tid --namewordpress_master_db --hostnamewordpress_master_db \--restartalways\--cpus1--memory1G\-eMYSQL_ROOT_PASSWORDredhat\-v/opt/wordpress/master_my.cnf:/etc/my.cnf\-v/opt/wordpress/master_db:/var/lib/mysql\mysql:5.7[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d54b28bd02f8 mysql:5.7docker-entrypoint.s…6seconds ago Up5seconds3306/tcp,33060/tcp wordpress_master_db2、登录主库创建论坛需要的库、用户[rootmartin-host ~]# docker exec -ti wordpress_master_db bashrootwordpress_master_db:/#rootwordpress_master_db:/# mysql -uroot -predhatmysql:[Warning]Using a password on thecommandline interface can be insecure. Welcome to the MySQL monitor. Commands end with;or\g. Your MySQL connectionidis2Server version:5.7.35-log MySQL Community Server(GPL)Copyright(c)2000,2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Typehelp;or\hforhelp. Type\ctoclearthe current input statement. mysqlmysqlcreate database wordpress charset utf8;Query OK,1row affected(0.00sec)mysqlcreate userwpuser%identified byredhat;Query OK,0rows affected(0.01sec)mysqlgrant all on wordpress.* towpuser%;Query OK,0rows affected(0.00sec)mysqlflush privileges;Query OK,0rows affected(0.00sec)3、部署wordpress论坛[rootmartin-host ~]# docker run -tid --namewordpress --hostnamewordpress \--restartalways\--cpus2--memory2G\--linkwordpress_master_db:wordpress_master_db\-eWORDPRESS_DB_HOSTwordpress_master_db\-eWORDPRESS_DB_USERwpuser\-eWORDPRESS_DB_PASSWORDredhat\-eWORDPRESS_DB_NAMEwordpress\-eWORDPRESS_TABLE_PREFIXwp_\-v/opt/wordpress/html:/var/www/html\-p9000:80\wordpress[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a836434451bc wordpressdocker-entrypoint.s…3seconds ago Up2seconds0.0.0.0:9000-80/tcp, :::9000-80/tcp wordpress 测试访问论坛, 再使用一样的配置参数创建另外运行论坛的容器4、再次使用相同的项目文件部署论坛[rootmartin-host ~]# docker run -tid --namewordpress_2 --hostnamewordpress_2 --restartalways --cpus2 --memory2G --linkwordpress_master_db:wordpress_master_db -v /opt/wordpress/html:/var/www/html -p 9001:80 wordpressc75caf0cdb769be2ba6f5e7ca6f10429c12e254ddaa37598bcdd0dd2b20e1d1e[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c75caf0cdb76 wordpressdocker-entrypoint.s…3seconds ago Up2seconds0.0.0.0:9001-80/tcp, :::9001-80/tcp wordpress_25、部署haproxy实现论坛的负载均衡[rootmartin-host ~]# docker run -tid --namewordpress_haproxy --hostnamewordpress_haproxy \--restartalways\--cpus2--memory4G\--linkwordpress:wordpress_1\--linkwordpress_2:wordpress_2\-v/opt/wordpress/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg\-p80:8888\haproxy 39111da543b9fedef69943a33dab8ac9ed57d3f58ed6868e7075d71f1dbdd4b5[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39111da543b9 haproxydocker-entrypoint.s…3seconds ago Up2seconds0.0.0.0:80-8888/tcp, :::80-8888/tcp wordpress_haproxy6、扩展MySQL为主从复制[rootmartin-host ~]# docker run -tid --namewordpress_slave_db --hostnamewordpress_slave_db \--restartalways\--cpus1--memory2G\-v/opt/wordpress/slave_db:/var/lib/mysql\-v/opt/wordpress/slave_my.cnf:/etc/my.cnf\--linkwordpress_master_db:wordpress_master_db\-eMYSQL_ROOT_PASSWORDredhat\mysql:5.7[rootmartin-host ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 08e41d61ea7e mysql:5.7docker-entrypoint.s…5seconds ago Up4seconds3306/tcp,33060/tcp wordpress_slave_db[rootmartin-host ~]# docker exec -ti wordpress_master_db bashrootwordpress_master_db:/#rootwordpress_master_db:/# mysql -uroot -predhatmysql:[Warning]Using a password on thecommandline interface can be insecure. Welcome to the MySQL monitor. Commands end with;or\g. Your MySQL connectionidis32Server version:5.7.35-log MySQL Community Server(GPL)Copyright(c)2000,2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Typehelp;or\hforhelp. Type\ctoclearthe current input statement. mysqlmysqlcreate userrepluser%identified byredhat;Query OK,0rows affected(0.00sec)mysqlgrant replication slave on *.* torepluser%;Query OK,0rows affected(0.00sec)mysqlflush privileges;Query OK,0rows affected(0.00sec)mysqlexitBye登录主库备份数据拷贝到从库上恢复[rootmartin-host ~]# docker exec -ti wordpress_master_db bashrootwordpress_master_db:~# mysqldump -uroot -predhat --lock-all-tables --master-data2 --all-databases /wordpress.sqlmysqldump:[Warning]Using a password on thecommandline interface can be insecure. rootwordpress_master_db:~#[rootmartin-host ~]# docker cp wordpress_master_db:/wordpress.sql ./Successfully copied3.63MB to /root/./[rootmartin-host ~]# lswordpress.sql[rootmartin-host ~]# docker cp wordpress.sql wordpress_slave_db:/Successfully copied3.63MB to wordpress_slave_db:/建立主从复制关系[rootmartin-host ~]# docker exec -ti wordpress_slave_db bashrootwordpress_slave_db:/# lsbin dev entrypoint.sh home lib64 mnt proc run srv tmp var boot docker-entrypoint-initdb.d etc lib media opt root sbin sys usr wordpress.sql rootwordpress_slave_db:/#rootwordpress_slave_db:/# mysql -uroot -predhatmysql:[Warning]Using a password on thecommandline interface can be insecure. Welcome to the MySQL monitor. Commands end with;or\g. Your MySQL connectionidis2Server version:5.7.35-log MySQL Community Server(GPL)Copyright(c)2000,2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Typehelp;or\hforhelp. Type\ctoclearthe current input statement. mysqlmysqlsource/wordpress.sql;mysqlCHANGE MASTER TO -MASTER_HOSTwordpress_master_db, -MASTER_USERrepluser, -MASTER_PASSWORDredhat, -MASTER_LOG_FILEmaster.000003, -MASTER_LOG_POS512877;Query OK,0rows affected,2warnings(0.01sec)mysqlSTART SLAVE;Query OK,0rows affected(0.00sec)mysqlSHOW SLAVE STATUS\G;***************************1. row *************************** Slave_IO_State: Waitingformaster to send event Master_Host: wordpress_master_db Master_User: repluser Master_Port:3306Connect_Retry:60Master_Log_File: master.000003 Read_Master_Log_Pos:512877Relay_Log_File: wordpress_slave_db-relay-bin.000002 Relay_Log_Pos:317Relay_Master_Log_File: master.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno:0Last_Error: Skip_Counter:0Exec_Master_Log_Pos:512877Relay_Log_Space:537Until_Condition: None Until_Log_File: Until_Log_Pos:0Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No Last_IO_Errno:0Last_IO_Error: Last_SQL_Errno:0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id:10Master_UUID: e6dcbcb4-4403-11ef-ba30-0242ac110002 Master_Info_File: /var/lib/mysql/master.info SQL_Delay:0SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave hasreadall relay log;waitingformoreupdates Master_Retry_Count:86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position:0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:1rowinset(0.00sec)