docker 书
安装地址 官方
https://docs.docker.com/compose/install/
原安装地址太慢
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
凡是慢的 肯定就是 你离资源太远 下面这个
让你docker的请求更快的地址
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-composechmod +x /usr/local/bin/docker-compose
然后 安装成功
准备启动
//
这是记得把docker 的镜像地址换了
Azure 中国镜像https://dockerhub.azk8s.cn
阿里云加速器(需登录账号获取)
七牛云加速器https://reg-mirror.qiniu.com
=============================================
Get started with Docker Compose
Estimated reading time: 10 minutes
On this page you build a simple Python web application running on Docker Compose. The application uses the Flask framework and maintains a hit counter in Redis. While the sample uses Python, the concepts demonstrated here should be understandable even if you’re not familiar with it.
Prerequisites
Make sure you have already installed bothDocker EngineandDocker Compose. You don’t need to install Python or Redis, as both are provided by Docker images.
Step 1: Setup
Define the application dependencies.
Create a directory for the project:
$ mkdir composetest
$ cd composetest
Create a file calledapp.pyin your project directory and paste this in:
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
In this example,redisis the hostname of the redis container on the application’s network. We use the default port for Redis,6379.
Handling transient errors
Note the way theget_hit_countfunction is written. This basic retry loop lets us attempt our request multiple times if the redis service is not available. This is useful at startup while the application comes online, but also makes our application more resilient if the Redis service needs to be restarted anytime during the app’s lifetime. In a cluster, this also helps handling momentary connection drops between nodes.
Create another file calledrequirements.txtin your project directory and paste this in:
flask
redis
Step 2: Create a Dockerfile
In this step, you write a Dockerfile that builds a Docker image. The image contains all the dependencies the Python application requires, including Python itself.
In your project directory, create a file namedDockerfileand paste the following:
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run"]
This tells Docker to:
Build an image starting with the Python 3.7 image.
Set the working directory to/code.
Set environment variables used by theflaskcommand.
Install gcc so Python packages such as MarkupSafe and SQLAlchemy can compile speedups.
Copyrequirements.txtand install the Python dependencies.
Copy the current directory.in the project to the workdir.in the image.
Set the default command for the container toflask run.
For more information on how to write Dockerfiles, see theDocker user guideand theDockerfile reference.
Step 3: Define services in a Compose file
Create a file calleddocker-compose.ymlin your project directory and paste the following:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
This Compose file defines two services:webandredis.
Web service
Thewebservice uses an image that’s built from theDockerfilein the current directory. It then binds the container and the host machine to the exposed port,5000. This example service uses the default port for the Flask web server,5000.
Redis service
Theredisservice uses a publicRedisimage pulled from the Docker Hub registry.
Step 4: Build and run your app with Compose
From your project directory, start up your application by runningdocker-compose up.
$ docker-compose up
Creating network "composetest_default" with the default driver
Creating composetest_web_1 ...
Creating composetest_redis_1 ...
Creating composetest_web_1
Creating composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
redis_1 | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
web_1 | * Restarting with stat
redis_1 | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
web_1 | * Debugger is active!
redis_1 | 1:M 17 Aug 22:11:10.483 # Server initialized
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
web_1 | * Debugger PIN: 330-787-903
redis_1 | 1:M 17 Aug 22:11:10.483 * Ready to accept connections
Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.
Enter http://localhost:5000/ in a browser to see the application running.
If you’re using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop for Windows, then the web app should now be listening on port 5000 on your Docker daemon host. Point your web browser to http://localhost:5000 to find theHello Worldmessage. If this doesn’t resolve, you can also try http://127.0.0.1:5000.
If you’re using Docker Machine on a Mac or Windows, usedocker-machine ip MACHINE_VMto get the IP address of your Docker host. Then, openhttp://MACHINE_VM_IP:5000in a browser.
You should see a message in your browser saying:
Hello World! I have been seen 1 times.
Refresh the page.
The number should increment.
Hello World! I have been seen 2 times.
Switch to another terminal window, and typedocker image lsto list local images.
Listing images at this point should returnredisandweb.
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
composetest_web latest e2c21aa48cc1 4 minutes ago 93.8MB
python 3.4-alpine 84e6077c7ab6 7 days ago 82.5MB
redis alpine 9d8fa9aa0e5b 3 weeks ago 27.5MB
You can inspect images withdocker inspect
Stop the application, either by runningdocker-compose downfrom within your project directory in the second terminal, or by hitting CTRL+C in the original terminal where you started the app.
Step 5: Edit the Compose file to add a bind mount
Editdocker-compose.ymlin your project directory to add abind mountfor thewebservice:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
environment:
FLASK_ENV: development
redis:
image: "redis:alpine"
The newvolumeskey mounts the project directory (current directory) on the host to/codeinside the container, allowing you to modify the code on the fly, without having to rebuild the image. Theenvironmentkey sets theFLASK_ENVenvironment variable, which tellsflask runto run in development mode and reload the code on change. This mode should only be used in development.
Step 6: Re-build and run the app with Compose
From your project directory, typedocker-compose upto build the app with the updated Compose file, and run it.
$ docker-compose up
Creating network "composetest_default" with the default driver
Creating composetest_web_1 ...
Creating composetest_redis_1 ...
Creating composetest_web_1
Creating composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
...
Check theHello Worldmessage in a web browser again, and refresh to see the count increment.
Shared folders, volumes, and bind mounts
If your project is outside of theUsersdirectory (cd ~), then you need to share the drive or location of the Dockerfile and volume you are using. If you get runtime errors indicating an application file is not found, a volume mount is denied, or a service cannot start, try enabling file or drive sharing. Volume mounting requires shared drives for projects that live outside ofC:\Users(Windows) or/Users(Mac), and is required foranyproject on Docker Desktop for Windows that usesLinux containers. For more information, seeShared Driveson Docker Desktop for Windows,File sharingon Docker for Mac, and the general examples on how toManage data in containers.
If you are using Oracle VirtualBox on an older Windows OS, you might encounter an issue with shared folders as described in thisVB trouble ticket. Newer Windows systems meet the requirements forDocker Desktop for Windowsand do not need VirtualBox.
Step 7: Update the application
Because the application code is now mounted into the container using a volume, you can make changes to its code and see the changes instantly, without having to rebuild the image.
Change the greeting inapp.pyand save it. For example, change theHello World!message toHello from Docker!:
return 'Hello from Docker! I have been seen {} times.\n'.format(count)
Refresh the app in your browser. The greeting should be updated, and the counter should still be incrementing.
need-to-insert-img
Step 8: Experiment with some other commands
If you want to run your services in the background, you can pass the-dflag (for “detached” mode) todocker-compose upand usedocker-compose psto see what is currently running:
$ docker-compose up -d
Starting composetest_redis_1...
Starting composetest_web_1...
$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------
composetest_redis_1 /usr/local/bin/run Up
composetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp
Thedocker-compose runcommand allows you to run one-off commands for your services. For example, to see what environment variables are available to thewebservice:
$ docker-compose run web env
Seedocker-compose --helpto see other available commands. You can also installcommand completionfor the bash and zsh shell, which also shows you available commands.
If you started Compose withdocker-compose up -d, stop your services once you’ve finished with them:
$ docker-compose stop
You can bring everything down, removing the containers entirely, with thedowncommand. Pass--volumesto also remove the data volume used by the Redis container:
$ docker-compose down --volumes
At this point, you have seen the basics of how Compose works.