kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose up Creating flask_redis_practice_flask-demo_1 ... done Creating flask_redis_practice_redis-server_1 ... done Attaching to flask_redis_practice_redis-server_1, flask_redis_practice_flask-demo_1 redis-server_1 | 1:C 07 Mar 2022 02:41:20.371 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo redis-server_1 | 1:C 07 Mar 2022 02:41:20.371 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started redis-server_1 | 1:C 07 Mar 2022 02:41:20.371 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf redis-server_1 | 1:M 07 Mar 2022 02:41:20.372 * monotonic clock: POSIX clock_gettime redis-server_1 | 1:M 07 Mar 2022 02:41:20.372 * Running mode=standalone, port=6379. redis-server_1 | 1:M 07 Mar 2022 02:41:20.372 # Server initialized redis-server_1 | 1:M 07 Mar 2022 02:41:20.372 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. redis-server_1 | 1:M 07 Mar 2022 02:41:20.372 * Ready to accept connections flask-demo_1 | * Serving Flask app 'app.py' (lazy loading) flask-demo_1 | * Environment: production flask-demo_1 | WARNING: This is a development server. Do not use it in a production deployment. flask-demo_1 | Use a production WSGI server instead. flask-demo_1 | * Debug mode: off flask-demo_1 | * Running on all addresses. flask-demo_1 | WARNING: This is a development server. Do not use it in a production deployment. flask-demo_1 | * Running on http://172.19.0.2:5000/ (Press CTRL+C to quit) flask-demo_1 | 172.19.0.1 - - [07/Mar/2022 02:41:39] "GET / HTTP/1.1" 200 - flask-demo_1 | 172.19.0.1 - - [07/Mar/2022 02:41:39] "GET /favicon.ico HTTP/1.1" 404 - flask-demo_1 | 172.19.0.1 - - [07/Mar/2022 02:41:40] "GET / HTTP/1.1" 200 -
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------------------------------- flask_redis_practice_flask-demo_1 flask run -h 0.0.0.0 Up 0.0.0.0:8080->5000/tcp,:::8080->5000/tcp flask_redis_practice_redis-server_1 docker-entrypoint.sh redis ... Up 6379/tcp
注意:這個指定一定要在docker-compose檔案所在的資料夾執行才有效
我們稍微看一下相關指令
1 2 3 4 5 6 7 8
kite@host04:~/Desktop/flask_redis_practice$ docker-compose 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) ... ...
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose -p myproject up -d
使用指令查看
1 2 3 4 5
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose -p myproject ps Name Command State Ports ------------------------------------------------------------------------------------------------------------ myproject_flask-demo_1 flask run -h 0.0.0.0 Up 0.0.0.0:8080->5000/tcp,:::8080->5000/tcp myproject_redis-server_1 docker-entrypoint.sh redis ... Up 6379/tcp
@app.route('/') defhello(): redis.incr('hits') returnf"Hello Container World! I have been seen {redis.get('hits').decode('utf-8')} times and my hostname is {socket.gethostname()}.\n"
kite@host04:~/Desktop/flask_redis_practice$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE flask_redis_practice_flask-demo latest 626b99e2547c 36 seconds ago 129MB
kite@host04:~/Desktop/flask_redis_practice$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE flask-kite latest 33a0bf4d2428 11 seconds ago 129MB
3.10. docker-compose build 文件格式 - context、dockefile name
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) --profile NAME Specify a profile to enable -c, --context NAME Specify a context name --verbose Show more output --log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) --ansi (never|always|auto) Control when to print ANSI control characters --no-ansi Do not print ANSI control characters (DEPRECATED) -v, --version Print version and exit -H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify --tlscacert CA_PATH Trust certs signed only by this CA --tlscert CLIENT_CERT_PATH Path to TLS certificate file --tlskey TLS_KEY_PATH Path to TLS key file --tlsverify Use TLS and verify the remote --skip-hostname-check Don't check the daemon's hostname against the name specified in the client certificate --project-directory PATH Specify an alternate working directory (default: the path of the Compose file) --compatibility If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent (DEPRECATED) --env-file PATH Specify an alternate environment file
Commands: build Build or rebuild services config Validate and view the Compose file create Create services down Stop and remove resources events Receive real time events from containers exec Execute a command in a running container help Get help on a command images List images kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pull service images push Push service images restart Restart services rm Remove stopped containers run Run a one-off command scale Set number of containers for a service start Start services stop Stop services top Display the running processes unpause Unpause services up Create and start containers version Show version information and quit
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose build --help Build or rebuild services.
Services are built once and then tagged as `project_service`, e.g. `composetest_db`. If you change a service's `Dockerfile` or the contents of its build directory, you can run `docker-compose build` to rebuild it.
Options: --build-arg key=val Set build-time variables for services. --compress Compress the build context using gzip. --force-rm Always remove intermediate containers. -m, --memory MEM Set memory limit for the build container. --no-cache Do not use cache when building the image. --no-rm Do not remove intermediate containers after a successful build. --parallel Build images in parallel. --progress string Set type of progress output (auto, plain, tty). --pull Always attempt to pull a newer version of the image. -q, --quiet Don't print anything to STDOUT
3.13. docker compose build -d –build
試著修改app.py檔
1 2 3 4 5 6 7 8 9 10 11 12 13
from flask import Flask from redis import Redis import os import socket
@app.route('/') defhello(): redis.incr('hits') returnf"Hello Container World!I'm kite. I have been seen {redis.get('hits').decode('utf-8')} times and my hostname is {socket.gethostname()}.\n"
在運行的過程中:
1 2 3 4 5
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------------------------------- flask_redis_practice_flask-demo_1 flask run -h 0.0.0.0 Up 0.0.0.0:8080->5000/tcp,:::8080->5000/tcp flask_redis_practice_redis-server_1 docker-entrypoint.sh redis ... Up 6379/tcp
使用--build指令
1
docker compose build -d --build
這個指令會發現檔案有發生改變,就會決定重新build,並且會直接重啟與運行容器,不用經過compose stop、compose rm、compose build、 compose up 過程,非常方便,結果如下:
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose ps [sudo] password for kite: Name Command State Ports ----------------------------------------------------------------------------------------------------------------------- flask_redis_practice_flask-demo_1 flask run -h 0.0.0.0 Up 0.0.0.0:8080->5000/tcp,:::8080->5000/tcp flask_redis_practice_redis-server_1 docker-entrypoint.sh redis ... Up 6379/tcp
接著,我們再試著下docker-compose up -d,我們會發現有出現create busybox的訊息:
1 2 3 4
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose up -d flask_redis_practice_flask-demo_1 is up-to-date flask_redis_practice_redis-server_1 is up-to-date Creating flask_redis_practice_busybox_1 ... done
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose up -d WARNING: Found orphan containers (flask_redis_practice_busybox_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. flask_redis_practice_flask-demo_1 is up-to-date flask_redis_practice_redis-server_1 is up-to-date
會出現警告:
WARNING: Found orphan containers (flask_redis_practice_busybox_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the –remove-orphans flag to clean it up.
照著指令操作,移除busybox的指令需要--remove-orphans,執行如下:
1 2 3 4 5
kite@host04:~/Desktop/flask_redis_practice$ sudo docker-compose up -d --remove-orphans Removing orphan container "flask_redis_practice_busybox_1" flask_redis_practice_flask-demo_1 is up-to-date flask_redis_practice_redis-server_1 is up-to-date kite@host04:~/Desktop/flask_redis_practice$