In real life, a container is a thing to hold something. Put your application in the container, the environment in the container you can freely customize, you can install anything you need in the configuration application, and then you can take the container of the application and run it anywhere, where you don't need to know what's inside the container, it just needs to know how to run the container. Docker provides a range of tools to make it easier to put applications in containers.
Mirrors and Containers
Mirrors and containers, image and container. Mirroring can be used to create containers, mirroring is a bit like a container template, for example, you create a container with a centos image, the environment in this container is equivalent to a centos system. There may be something pre-installed in the mirror, such as the Nginx image contains the Nginx Web server, and the MongoDB database is already installed in the MongoDB image. These mirrors are created based on the mirroring of certain system types, such as debian,alpine and so on.
We can also make mirrors, describe your mirror image in a dockerfile file, typically based on a system-type image, then describe what software to install, what configuration to do, what ports to advertise, and so on, and execute the Docker build command to generate this image. You can publish your own images in a public or private mirrored warehouse.
Internet
Network, network. The containers created by default have a bridged type of network that can communicate with each other, but the container is connected to the outside through the bridging network, exposing some ports of the container so that the outside world can communicate with him only through the port number that the container exposes. For example, a container created with a nginx mirror may need to advertise port 80 because this is the port number that the HTTP protocol uses by default.
You can specify which port on the host corresponds to the 80 port of the Nginx container. For example, you set the port 8080, assuming that the host IP is 192.168.99.100, then the user visits http://192.168.99.100:8080, the reception service will be nginx containers. The host is the machine that runs Docker.
We can also customize the container's network, and then let the specified containers into the custom network, so that in the network between the containers can communicate with each other, the custom network after the container can not directly communicate with the container inside the custom network. You can also have a container that belongs to multiple networks at the same time. Custom networks can be used to isolate containers, which is more secure. A container within a custom network can be accessed directly using the container's name, so that no IP address is required.
Data volume
Data volume, volumes. Sometimes there is some data to be shared between the container and the host, or between the container and the container. A container is created based on mirroring, the container has a container layer of storage, the data changes in the container will be placed on the storage of this container layer to retain, after we remove the container, the container layer stored on the things are gone. Some data needs to be preserved for a long time for example, MySQL database container database, you can create a data volume, mount the MySQL container to save the database, so that the data in the database will be saved to the host, that is, even if you delete the container, the data on its data volume will be retained.
We can also manually specify where the data volume is located on the host, and then mount it to a location in the container. This data volume is useful when developing applications, such as you can modify the application code at any time, and then immediately see the results of the changes in the container.
Docker Compose
General applications may require several containers of services, such as a PHP application, the need for Web services, the use of nginx, the need for data services, you can use MYSQL/MARIADB, need to script interpretation services, you can use PHP-FPM. We can create these containers by hand using the Docker command, and you'll add some configuration options when you create them so that they can work together to serve PHP applications.
A better approach would be for us to write clearly in a file what containers the application needs to provide, whether they are created using ready-made mirrors, or they are made by themselves, the port number that the container exposes, the data volumes used, and the network they belong to can be written clearly in the file. You can then use a command to start all of the containers that are well defined. This is Docker Compose, the name of the file that describes the configuration is docker-compose.yml.
Mirrored Warehouse
Docker Hub is the Docker company's official Docker Image warehouse, which stores the official mirror, as well as a lot of user-made mirrors, you can also push the image of their own production to the warehouse. The Open mirrored warehouse is free, but the private mirrored warehouse is charged. There are also domestic service providers to provide this mirrored warehouse services, such as Aliyun, recommended in the domestic use, because the upload or download mirror speed will be faster, in the Aliyun of the Mirror warehouse, you can also find the Docker Hub of all the mirrors.
Using Docker
Windows user Downloads install Docker for Windows (https://docs.docker.com/docker-for-windows/) and is currently a beta version.
The MacOS user downloads the install Docker for MAC (Https://download.docker.com/win/beta/InstallDocker.msi) and is currently a beta version.
Linux User download installation docker for Linux (https://docs.docker.com/engine/installation/linux/)
Docker for Windows and Docker for MAC are tools that are used on desktop computers, including Docker and Docker Compose, which also provides an interface to start configuration Docker.
Before recording a few Docker courses, the use of the Docker Toolbox, which also contains Docker and related tools, it also needs to use Virtualbox. It is now recommended that you use the Docker for Windows or Docker for Mac because they use native virtual technology.
PHP Application Development environment
I introduced the use of Docker to create a local environment for developing PHP applications in the Docker container application. Using the NGINX,MARIADB,PHP-FPM, you can run general PHP applications, such as laravel,drupal,wordpress and so on. Below the command line, the steps are as follows:
CD Desktop
git clone git://github.com/ninghao/nest
CD Nest
Docker-compose Build
Docker-compose up-d
After startup, open the browser, enter localhost:8080, you can open the Web service, Access localhost:8080/phpinfo.php, you can open the information page of PHP.
In the build service, will automatically download the PHP package management tool composer, due to domestic network problems, may fail, can build several times, or find ways to scientific Internet:
Run Laravel
Go to the console service and then use composer to install the laravel.
Docker-compose EXEC Console Bash
Cd/mnt/app
Composer Create-project--perfer-dist Laravel/laravel laravel
After the completion of the configuration Nginx, open images/nginx/config/default.conf, modify the value of the root directive, set to/mnt/app/laravel/public. Then restart the Web service.
Docker-compose Restart Web
Access to localhost:8080, you can open the Laravel welcome interface. Note that when configuring the Laravel database, the host of the database is not localhost, but db, it is the name of the data service, which is defined in DOCKER-COMPOSE.YML.
Summarize
I'm going to learn something new. Don't be frightened by it, we should embrace it fresh. Docker is definitely something that every developer needs to know.