Steps to build web service environment using docker

  • 2020-06-23 02:28:11
  • OfStack

preface

People who have done development should be familiar with the installation and configuration of the development environment. No matter what you do, you will have a certain dependence on the development environment. For PHP Web development, the development environment should have at least one Web server (such as Apache, Nginx), a database server (such as MySQL) and 1 PHP parser, if your PHP code using 1 some extend, that you also need to get your PHP fitted these extensions, so your PHP code can run smoothly. For a developer who just wants to quietly type code, the setup and configuration of a development environment can be tedious enough to go through once. However, things don't always work out the way people want them to, and you're guaranteed to get a new system or a new environment. In short, you will occasionally need to reinstall and configure your development environment!

You can recall how long it took you to configure your development environment the last time you installed it. Did you need the PHP extension until the runtime made a mistake and remember that it wasn't installed? Thankfully, with Docker, my mom doesn't have to worry about my development environment anymore. With Docker, the development environment only needs to be installed and configured once. After that, it is easy to set up the same development environment. It only needs 1 command to complete!

This is the purpose of this paper. It introduces the service environment of Docker installation and configuration, and realizes the functions of once installation and configuration, multiple reuse and cross-platform reuse.

Docker overview

What is Docker? Think of Docker as a container for packaging your environment. It can package your environment as an image and, when needed, create a container based on that image so that the required environment comes back. I just mentioned the two core concepts of Docker, mirror images and containers.

Docker images are applications made according to the rules of Docker for special purposes, somewhat similar to the installation packages in ES34en-ES35en. Containers are created based on images, and you can create several containers with different names but the same function based on one image. The mirror image is static and the container is dynamic.

In addition to mirroring and containers, there are two concepts you need to understand under 1, namely warehouse and ES38en-ES39en. The Docker repository is where the image is stored. We can pull the image locally from the Docker repository and then create containers based on the image. docker-compose is for managing containers. The idea of Docker is that one container does only one thing, and multiple containers may be used in one development environment, such as Web service environment that USES PHP, Nginx, MySQL, and so on. Then one Web service environment needs three containers, and there are dependencies between these containers, Nginx depends on PHP, PHP depends on MySQL. With ES54en-ES55en, these containers can be well managed by determining the starting and closing order of each container based on the dependencies between containers, managing port mappings between containers and host machines, configuring data sharing policies between containers and host machines, and so on.

A mirror image warehouse in China

Due to the geographical location, it is relatively slow to visit the official Docker warehouse in China, so here I introduce one domestic Docker warehouse: Lingke Cloud. Lingque Cloud image warehouse gathers a large number of high-quality works from the community, allowing users to combine and reuse containerized micro services and easily build a new generation of cloud applications.

Set up Web service environment

The focus of this article is to use Docker to build an Web service environment. If you don't already have Docker installed in your environment, please refer to this section to install it yourself.

After installing Docker, it is easy to set up Web service environment with only 1 file and 1 command.

1 document: ES76en-ES77en. yml


nginx:
 image: index.alauda.cn/library/nginx
 links:
 - phpfpm
 ports:
 - "80:80"
 - "443:443"
 volumes:
 - /Users/chenishr/www:/usr/share/nginx/html
 - ./nginx.conf:/etc/nginx/nginx.conf
 - ./nginx.d:/etc/nginx/conf.d

mysql:
 image: index.alauda.cn/library/mysql
 environment:
 MYSQL_ROOT_PASSWORD: qazasdedc123
 ports:
 - "3306:3306"

phpfpm:
 image: index.alauda.cn/library/php:7.0-fpm
 links:
 - mysql
 volumes:
 - /Users/chenishr/www:/var/www/html
 ports:
 - "9000:9000"

The above file is simple in that it defines three containers, nginx, mysql, and phpfpm. Each container definition contains 1 bit of information, which is briefly described here.

image: This field indicates that the container was created based on that image links: Indicates that the other container needs to be accessed inside the container. The name inside the container behaves like a domain name and binds IP for the container ports: Maps ports inside containers to host machines volumes: Configure the Shared file or directory between the container and the host machine

Article 1 the command

With the docker-ES95en. yml file above, it only takes one command to start all the containers and set up the entire Web service environment!


docker-compose up -d

The last

With Docker, setting up an environment has never been easier.

This article is just an introduction to the most basic application of the Docker, and it has many USES beyond this article. It enables multiple versions of software to work simultaneously, for example, I can use both PHP7 and PHP5 on one computer without confusion. Of course, there are others...


Related articles: