Docker-Compose,
Compose is a Docker application that defines and runs multiple containers. In Compose, you can use the YAML file to configure your application service. Then, you only need a simple command to create and start all the services you configured.
There are three steps to use Compose:
Define your application environment in Dockfile so that it can be copied anywhere. Define the services that comprise the application in the docker-compose.yml so that they can run in an isolated environment. Finally, run dcoker-compose up. Compose starts and runs the entire application. Start using Docker Compose
A simple Python program is built in Docker Compose. The application uses the Flask framework and maintains a counter in Redis.
Prerequisites
Make sure that you have installed Docker Engine and Docker Compose. You do not need to install Python or Redis. Both are provided in the Docker image.
Step 1: Define application dependencies
Create a directory for the project
import timeimport redisfrom flask import Flaskapp = 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)if __name__ == "__main__": app.run(host="0.0.0.0", debug=True)
In this example, redis is the host name of the redis container in the Application Network. We use Redis's default port 6379.
Create another file named requirements.txt in your project path and paste the following content into it:
flaskredis
Step 2: Create a Dockerfile
In this step, you need to write a Dockerfile to build a Docker image. This image contains all the dependencies of Python applications and Python itself.
Create a Dockerfile in your project path and paste the following content into it:
return 'Hello from Docker! I have been seen {} times.\n'.format(count)
Refresh your browser. The welcome speech has been updated, and the counter is still growing.
Step 8: try some other commands
? If you want your application to run in the background, you can pass the-d mark to docker-compose up and use docker-compose ps to view the currently running application.
$ docker-compose up -dStarting composetest_redis_1...Starting composetest_web_1...$ docker-compose psName Command State Ports-------------------------------------------------------------------composetest_redis_1 /usr/local/bin/run Upcomposetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp
The docker-compose run command allows you to run one-time commands for your application. For example, to view which environment variables can be used for web Services:
$ docker-compose run web env
Use docker-compose -- help to view all available commands.
If you use docker-compose up-d to start Compose, you may want to stop the service after they are completed:
$ docker-compose stop
You can stop everything and use the down command to completely remove the container. Transfer-volumes can also delete the volumes used in the Redis container.
$ docker-compose down --volumes
Now you have seen the basic knowledge of Compose.