It can create a container based on the local image. If the image does not exist, the image will be pulled down. If the container exists, it will be started directly. Then execute any service you want to execute in the container.
Project description
easier-docker
Repository Introduction
This is based on docker-py which makes it easier to run your program in docker. It can create a container based on the local image. If the image does not exist, the image will be pulled down. If the container exists, it will be started directly. Then execute any service you want to execute in the container.
Install
pip install easier-docker
Usage
💡 Please check config parameters in Docker SDK for Python
Use examples in code
python example.py
# example.py
import os
from easierdocker import EasierDocker
if __name__ == '__main__':
host_script = os.path.dirname(os.path.abspath(__file__))
container_script = '/path/to/container'
config = {
'image': 'python:3.9',
'name': 'python_test',
'volumes': {
f'{host_script}': {'bind': container_script, 'mode': 'rw'}
},
'detach': True,
'command': ["sh", "-c", f'cd {container_script} &&'
'python docker_example.py'],
}
easier_docker = EasierDocker(config)
easier_docker.start()
"""
>>> 2023-12-05 21:02:04,327 - INFO - easier-docker ==> Find docker image: [python:3.9] locally...
>>> 2023-12-05 21:02:04,330 - INFO - easier-docker ==> Image: [python:3.9] is found locally
>>> 2023-12-05 21:02:04,330 - INFO - easier-docker ==> Find docker container: [python_test] locally...
>>> 2023-12-05 21:02:04,332 - INFO - easier-docker ==> ContainerNotFound: [python_test], it will be created
>>> 2023-12-05 21:02:04,568 - INFO - easier-docker ==> Container name: [python_test] is running
>>> 2023-12-05 21:02:04,568 - INFO - easier-docker ==> Container id: [50e5ae686a9f] is running
>>> 2023-12-05 21:02:04,568 - INFO - easier-docker ==> Container ip address: []
>>> 2023-12-05 21:02:04,568 - INFO - easier-docker ==> Successfully container is running and be created at 2023-12-05T13:02:04.344804339Z
"""
The content of docker_example.py is
# docker_example.py
def main():
import logging
import time
for i in range(1, 101):
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info(f'sleep 30s, times:{i}')
time.sleep(30)
if __name__ == '__main__':
main()
Run directly from configuration file
Currently supports type of file: yml, yaml, json
easier-docker -c config.yaml
The content of config.yaml is
# config.yaml
image: python:3.9
name: python_test
volumes:
/Users/admin/data/code_project/easier-docker/example:
bind: /path/to/container
mode: rw
detach: true
command:
- sh
- -c
- cd /path/to/container && python docker_example.py
Related
Repository
- docker-py — A Python library for the Docker Engine API.
Materials
Repository Used
- opsariichthys-bidens — About Building a Basic Information API for Chinese National Universities in the Handheld College Entrance Examination Based on Fastapi.
Maintainers
Contributing
Open an issue or submit PRs.
Standard Python follows the Python PEP-8 Code of Conduct.
Contributors
This project exists thanks to all the people who contribute.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for easier_docker-2.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ee621191794519b924bd1cee5d7f5300cbb695048de99b37758a250ef4460e8 |
|
MD5 | 1af4dd04a91e6714b04aa5518f9dbde8 |
|
BLAKE2b-256 | 9d3fef641f5065b8635d1ae99b9a506ea60245a9792a6bc3a493333831b65f1a |