- Hands-On Docker for Microservices with Python
- Jaime Buelta
- 562字
- 2021-06-24 12:35:55
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 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.
- Building E-commerce Sites with VirtueMart Cookbook
- SEO 20日
- HCNA網絡技術
- 新一代物聯網架構技術:分層算力網絡
- Getting Started with WebRTC
- 雷達饋線技術
- 物聯網通信技術
- 語音信號處理及Blackfin DSP實現
- Learning Windows 8 Game Development
- 網絡利他行為研究:積極心理學的視角
- 計算機網絡技術
- 5G非正交多址接入技術:理論、算法與實現
- Getting Started with tmux
- Laravel Application Development Cookbook
- SEO攻略:搜索引擎優化策略與實戰案例詳解