This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

preface

background

Recently, I was helping my tutor to write a project. There were four members in the team, and the front and back of the project were separated. If waterfall development is selected :(in the case of the agreed interface) students A and B write the front end, students C and D write the back end, and then agree on A time for unified joint adjustment, and finally deliver the project to the client machine.

In combination with the school background, there are several problems in the development process:

  1. Project requirements may change from time to time, and customers want to see results over time
  2. After the completion of the development of the front and back end, the workload of joint adjustment is large, easy to make mistakes, and the development of bugs is easy to accumulate, and become more and more intense

If the front and back end can realize the simultaneous development and adjustment, the effect of a small function can be immediately seen and the corresponding test can be carried out, so that even if the requirements change in the middle of the process, the modification can be made quickly, the customer can also preview the phased results, and the demand adjustment is more targeted.

In that case, can I just push a backend feature into a Git repository and deploy it so that the front end can use the interface and do some testing? Wouldn’t that fulfill my purpose? Just having to manually deploy the project as soon as you develop a small feature is a bit more work… But I can liver!

Of course, talking about this will lead to the protagonist of this paper: Docker + Jenkins, use Jenkins to monitor the changes of git repository, once the changes occur, automatically pull git repository code, build docker image, and then automatically deploy, run the container. As long as the back end pushes the code, the new version of the project will be automatically deployed by Jenkins to the specified server (the same for the front end), so that when a function of the back end is developed (Jenkins automatic deployment), the front end can use the interface to develop (after the front end pushes, it will also be automatically deployed) and complete the subsequent related tests.

Of course, the Jenkins usage mentioned above is not standardized compared with the actual work. In the work, Jenkins deployment environment will be more subdivided: development environment, test environment, production environment, etc. But the basic process is the same.

plan

This article explains how to use Docker to build our project file as an image, and then deploy it to the server of Tencent Cloud (equivalent to completing a deployment manually), and then conduct an external network access test.

The next article will explain the function of using Jenkins + Docker to complete the automatic deployment project. If I write the third article, DOCker + Jenkins + K8S will deploy the project to the cluster and complete the automatic deployment process (which is largely biased to the work of operation and maintenance engineers).

Of course, I am also learning these techniques, so please point out any mistakes in my writing

docker

The installation

  1. Install Docker Desktop on your computer: www.docker.com/get-started… Desktop will have a Docker logo display.

  1. Installed on the tencent cloud host docker: dockerdocs. Cn/engine/inst…

Project directory

The project is built by python’s Flask microframework, and the editor is PyCharm. However, the project requires Python, which would be the same if it were Go or Java, and can be completely ignored here, because the project itself only has one app.py file, which defines an interface. To return Hello World, visit http://host IP address :5000/.

Docker Hub

First of all, please register an account on Docker Hub. This platform, just like GitHub, is a platform for everyone to publish Docker images. Then I create a Repository on my home page. I create a Repository named Wood-app-Backend for local image building, uploading the image to my Docker Hub, and logging in to Tencent Cloud host. Pull the image from the Docker Hub and deploy it to the cloud.

Dockerfile

Dockerfile is an image of a docker project, which is created by creating a Dockerfile in your project and then writing the logic to build the image. Each row is a keyword + several parameters, specific can consult novice tutorial: www.runoob.com/docker/dock…

An image is built from an original image. Here I went to the Docker Hub to find a python3.6 image as the base image
FROM quoinedev/python3.6-pandas-alpine:latest
# Working directory
WORKDIR /usr/src/app
# copy file
COPY requirements.txt ./
You can run the following command to download the python dependency package:
RUN pip install --no-cache-dir -r requirements.txt
Copy files from current directory to working directory
COPY.
# Project to run Python (container runtime commands defined first)
CMD ["python"."./app.py"]
Copy the code

Baize1998 /wood-app-backend:1.0, baize1998/wood-app-backend:1.0 Indicate the repository to which the image belongs and the corresponding Tag version (note the repository created on its own Docker Hub)

Docker build-t baize1998/wood-app-backend:1.0.Copy the code

Enter the command to see if the image was successfully built (docker command is still handled locally, that is, docker Desktop)

docker images
Copy the code

Upload the Docker image

Login Docker Hub

Docker login -u Username -p PasswordCopy the code

Upload docker image (since the name has been corresponding to the name of our Docker Hub warehouse when building the image, it can be pushed directly)

Docker push baize1998 / wood - app - backend: 1.0Copy the code

Docker Hub’s wood-app-Backend repository already shows a 1.0 tag (push an image with a 2.0 tag will also appear in this repository)

The cloud server pulls the image

(First make sure you have installed Docker on the cloud server, refer to the document I gave above) also need to log in docker Hub first

Docker login -u Username -p PasswordCopy the code

The image latto just uploaded to our own Docker Hub

Docker pull baize1998 / wood - app - backend: 1.0Copy the code

Check whether the Docker image is pulled successfully

docker images
Copy the code

Run the container with an image file

#The -d parameter indicates background runningDocker run -d -p 55:5000 Indicates the image ID#Add a few common commands
#Enter a running Docker container (because a container may be running in the background, you can enter to see how the container is running)Docker exec it Image ID sh#orDocker exec -it#orDocker exec-it imageid /bin/bash#Exits from the container
exit
#You can also run commands to check the port usage of containers to check whether containers are running successfully
Copy the code

Extranet access test

Access success, manual use of Dockerfile to build docker image and deployment process is demonstrated

conclusion

The Jenkins service itself is also deployed using docker images, so I wrote this article to smooth the use of Jenkins, and we will see you next time

Set up a spring and autumn recruitment preparation/internal push/chat group, welcome to join.

P3-juejin.byteimg.com/tos-cn-i-k3…

Pay attention to the public number [programmer Bai Ze], take you to a bit of chatty programmer/student party.

P3-juejin.byteimg.com/tos-cn-i-k3…