Hands-On Docker for Microservices with Python
上QQ阅读APP看书,第一时间看更新

Deploying the Docker service locally

With all these elements, we can create the service to locally deploy the Thoughts service:

     server:
env_file: environment.env
image: thoughts_server
build:
context: .
dockerfile: docker/app/Dockerfile
ports:
- "8000:8000"
depends_on:
- db

We need to be sure to add the dependency of the db database service. We also bound the internal port so that we can access it locally.

We start the service with the up command. There are some differences between the up and the run commands, but the main one is that run is for single commands that start and stop, while up is designed for services. For example, run creates an interactive Terminal, which displays colors, and up shows the standard output as logs, including the time when they were generated, accepts the -d flag to run in the background, and so on. Using one instead of the other is normally okay, however,  up exposes ports and allows other containers and services to connect, while run does not.

We can start the service now with the following commands:

$ docker-compose up server
Creating network "ch3_default" with the default driver
Creating ch3_db_1 ... done
Creating ch3_server_1 ... done
Attaching to ch3_server_1
server_1 | [uWSGI] getting INI configuration from /opt/uwsgi/uwsgi.ini
server_1 | *** Starting uWSGI 2.0.18 (64bit) on [Sun Jun 2
...
server_1 | spawned uWSGI master process (pid: 6)
server_1 | spawned uWSGI worker 1 (pid: 7, cores: 1)
server_1 | spawned uWSGI http 1 (pid: 8)

Now access the service in localhost:8000 in a browser:

You can see the logs in the Terminal. Hitting Ctrl + C will stop the server. The service can also be started using the -d flag, to detach the Terminal and run in daemon mode:

$ docker-compose up -d server
Creating network "ch3_default" with the default driver
Creating ch3_db_1 ... done
Creating ch3_server_1 ... done
$

Check the running services, their current state, and open ports with docker-compose ps:

$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------
ch3_db_1 postgres Up 0.0.0.0:5432->5432/tcp
ch3_server_1 /bin/sh /opt/uwsgi/start_s ... Up 0.0.0.0:8000->8000/tcp

As we've seen before, we can directly access the database and run raw SQL commands in it. This can be useful for debugging problems or conducting experiments:

$ PGPASSWORD=somepassword pgcli -h localhost -U postgres thoughts
Server: PostgreSQL 11.3
Version: 2.0.2

postgres@localhost:thoughts>
INSERT INTO thought_model (username, text, timestamp)
VALUES ('peterparker', 'A great power carries a great
responsability', now());

INSERT 0 1
Time: 0.014s
postgres@localhost:thoughts>

Now the thought is available through the following API:

$ curl http://localhost:8000/api/thoughts/
[{"id": 1, "username": "peterparker", "text": "A great power carries a great responsability", "timestamp": "2019-06-02T19:44:34.384178"}]

If you need to see the logs in detach mode, you can use the docker-compose logs <optional: service> command:

$ docker-compose logs server
Attaching to ch3_server_1
server_1 | [uWSGI] getting INI configuration from /opt/uwsgi/uwsgi.ini
server_1 | *** Starting uWSGI 2.0.18 (64bit) on [Sun Jun 2 19:44:15 2019] ***
server_1 | compiled with version: 8.3.0 on 02 June 2019 11:00:48
...
server_1 | [pid: 7|app: 0|req: 2/2] 172.27.0.1 () {28 vars in 321 bytes} [Sun Jun 2 19:44:41 2019] GET /api/thoughts/ => generated 138 bytes in 4 msecs (HTTP/1.1 200) 2 headers in 72 bytes (1 switches on core 0)

To totally stop the cluster, call docker-compose down:

$ docker-compose down
Stopping ch3_server_1 ... done
Stopping ch3_db_1 ... done
Removing ch3_server_1 ... done
Removing ch3_db_1 ... done
Removing network ch3_default

This stops all the containers.