📜 ⬆️ ⬇️

Development for Docker. Local environment. Part 1

Perhaps one of the most basic reasons why I like docker is that it eliminates the need to install various services on a computer. These include the Apache or Nginx web server itself, databases and other components of the application infrastructure. The entire infrastructure is written in the docker-compose.yml configuration file and is launched as a team with your application. All that the developer working with the docker needs is, in fact, the docker himself and his favorite development environment and EVERYTHING!

For completeness of the further narration, you still have to briefly tell you what docker is and its basic concepts.

So, docker should be considered as some kind of virtualization and containerization system.
One of the basic concepts of a docker is an image. The image can be compared with a file (maybe even with an executable program file), which contains some information. Docker can run the image. The launched image is called a container. Multiple containers of the same image may be running.

So what is in the image?
')
There may be an operating system image. For example, the image of ubuntu. Maybe an image with a database, a web server and php and almost anything. To start this knowledge will be enough for us.

It is assumed that the reader has already installed docker itself and the docker-compose utility.

Let's start the deployment of our environment from simple to more complex.

Lesson number 1. Nginx installation


Let's try to install only one Nginx. Create docker-compose.yml as follows:

version: '3.0' services: nginx: image: nginx ports: - 80:80 

Next, execute the docker-compose up -d command in response should appear something like the following:

 Creating network "lesson1_default" with the default driver Creating lesson1_nginx_1 ... done 

Enter in the address bar of the browser http: // localhost / and our gaze should open the greeting "Welcome to nginx!". If so, you're on the right track.

What is going on here?

To understand the structure of the compose file, I recommend referring to the official documentation , despite the fact that it is in English the best source of information. The documentation describes all possible options that can be used.

Let's analyze the submitted file:


Let us analyze the image declaration in more detail, the directive image: nginx.

The main repository of all images is Docker Hub there are many different ready-made images (You can collect your own, but more on that later). Announced image of nginx is one of them.

As for the "forwarding" ports. If you specify a 80:80 match, as in this example, then nginx will be available at localhost: 80 or just localhost. If port 80 is already busy, you can specify 8080: 80. Then the site will be available at localhost: 8080. And accordingly, if you completely forgot to specify this ports directive, then the port will be available only inside the container and nginx will no longer be available through the browser.

The container is running. And how to work with him?

The installation of the Web server assumes that we want to use it to receive and view the HTML pages of the site. There is a question. How can I transfer any html files to the container? This will help us volumes

volumes


Let's take our docker-compose.yml to the following form:

 version: '3.0' services: nginx: image: nginx ports: - 80:80 volumes: - ./html:/usr/share/nginx/html 

A new directive appeared here called volumes, which says that the local ./html folder is being mounted into the container at / usr / share / nginx / html.

When mounted, the folder at the specified address inside the container is replaced with the folder from the local computer.

To make it work, we will create the html folder on the same level as the docker-compose.yml file and add the index.html file with free text to it. For example, Hello from Docker!

And do the re-creation of the container with the same command docker-compose up -d
Docker performs the re-creation of the container.

 Recreating lesson1_nginx_1 ... done 

We check the result in the browser. And see: Hello from Docker! Everything worked out.

It is important to note that the mounted folder is available for real-time changes. Those. if we change the text in the index.html file or add a new file to the folder, all these changes will immediately be available inside the container. This important feature allows you to develop through docker. We make changes on our computer to mounted files and folders and they are immediately displayed in the docker's container.

Part 2 - Nginx + PHP + MySql + phpMyAdmin

Source: https://habr.com/ru/post/459972/


All Articles