GithubHelp home page GithubHelp logo

2022-docker's Introduction

How To Use Docker To Make Local Development A Breeze

Docker is a very powerful tool for developing applications that run in the cloud. If you want to get the most out of it, you need to make sure that the way you're running your code locally matches as closely as possible with how it runs in the cloud.

Today I'm going to show you how to do this, using a simple API server in Python as an example.

Video: https://youtu.be/zkMRWDQV4Tg.

Usage

As a first step, install Docker (see: https://www.docker.com) and install the requirements:

pip install -r requirements.txt

This is how you run the code locally (without Docker):

uvicorn main:app --host 0.0.0.0 --port 8080 --reload

Build and run the Docker image locally, as follows:

docker build -t channel-api .
docker run -d -p 8080:80 channel-api

In order to run the example server with docker compose, use this:

docker-compose up --build

If you use docker compose and you make a minor change in the file, you can now see how everything is updated and the server is restarted automatically.

2022-docker's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

2022-docker's Issues

Which version of Python is this tested with?

I like the concept very much but was unable to run it:

Which Python version?

FROM python:latest may not be ideal... ?

Also, why no version numbers in requirements.txt ?

When I tried, after this :

docker build -t channel-api .
docker run -d -p 8080:80 channel-api
```

 I got this :

```
2023-01-09 14:02:49 Traceback (most recent call last):
2023-01-09 14:02:49   File "/usr/local/bin/uvicorn", line 8, in <module>
2023-01-09 14:02:49     sys.exit(main())
2023-01-09 14:02:49              ^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1130, in __call__
2023-01-09 14:02:49     return self.main(*args, **kwargs)
2023-01-09 14:02:49            ^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1055, in main
2023-01-09 14:02:49     rv = self.invoke(ctx)
2023-01-09 14:02:49          ^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1404, in invoke
2023-01-09 14:02:49     return ctx.invoke(self.callback, **ctx.params)
2023-01-09 14:02:49            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 760, in invoke
2023-01-09 14:02:49     return __callback(*args, **kwargs)
2023-01-09 14:02:49            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/uvicorn/main.py", line 404, in main
2023-01-09 14:02:49     run(
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/uvicorn/main.py", line 569, in run
2023-01-09 14:02:49     server.run()
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 60, in run
2023-01-09 14:02:49     return asyncio.run(self.serve(sockets=sockets))
2023-01-09 14:02:49            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
2023-01-09 14:02:49     return runner.run(main)
2023-01-09 14:02:49            ^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
2023-01-09 14:02:49     return self._loop.run_until_complete(task)
2023-01-09 14:02:49            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
2023-01-09 14:02:49     return future.result()
2023-01-09 14:02:49            ^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 67, in serve
2023-01-09 14:02:49     config.load()
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/uvicorn/config.py", line 477, in load
2023-01-09 14:02:49     self.loaded_app = import_from_string(self.app)
2023-01-09 14:02:49                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
2023-01-09 14:02:49     module = importlib.import_module(module_str)
2023-01-09 14:02:49              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
2023-01-09 14:02:49     return _bootstrap._gcd_import(name[level:], package, level)
2023-01-09 14:02:49            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
2023-01-09 14:02:49   File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
2023-01-09 14:02:49   File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
2023-01-09 14:02:49   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
2023-01-09 14:02:49   File "<frozen importlib._bootstrap_external>", line 940, in exec_module
2023-01-09 14:02:49   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2023-01-09 14:02:49   File "/app/main.py", line 26, in <module>
2023-01-09 14:02:49     @app.get("/")
2023-01-09 14:02:49      ^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 633, in decorator
2023-01-09 14:02:49     self.add_api_route(
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 572, in add_api_route
2023-01-09 14:02:49     route = route_class(
2023-01-09 14:02:49             ^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 400, in __init__
2023-01-09 14:02:49     self.response_field = create_response_field(
2023-01-09 14:02:49                           ^^^^^^^^^^^^^^^^^^^^^^
2023-01-09 14:02:49   File "/usr/local/lib/python3.11/site-packages/fastapi/utils.py", line 90, in create_response_field
2023-01-09 14:02:49     raise fastapi.exceptions.FastAPIError(
2023-01-09 14:02:49 fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'starlette.responses.Response'> is a valid pydantic field type
```

copying and mounting scripts at the same time?

Thank you for your valuable videos which help others learn. I am a bit unclear on the following:

  • In your Dockerfile you have the following: COPY . /app
  • In the docker-compose you have: .:/app

What is the point of copying the directory when we will mount it under /app?

Does this suggest that when deploying it will not use this docker-compose and the code will be copied. Whereas we will use the local mount during development which is mounted using docker-compose. Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.