Build web server and APIs with C++ 1 – Docker

Web Servers and APIs using C++

Video link: https://www.linkedin.com/learning/web-servers-and-apis-using-c-plus-plus/building-crow?autoplay=true

Zero. Introduction

1. Goal:

How to build a website in C++ using MongoDB as a database, and Render/railway as a cloud server to deploy to the Internet.

2. Required skills

  • c++ intermediate knowledge
  • Familiar with using Docker
  • Familiar with using command line bash

1. Installation tool

1. Install Docker

?One of the difficulties of using C++ is getting used to the complexity of its build chain. Programs have separate build and link steps. Libraries must be installed in dev, build and production machines. Unknown error messages are displayed if any part is out of sync. . Docker solves this by creating an image with all the tools that need to be installed properly and using that image for both dev and prod.
?Install Docker Desktop: Docker official website
?I encountered a problem after installation: WSL needs to be updated. Follow the prompts to install the latest version and run it.
?After the installation is complete, you can use Docker Desktop on the desktop and Docker CLI on the terminal.
?In order to make git bash have multiple Tab windows, install Hyper (see the blog post under the installation tool for details), and use the command docker version in Hyper to display the version number of docker, indicating that the docker installation is successful.

2. Install c++ editor

Atom is used in the video, I use Vscode.

3. Create Dockerfile

?In bash, switch to the desktop (Desktop), use the following command to create a Dockerfile:

mkdir cppweb
cd cppweb
mkdir cppbox
cd cppbox
code.

?Create a new Dockerfile in the cppbox directory, edit it, and save it. See cppweb\cppbox\Dockerfile for details.
?Dockerfile is the blueprint that creates the docker image.
?Each line of command in the Dockerfile is: start with uppercase + + execute the command; if a line of command is too long, you can end it with \, and continue writing on the next line.
?Dockerfile is commented with # ,
where the error occurs:
Only one whole line can be commented, eg:#Update software list;
If you add # after the command line, the comment will not work, eg: RUN apt-get -qq update #Update the software list.

gcc is the C++ compiler and a set of tools; don’t use the latest version of gcc, it may not be compatible with the code.
RUN apt-get -qq update: Update the list of available packages without installing them.
RUN apt-get -qq upgrade: Update updatable installation packages
-qq: Quietly execute the command, answer “yes” to any installation prompts, and nothing will be printed; if you want detailed output, you can use “y” instead of “qq”.
RUN apt-get -qq install cmake: cmake is an open source cross-platform build tool that can be used to create make files, which are used to compile and link code.
RUN apt-get -qq install libboost-all-dev: Install the boost library, and the crow microframework depends on boost.

RUN apt-get -qq install build-essential libtcmalloc-minimal4 & amp; & amp; \
    ln -s /usr/lib/libtcmalloc-minimal.so4 /usr/lib/libtcmalloc-minimal.so`

: Install build-essential kit and TCMalloc minimal library, the former adds important dev tools to the image, and the latter is used to allocate and release memory.
The first Dockerfile is created.

4. Run Dockerfile

(1) Run command

?In bash, use the command cd cppbox to switch to the cppbox directory, there is a Dockerfile in it, run the following command, it will create an image from the Dockerfile:

docker build -t cppbox .

-t: Marking option, the image is marked as cppbox.

(2) Debugging process

error1: apt-get -qq updateThere is no release file in the warehouse

* error:

ERROR [2/6] RUN apt-get -qq update
#0 7.914 W: The repository 'http://security.debian.org/debian-security stretch/updates Release' does not have a Release file.
#0 7.914 W: The repository 'http://deb.debian.org/debian stretch Release' does not have a Release file.
#0 7.914 W: The repository 'http://deb.debian.org/debian stretch-updates Release' does not have a Release file.
#0 7.914 E: Failed to fetch http://security.debian.org/debian-security/dists/stretch/updates/main/binary-amd64/Packages 404 Not Found
#0 7.914 E: Failed to fetch http://deb.debian.org/debian/dists/stretch/main/binary-amd64/Packages 404 Not Found
#0 7.914 E: Failed to fetch http://deb.debian.org/debian/dists/stretch-updates/main/binary-amd64/Packages 404 Not Found
#0 7.914 E: Some index files failed to download. They have been ignored, or old ones used instead.

* Solution:
Change the gcc version from 7.3.0 to a new one: 12.3

error2: Install libboost-all-dev

* error:

[5/6] RUN apt-get -qq install libboost-all-dev=1.62.0.1:
#0 1.455 E: Version '1.62.0.1' for 'libboost-all-dev' was not found

* Solution:
Delete the version number, ie RUN apt-get -qq install libboost-all-dev

  • new error

* error:

Errors were encountered while processing:
#0 193.9 gfortran
#0 193.9 libcoarrays-dev:amd64
#0 193.9 libcoarrays-openmpi-dev:amd64
#0 193.9 E: Sub-process /usr/bin/dpkg returned an error code (1)

* Solution
Use --no-install-recommends, ie RUN apt-get install -y libboost-all-dev --no-install-recommends

error3: Unable to locate package xxx

* error:

#0 1.145 E: Unable to locate package built-essential

* Solution:
Built misspelling, correct it, RUN apt-get -qq install build-essential.
? If the error still occurs, disconnect the VPN connection.
The build image is successful, and the last four lines show:

exporting to image 3.9s
 => => exporting layers 3.9s
 => => writing image sha256:39d17a8f00093163866c75d7ea7d17bda4051819126934092198ca12a220d909 0.0s
 => => naming to docker.io/library/cppbox

(3) Check whether the libboost library is installed successfully

?To make sure the image builds correctly, check to see if it contains the boost library. Use bash on the container with the following command:

docker run -ti cppbox:latest bash

In the container, view all libboost libraries:

find /usr -name libboost*.*

If dozens of boost libraries are displayed, the installation is successful.
Exit the container with the exit command. At this point, the image is built and works normally.