日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

docker-compose初试及命令基础

發(fā)布時間:2024/1/17 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 docker-compose初试及命令基础 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

以一個簡單的lnmp.yaml的配置文件進行講解docker-compose命令的基礎(chǔ)講解,熟練掌握命令

[root@docker lnmp]# cat lnmp.yaml version: '3' services:nginx:image: nginxports:- "80:80"links:- php:phpvolumes:- "/www:/usr/local/nginx/html"php:image: phpexpose:- "9000"volumes:- "/www:/usr/local/nginx/html"db:image: mysqlports:- "3306:3306"environment:MYSQL_ROOT_PASSWORD: redhatMYSQL_DATABASE: wordpressMYSQL_USER: wordpressMYSQL_PASSWORD: wordpress

上面配置文件的關(guān)于編寫的參數(shù)暫時不進行講解,這里只進行關(guān)于docker-compose的命令

[root@docker ~]# docker-compose --help Define and run multi-container applications with Docker.Usage:docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]docker-compose -h|--help

看用法的定義就是利用docker定義和運行多個容器應用

Options:-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)-p, --project-name NAME Specify an alternate project name (default: directory name)

默認如果沒有指定具體的compose配置文件,那么就docker-compose.yml,并且如果不指定項目名稱,默認就是文件夾的name

up Create and start containers

現(xiàn)在利用command中的up進行創(chuàng)建并啟動容器(多個容器,安裝配置文件中,至于順序現(xiàn)在不講解)

[root@docker lnmp]# docker-compose -f lnmp.yaml up Creating lnmp_php_1 ... Creating lnmp_db_1 ... Creating lnmp_db_1 Creating lnmp_php_1 ... done Creating lnmp_nginx_1 ... Creating lnmp_db_1 ... done

后面的一大堆輸出,這里不進行全部顯示了,創(chuàng)建并啟動好了容器后,如何查看運行的狀態(tài)呢?

ps List containers [root@docker lnmp]# docker-compose ps Name Command State Ports ------------------------------

為什么這里沒有顯示呢?因為配置文件名稱不是docker-compose.yml,所以在執(zhí)行一系列命令時需要加上-f lnmp.yaml

[root@docker lnmp]# docker-compose -f lnmp.yaml psName Command State Ports ------------------------------------------------------------------------------ lnmp_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

由于配置文件lnmp.yaml在當前目錄,這里可以進行絕對路徑:

[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml psName Command State Ports ------------------------------------------------------------------------------ lnmp_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

由上面可以看出將所有的容器都顯示出來,如何指定顯出某一個容器呢?

[root@docker ~]# docker-compose ps --help List containers.Usage: ps [options] [SERVICE...]

一般的command命令都可以帶上SERVICE,這里的service是什么呢?就是配置文件中定義的services服務(wù),配置文件中定義了三個:nginx、php、db(不能寫錯service名稱)

[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml ps nginxName Command State Ports -------------------------------------------------------------------------- lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp

name這一列代表的是容器名稱,而不是service名稱

[root@docker lnmp]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a5747e115947 nginx "/usr/local/nginx/..." 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp lnmp_nginx_1 508ac0a6890c php "/usr/local/php/sb..." 6 minutes ago Up 6 minutes 9000/tcp lnmp_php_1 aaa3cc6f1040 mysql "docker-entrypoint..." 6 minutes ago Up 6 minutes 0.0.0.0:3306->3306/tcp lnmp_db_1

name的組成由project+service組成(后面的_1本人認為是構(gòu)建的次數(shù),第一次構(gòu)建up就代表1吧)

If you change a service’s Dockerfile or the contents of its build directory, run?docker-compose build?to rebuild it. docker-compose up --build或者docker-compose build

config:

config Validate and view the Compose file

利用config命令可以打印處配置文件的內(nèi)容和service名稱以及volumes信息

[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml config --services php nginx db[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml config services:db:environment:MYSQL_DATABASE: wordpressMYSQL_PASSWORD: wordpressMYSQL_ROOT_PASSWORD: redhatMYSQL_USER: wordpressimage: mysqlports:- 3306:3306/tcpnginx:image: nginxlinks:- php:phpports:- 80:80/tcpvolumes:- /www:/usr/local/nginx/html:rwphp:expose:- '9000'image: phpvolumes:- /www:/usr/local/nginx/html:rw version: '3.0'[root@docker lnmp]# docker-compose -f /root/compose_project/lnmp/lnmp.yaml config --volumes

down:停止并刪除容器、網(wǎng)絡(luò)、鏡像、數(shù)據(jù)卷

down Stop and remove containers, networks, images, and volumes[root@docker lnmp]# docker-compose -f lnmp.yaml down Stopping lnmp_nginx_1 ... done Stopping lnmp_db_1 ... done Stopping lnmp_php_1 ... done Removing lnmp_nginx_1 ... done Removing lnmp_db_1 ... done Removing lnmp_php_1 ... done Removing network lnmp_default [root@docker lnmp]# docker-compose -f lnmp.yaml ps Name Command State Ports ------------------------------ [root@docker lnmp]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

down也帶有其他的參數(shù):

Options:--rmi type Remove images. Type must be one of:'all': Remove all images used by any service.'local': Remove only images that don't have a custom tag set by the `image` field.-v, --volumes Remove named volumes declared in the `volumes` sectionof the Compose file and anonymous volumesattached to containers.--remove-orphans Remove containers for services not defined in theCompose file

exec:在運行的容器中執(zhí)行命令:

exec Execute a command in a running container[root@docker lnmp]# docker-compose -f lnmp.yaml exec db env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=aaa3cc6f1040 TERM=xterm MYSQL_ROOT_PASSWORD=redhat MYSQL_PASSWORD=wordpress MYSQL_USER=wordpress MYSQL_DATABASE=wordpress GOSU_VERSION=1.7 MYSQL_MAJOR=5.7 MYSQL_VERSION=5.7.20-1debian8 HOME=/root

db是指定了在哪一個service中執(zhí)行env命令

images:列出鏡像

[root@docker lnmp]# docker-compose -f lnmp.yaml imagesContainer Repository Tag Image Id Size ---------------------------------------------------------- lnmp_db_1 mysql latest 5709795eeffa 389 MB lnmp_nginx_1 nginx latest c3babfeba09b 551 MB lnmp_php_1 php latest 8902ce599658 1 GB

參數(shù)-q:列出鏡像的id

[root@docker lnmp]# docker-compose -f lnmp.yaml images -q 5709795eeffac51eca46cf40ab36669aad27e8cb4b0e91876d5e9f7bafad6acb c3babfeba09bab70a8e3b7e8d734ee274af4a02ed8b9b4d286cd58caf64dbdc5 8902ce599658633ee95e270843b37daabe2e0dc298861ac8c25f5f7e11a7de1e

kill:殺死容器

kill Kill containers[root@docker ~]# docker-compose kill --help Force stop service containers.Usage: kill [options] [SERVICE...]Options:-s SIGNAL SIGNAL to send to the container.Default signal is SIGKILL.

可以看見后面也可以指定哪一個service

logs:將容器中的日志進行輸出

logs View output from containers[root@docker ~]# docker-compose logs --help View output from containers.Usage: logs [options] [SERVICE...]Options:--no-color Produce monochrome output.-f, --follow Follow log output.-t, --timestamps Show timestamps.--tail="all" Number of lines to show from the end of the logsfor each container.

將db的日志輸出最后的10行:

[root@docker lnmp]# docker-compose -f lnmp.yaml logs -t --tail db ERROR: tail flag must be all or a number [root@docker lnmp]# docker-compose -f lnmp.yaml logs -t --tail="10" db Attaching to lnmp_db_1 db_1 | 2017-11-09T14:48:24.618652188Z 2017-11-09T14:48:24.618090Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode. db_1 | 2017-11-09T14:48:24.618654153Z 2017-11-09T14:48:24.618104Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode. db_1 | 2017-11-09T14:48:24.621872173Z 2017-11-09T14:48:24.620524Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode. db_1 | 2017-11-09T14:48:24.621897721Z 2017-11-09T14:48:24.620616Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode. db_1 | 2017-11-09T14:48:24.658532597Z 2017-11-09T14:48:24.658110Z 0 [Note] Event Scheduler: Loaded 0 events db_1 | 2017-11-09T14:48:24.660827605Z 2017-11-09T14:48:24.658335Z 0 [Note] mysqld: ready for connections. db_1 | 2017-11-09T14:48:24.660852816Z Version: '5.7.20' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL) db_1 | 2017-11-09T14:48:24.660857339Z 2017-11-09T14:48:24.658347Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check. db_1 | 2017-11-09T14:48:24.660861018Z 2017-11-09T14:48:24.658349Z 0 [Note] Beginning of list of non-natively partitioned tables db_1 | 2017-11-09T14:48:24.679766350Z 2017-11-09T14:48:24.679398Z 0 [Note] End of list of non-natively partitioned tables

pause:暫停service:

pause Pause services[root@docker ~]# docker-compose pause --help Pause services.Usage: pause [SERVICE...]

將db這個service進行暫停:

[root@docker lnmp]# docker-compose -f lnmp.yaml pause db Pausing lnmp_db_1 ... done [root@docker lnmp]# docker-compose -f lnmp.yaml psName Command State Ports ------------------------------------------------------------------------------- lnmp_db_1 docker-entrypoint.sh mysqld Paused 0.0.0.0:3306->3306/tcp lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

unpause:將暫停的service進行恢復:

unpause Unpause services[root@docker lnmp]# docker-compose -f lnmp.yaml unpause db Unpausing lnmp_db_1 ... done [root@docker lnmp]# docker-compose -f lnmp.yaml psName Command State Ports ------------------------------------------------------------------------------ lnmp_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

port:查看端口映射信息:

port Print the public port for a port binding[root@docker ~]# docker-compose port --help Print the public port for a port binding.Usage: port [options] SERVICE PRIVATE_PORTOptions:--protocol=proto tcp or udp [default: tcp]--index=index index of the container if there are multipleinstances of a service [default: 1][root@docker lnmp]# docker-compose -f lnmp.yaml port db 3306 0.0.0.0:3306

restart:重啟service:

restart Restart services[root@docker ~]# docker-compose restart --help Restart running containers.Usage: restart [options] [SERVICE...]Options:-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.(default: 10)

將nginx這個service進行重啟:

[root@docker lnmp]# docker-compose -f lnmp.yaml restart nginx Restarting lnmp_nginx_1 ... done [root@docker lnmp]# docker-compose -f lnmp.yaml psName Command State Ports ------------------------------------------------------------------------------ lnmp_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp lnmp_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:80->80/tcp lnmp_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp

rm:刪除已經(jīng)停止的服務(wù)容器,沒有停止刪除不了

rm Remove stopped containers[root@docker ~]# docker-compose rm --help Removes stopped service containers.By default, anonymous volumes attached to containers will not be removed. You can override this with `-v`. To list all volumes, use `docker volume ls`.Any data which is not in a volume will be lost.Usage: rm [options] [SERVICE...]Options:-f, --force Don't ask to confirm removal-s, --stop Stop the containers, if required, before removing-v Remove any anonymous volumes attached to containers-a, --all Deprecated - no effect.[root@docker lnmp]# docker-compose -f lnmp.yaml rm No stopped containers

如果加上-s/--stop那么就會先將服務(wù)進行停止,然后詢問你是否刪除:

[root@docker lnmp]# docker-compose -f lnmp.yaml rm -s db Stopping lnmp_db_1 ... done Going to remove lnmp_db_1 Are you sure? [yN]

-f:代表在刪除的過程中不會詢問你是否刪除,而是直接進行刪除

run:運行一次性命令

run Run a one-off command[root@docker ~]# docker-compose run --help Run a one-off command on a service.For example:$ docker-compose run web python manage.py shell

scale:擴展容器,為service設(shè)置多個容器:

scale Set number of containers for a service[root@docker ~]# docker-compose scale --help Set number of containers to run for a service.Numbers are specified in the form `service=num` as arguments. For example:$ docker-compose scale web=2 worker=3This command is deprecated. Use the up command with the `--scale` flag instead.Usage: scale [options] [SERVICE=NUM...][root@docker lnmp]# docker-compose -f lnmp.yaml scale nginx=2 WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.

而且在創(chuàng)建多個容器時需要考慮端口及ip的監(jiān)聽狀況(占用的情況)

start? ? ? ? ? ? ? Start services

stop? ? ? ? ? ? ? ?Stop services

[root@docker lnmp]# docker-compose -f lnmp.yaml stop db Stopping lnmp_db_1 ... done [root@docker lnmp]# docker-compose -f lnmp.yaml start db Starting db ... done

top:顯示運行的進程

top Display the running processes[root@docker lnmp]# docker-compose -f lnmp.yaml top lnmp_db_1UID PID PPID C STIME TTY TIME CMD -------------------------------------------------------------- systemd+ 38246 38223 7 23:27 ? 00:00:00 mysqldlnmp_nginx_1 UID PID PPID C STIME TTY TIME CMD -------------------------------------------------------------------------------------------------------------------- root 38367 38352 1 23:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off; www 38421 38367 0 23:27 ? 00:00:00 nginx: worker process lnmp_php_1 UID PID PPID C STIME TTY TIME CMD ------------------------------------------------------------------------------------------------------------- root 38208 38186 0 23:27 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) www 38307 38208 0 23:27 ? 00:00:00 php-fpm: pool www www 38308 38208 0 23:27 ? 00:00:00 php-fpm: pool www

指定某一個service:

[root@docker lnmp]# docker-compose -f lnmp.yaml top nginx lnmp_nginx_1 UID PID PPID C STIME TTY TIME CMD -------------------------------------------------------------------------------------------------------------------- root 38367 38352 0 23:27 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off; www 38421 38367 0 23:27 ? 00:00:00 nginx: worker process

version:顯示docker-compose的版本信息:

version Show the Docker-Compose version information[root@docker lnmp]# docker-compose -f lnmp.yaml version docker-compose version 1.17.0, build ac53b73 docker-py version: 2.5.1 CPython version: 2.7.13 OpenSSL version: OpenSSL 1.0.1t 3 May 2016

?

docker-compose命令運行的級別在service層,而如果需要對容器進行修改等操作還是運行docker命令方便

總結(jié)

以上是生活随笔為你收集整理的docker-compose初试及命令基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。