First, based on the official website Dockerfile build image

concept

Dockerfile reconstructs a new image based on a series of scripts or commands. Of course, the most basic underlying image of these scripts or commands (usually with operating system-level encapsulation of the underlying image: For example, centos, and nginx image we enter the internal structure can see it for the directory has the relevant operating system level directory structure, so it can be explained that all the basic images are generally operating system level basic image).

Image Construction scenario

  • 1: provide a consistent development environment for developers.

  • 2: Seamless migration of operation and maintenance personnel deployment.

  • 3: Testers make related extensions or build new images based on the built images.

Common commands

The command role
FROM IMAGE_NAME:TAG Based on which image to start the build process (the premise is that the image must exist first, otherwise it will pull)
MAINTAINER USER_NAME Declare the creator of the image (no matter, make some remarks or copyright notes)
ENV ke vlaue Set the environment variable of the current mirror, (multiple Settings can be written)
RUN COMMAND Command execution is a core part of Dockerfile.
ADD source_dir/file dst_dir/dir Copy the host file to the container (if the copied file is a compressed file, it will be automatically decompressed after copying).
COPY source_dir/file dst_dir/dir Copy the host file to the container (but if the copied file is a compressed file, it will not be automatically decompressed)
WORKDIR parh_dir This section describes how to set a working directory in which all subsequent commands are executed
CMD To run a program or command

Addendum to CMD:

CMD is similar to the RUN command and is used to RUN programs, but at different points in time: CMD runs when docker runs. RUN is in the Docker build. What it does: Specifies the default program to run for the started container. The container ends when the program ends. Programs specified by CMD directives can be overwritten by programs specified to run in the Docker run command line argument. Note: If there are multiple CMD directives in a Dockerfile, only the last one takes effect.Copy the code

Simplest practice fastAPI Docker servitization

  • 1) Project structure planning:

  • 2) Write Dockerfile (based on the basic image provided by FastAPI)

Method 1:

FROM tiangolo/ uvicorn-gunicorn-fastAPI :python3.7 COPY./main.py /appCopy the code

Py /app COPY./main.py /app COPY the current directory main.py to /app

  • 3) Upload the project to the appropriate directory for building

  • 4) Go to the appropriate docker_fastAPI directory and build the image

Dockerfile = ‘Dockerfile’; Dockerfile = ‘Dockerfile’;

[root@localhost docker_fastapi]# docker build -t dfastapi .

View the image file that is successfully built:

[root@localhost docker_fastapi]# docker image ls -a | grep fast
dfastapi                            latest      e75f29899cf9   23 minutes ago
tiangolo/uvicorn-gunicorn-fastapi   python3.7   86ade7dea2c7   7 weeks ago
[root@localhost docker_fastapi]#

Copy the code
  • 5) After the completion of the construction, the relevant image container startup test

View the built image:

[root@localhost docker_fastapi]# docker run -d –name myfastapiserve -p 80:80 dfastapi

  • –name myFastapiserve The container name is myFastapiserve
  • -p 80:80 port mapping

Temporary startup test, which can be used to view internal startup, convenient preview service startup error

Key points: -it –rm

[root@localhost docker_fastapi]# docker run -it --rm -p 80:80 dfastapi Checking for script in /app/prestart.sh Running script /app/prestart.sh Running inside /app/prestart.sh, you could add migrations to this file, e.g.: #! /usr/bin/env bash # Let the DB start sleep 10; # Run migrations alembic upgrade head {"loglevel": "info", "workers": 2, "bind": "0.0.0.0:80", "graceful_timeout": 120, "timeout" : 120, "keepalive" : 5, "errorlog" : "-", "accesslog" : "-", "workers_per_core" : 1.0, "use_max_workers" : Null, "host", "0.0.0.0", "port" : "80"} [2021-08-02 04:16:07 +0000] [1] [INFO] Starting Gunicorn 20.0.4 [2021-08-02 04:16:07 +0000] [1] [INFO] Listening At: http://0.0.0.0:80 (1) [2021-08-02 04:16:07 + 0000] [1] [INFO] Using the worker: uvicorn. Workers. UvicornWorkerCopy the code

Add a non-temporary boot case with CTRL + C to forcibly end the boot again

Docker container start docker container start docker container start Docker container Stop docker container StopCopy the code

6. Access service address verification

7. Use based on running containersdocker commitCommand to build a new image

View a list of containers that have been created

[root@localhost docker_fastapi]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5dbce938447b Dfastapi "/start.sh" 32 minutes ago Exited (0) 18 minutes ago myFastapiserve 3bd91733a27c Ubuntu :15.10 "/bin/echo 'Hello Send..." 2 days ago Exited (0) 2 days ago pedantic_dirac 86eb9e903b4a hello-world "/hello" 2 days ago Exited (0) 2 days ago objective_nobel [root@localhost docker_fastapi]#Copy the code

Create a new image based on an existing container

[root@localhost docker_fastapi]# docker commit -m "fastapiserve_image" -a "xiaozhong" 5dbc zyxfastapi:v1
sha256:fd7e914200f3ec5885171c405083341537af46501e5dc8c074405dfdcdddd177
[root@localhost docker_fastapi]#

Copy the code

Parameter Description:

  • -mOption specifies the commit information for the new image,
  • -aMark the author information,
  • b66Is the container ID
  • myubuntu:v1Is the name of the specified new mirror.

View the new image file generated:

8. Test and verify the new container instance based on the new image

Run a new container with a new image:

[root@localhost docker_fastapi]# docker run -d --name zyxfastapi_contacin -p 80:80 zyxfastapi:v1
4f1951c2582fa2e146b10b8d43b1293215a0445b3e7682dfc7a82793854dc737
[root@localhost docker_fastapi]#

Copy the code

Test verification:

Other tests:


[root@localhost docker_fastapi]# docker rm 4f
4f
[root@localhost docker_fastapi]# docker run -d --name zyxfastapi_contacin -p 8080:80 zyxfastapi:v1
0f3ba7a5c1e910ec74ad7f334e612f21f1858554f8b996fdb0ea5400efdcae35
[root@localhost docker_fastapi]#

Copy the code

2, custom Dockerfile build image

1. Project scaffolding:

2: service startup entry main.py file:

#! The/usr/bin/evn python # - * - coding: utf-8 - * - "" "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the file name: the main file function description: Founder of functional description: let students creation time: 2021/7/15 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 2021/7/15 modify description: ------------------------------------------------- """ from apps.app import FastSkeletonApp def create_app(): Create our Fastapi object :return: ''' # from apps.middleware.logroute import ContextIncludedRoute # self.app.router.route_class = ContextIncludedRoute app = FastSkeletonApp() # return app.startge App =create_app() if __name__ == '__main__': Routes_helper import print_all_routes_info print_all_routes_info(app # # log_level="info", # # log_config=LOGGING_CONFIG, import uvicorn uvicorn.run('main:app', port=8080, debug=True, reload=True, access_log=True,workers=1, use_colors=True)Copy the code

3: write Dockerfile

FROM python:3.7 WORKDIR /app COPY requiredings.txt./ RUN PIP install --no-cache-dir --default-timeout=100 -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple COPY . . CMD [ "python", "./main.py" ]Copy the code

4: Upload to our server (image generation)

5: Go to the directory and perform image generation

[root@localhost fastapi_framework_skeleton]#  docker build -t fsfastapi .
Copy the code

6: Start a container test validation of the image

Start the test can see the log container, used for troubleshooting!

[root@localhost fastapi_framework_skeleton]# docker run -it --rm --name zidingfas -p 80:80 fsfastapi
Copy the code

A bunch of mistakes:

7: Mount external files for troubleshooting

View specific log information

The main reason is that when the framework of my scaffold starts, it needs to be connected to our local Redis service, but the container does not have this REDis service drop, so the link will fail!

Try to annotate the redis connection first and then orchestrate the entire service deployment with multiple containers.

Delete the image and generate a new image:


[root@localhost fastapi_framework_skeleton]# docker rmi fsfastapi

Copy the code

Booting is still wrong, the wrong question is:

Asyncio. CancelledError was not caughtCopy the code

To troubleshoot errors and avoid regenerating the image, use mount mode to test the external file modification mapped to the internal container:

Startup Mount startup mode:

[root@localhost fastapi_framework_skeleton]# docker run --name zidingfas -v /data/server/fastapi_framework_skeleton/:/app -it --rm  -p 80:80 fsfastapi

Copy the code

8: correction

The error is my asynchronous processing of the event processing, a batch of tasks to cancel the operation of the error exception!

from fastapi import FastAPI from apps.ext.logger import logger import signal import asyncio from apps.utils.singleton_helper import Singleton @Singleton class ExitAppCleanTasksHandler(): def __init__(self, app=None, *args, **kwargs): super().__init__(*args, **kwargs) if app is not None: self.init_app(app) def init_app(self, app: FastAPI): pass @app.on_event("startup") async def startup(): Pass Logger. info(' Application started! ') loop = asyncio.get_event_loop() try: signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT) for s in signals: loop.add_signal_handler(s, lambda s=s: asyncio.create_task(shutdown(s))) logger.info('startup end') except AttributeError: Import warnings # warnings. Warn (" system compatibility problem, no corresponding signal attribute! ") ) @app.on_event("shutdown") async def shutdown(signal: signal): try: CancelledError logger.info("Received exit signal %s... . signal.name) tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()] [task.cancel() for task in tasks] logger.info("Canceling outstanding tasks") await asyncio.gather(*tasks) except: passCopy the code

In fact, this is no need to carry out such a batch operation exit drop! Comments are normal after the plugin is initialized!!

In addition, we are starting the inside of 127.0.0.1, which needs to be modified:

Modify our main file:

if __name__ == '__main__': Routes_helper import print_all_routes_info print_all_routes_info(app # # log_level="info", # # log_config=LOGGING_CONFIG, Import uvicorn uvicorn.run('main:app',host='0.0.0.0', port=8080, debug=True, reload=True, access_log=True,workers=1, use_colors=True)Copy the code

9: Start the test again:

Modified startup:


[root@localhost fastapi_framework_skeleton]# docker run --name zidingfas -v /data/server/fastapi_framework_skeleton/:/app -it --rm  -p 8080:8080 fsfastapi

Copy the code

View the startup display:

Now you can access it normally!

But the problem is that our service restarts every time it is accessed, which is a problem:

The reason is because of our open listening mode, listening to the file changes on the restart!

So modify our main file:

It’ll be perfect again!

10:

The above deployment only packaged our project into Docker, but our project also uses services such as Redis and PostgresQL. They are mutually dependent and can be accessed normally. In this case, we need to start all the services. But can all services be docker-like? Then access each other inside the container, which must be possible without a doubt! Ha ha

In addition, the disadvantages of the above method are as follows: 1: image file theft 2: Redis cannot be accessed from inside the container, because redis has databases and so on.

Docker compose compose

3.1 role

Docker compose is a service composing tool for docker, which is composed for complex docker-based applications. Docker compose is composed by composing a configuration file to manage multiple Docker containers. Or the management and orchestration of container clusters, which are ideal for scenarios where multiple containers are combined for development.

Dockerfile is used to build Docker images, docker-compose is used to create containers. Docker-compose is mainly used in the construction of docker Run command encapsulation execution problems.

3.2 Function Example description

Usually, we are actually a complete project is the need to integrate several services node including App services, database services, and caching services, etc., we again at the early stage of the project development, in order to develop our environment and convenient testing, good actually can orchestrate our service node, all services are integrated into a container, This is also convenient for our development environment migration and construction, do not need to call too much time to build the environment. Of course, it is definitely not recommended for online production environment, after all, the purpose of Docker itself is to serve independence and environmental isolation!

Test environment building:

\

image.png

In an online environment, it’s going to be a separate container

3.3 Docker Compose installation

1 Download and Install: / root @ localhost ~ # curl - L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose- ` uname -s`-`uname -m` > /usr/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 423 100 423 00 221 0 0:00:01 0:00:01 --:-- -- 221 100 15.4m 100 15.4m 00 4427K 0 0:00:03 0:00:03 --:--:-- 10.0m 2: : [root@localhost ~]# chmod +x /usr/local/bin/docker-compose [root@localhost ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose [root@localhost ~]# docker-compose docker-compose version 1.24.0, build 0aa59064 docker-py version 3.7.2 CPython Version: Docker-compose OpenSSL 1.1.0j 20 Nov 2018 [root@localhost ~]# docker-compose v docker-compose version 1.24.0, build 0aa59064 [root@localhost ~]#Copy the code

3.4 Docker compose command

[root@localhost web_statistics]# docker-compose Define and run multi-container applications with Docker. Usage: docker-compose [-f <arg>...]  [options] [COMMAND] [ARGS...]  docker-compose -h|--help Options: -f, --file FILE Specify an alternate compose file (default: docker-compose.yml) -p, --project-name NAME Specify an alternate project name (default: directory name) --verbose Show more output --log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) --no-ansi Do not print ANSI control characters -v, --version Print version and exit -H, --host HOST Daemon socket to connect to --tls Use TLS; implied by --tlsverify --tlscacert CA_PATH Trust certs signed only by this CA --tlscert CLIENT_CERT_PATH Path to TLS certificate file --tlskey TLS_KEY_PATH Path to TLS key file --tlsverify Use TLS and verify the remote --skip-hostname-check Don't check the daemon's hostname against the name specified in the client certificate --project-directory PATH Specify an alternate working directory (default: the path of the Compose file) --compatibility If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent Commands: build Build or rebuild services bundle Generate a Docker bundle from the Compose file config Validate and view the Compose file create Create services down Stop and remove containers, networks, images, and volumes events Receive real time events from containers exec Execute a command in a running container help Get help on a command images List images kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pull service images push Push service images restart Restart services rm Remove stopped containers run Run a one-off command scale Set number of containers for a service start Start  services stop Stop services top Display the running processes unpause Unpause services up Create and start containers version Show the Docker-Compose version information [root@localhost web_statistics]#Copy the code

Extended record:

Docker-compose -f docker-compose. Yml up -d ## Stop docker-compose -f docker-compose. Yml stop ## Stop docker-compose and delete docker-compose docker-compose -f docker-compose.yml downCopy the code

3.5 Docker Compose starts two fastAPI service examples

In order to facilitate testing, local related images and containers are deleted first!

Docker rmI $(docker images -q)# delete all imagesCopy the code

Then create a new FastAPI image:

A fright! | image file a thief! Behind is definitely the need to optimize drops!

With the images in place, start a small practice of running container instances of two services.

3.5.1 File Layout

Services: fastAPI_web_01: image: fastAPI_web :latest build:. Container_name: fastAPI_web_API_01 restart: always ports: - "1245:8080" fastapi_web_02: image: fastapi_web:latest build: . container_name: fastapi_web_api_02 restart: always ports: - "1246:8080"Copy the code

3.5.2 Uploading a File

3.5.3 Start the container using the Docker Compose file

Go to the current uploaded project directory and execute:

[root@localhost fastapi_framework_skeleton]# docker-compose up
ERROR: yaml.scanner.ScannerError: mapping values are not allowed here

Copy the code

Error message:

The main problem is the YAML file format!

Alignment needed!

Services: fastAPI_web_01: image: fastAPI_web :latest build:. Container_name: fastAPI_web_API_01 restart: always ports: - "1245:8080" fastapi_web_02: image: fastapi_web:latest build: . container_name: fastapi_web_api_02 restart: always ports: - "1246:8080"Copy the code

Waiting for processing to build and start:

3.5.4 Access Verification

Conclusion:

The above is just a personal combination of their own actual needs, do study practice notes! If there are clerical errors! Welcome criticism and correction! Thank you!

At the end

END

Jane: www.jianshu.com/u/d6960089b…

The Denver nuggets: juejin. Cn/user / 296393…

Public account: wechat search [children to a pot of wolfberry wine tea]

Let students | article | welcome learning exchange together 】 【 original 】 【 QQ: 308711822