Vagrant has been used as my own development environment, and my previous company also pushed everyone to adopt Vagrant as their development environment to ensure that the company uses the same development environment. With the popularity of Docker, more and more people are running their own projects on docker. It is also very convenient to simulate various online clusters by using Docker, which will be more efficient than virtual machines in both construction and operation.

There are a lot of information about docker environment construction on the Internet, but I have not carried out the practice. Paper come zhongjue shallow, only after their own hands, in order to be more familiar with the relevant concepts. It took nearly two days to build up the development environment of Docker and gain a deeper understanding of many of its concepts. Sort out the whole process, so that you can fill in the gaps later.

Here’s how to start the Docker journey. I plan to use Docker to complete the following tasks:

  • Basic environment construction: MySQL, Redis, Nginx;
  • Build PHP and Golang development environment;
  • Build the master and slave of MySQL and Redis;
  • Explore how to monitor processes in Docker.

This section describes related commands

This is just a working note, not a knowledge of the Docker principle or anything, but how to apply it. To facilitate subsequent understanding, related commands are introduced here.

Find the mirror

➜ ~/home/dockerenv >docker search mysql
Copy the code

This command is used to find the mysql image. You can replace mysql with the name of any other mirror you want to find. The common options of this command are -s. You can set the search criteria: how many mirrors above start.

➜ ~ / home/dockerenv > docker search-s 100 mysql
Copy the code

Search for mysql images with more than 100 starts.

Access to the mirror

docker pull centos7
Copy the code

Git clone this command will clone the image locally, just like git clone. The idea of what a mirror is, what a container is, is outside my scope.

Viewing and deleting mirrors

➜ ~/home/dockerenv >docker image ls
Copy the code

All currently locally installed images are listed. Sometimes a mirror we do not use, want to kill it, for the hard disk, you can use the following command:

➜ ~/home/dockerenv >docker rmi 0d16d0a97dd1 # is the image ID
Copy the code

Create an image – Dockerfile

Docker image can be directly pulled from the official, but also through Dockerfile for customization, write Dockerfile file, you can execute the following command to run the image.

➜ ~/home/dockerenv >docker build -t nginx:1.14.0Copy the code

There is a. At the end of the file that represents the context, not the path to the Dockerfile. For example, the Copy directive is often used in Dockerfile, and the files it copies must be in this context.

The main purpose of Dockerfile is to re-customize the image from the base image, rather than pulling it from the official repository.

Yeasy.gitbooks. IO /docker_prac…

Start the container

➜ ~/home/dockerenv >docker run -it --rm PHP :7 bashCopy the code

The command above starts a container and assigns a dummy terminal, which will be deleted upon exit.

➜ ~ / home/dockerenv > docker run-d -p 9000:9000 php:7
Copy the code

This command will run a container in the background. The container will not be deleted because of exit. You can repeat the start and stop operations.

Docker-compose + Dockerfile is used to build the docker-compose environment. Because according to best practice: a container inside contains only a single process, like the one above I need to install: PHP/Golang/Redis/Nginx/Mysql, etc., even if not master-slave also need to start the 5 containers, each time I start to be dead tired, also don’t say need to manage the interconnection between the container. Therefore, I use Compose to define and run multiple Docker container applications.

Viewing Container Information

➜ ~/home/dockerenv >docker inspect a49dfb2e6f45 # image id
Copy the code

Using this command, you can view the complete container information. I use this command mainly to check the container network and IP during container interconnection.

Into the container

➜ ~ / home/dockerenv > dockerexec -it e8d740a6ac7a bash # image id
Copy the code

The command above can enter the container and make you feel like you’ve SSH to a remote machine.

Stop, start

➜ ~/home/dockerenv >docker stop a49dfb2e6f45 # image id
Copy the code

If the container is started in the background, you can stop the container in this way. After stopping, you can start the container with the following command:

➜ ~/home/dockerenv >docker start a49dfb2e6f45 # image id
Copy the code

View and delete containers

Containers can also be viewed like mirrors

➜ ~ / home/dockerenv > docker psCopy the code

The command above shows only the mirrors that are started. To view all mirrors, add the -a option. If a container is no longer needed, you can use the following command to delete it:

➜ ~/home/dockerenv >docker rm 0d848bc87fe7  # image id
Copy the code

Container deletion does not affect the image, which can continue to be used to start new containers. If a container created by an image is not deleted, the image cannot be deleted directly. You need to delete the container before deleting the image.

Start and stop in Compose

For docker compose start and stop using the following command, PERSONALLY feel that this is batch operation, after all, there are too many containers, operation one by one is too troublesome, easy to miss and other problems.

Batch start:

➜ ~ / home/dockerenv > docker - compose up-d
Copy the code

While this command is not a pure start container, it is very powerful and will attempt to automate a series of operations including building an image, (re) creating a service, starting a service, and associating a service-related container.

If you are done, you can also use the following command to start the created container.

➜ ~ / home/dockerenv > docker - compose startCopy the code

To stop the container, use the following command:

➜ ~ / home/dockerenv > docker - compose the stopCopy the code

Of course, these commands must be run in the same directory as the docker-comemage. yml file, otherwise it will not know which containers to start or close.

Docker-comemage. yml: docker-comemage. yml: docker-comemage. yml

Docker Compose builds the environment

Docker Compose can easily manage multiple containers, which is called the orchestration technology.

At the heart of the choreography technique is the template file docker-comemage. yml. It defines the mirror, data volume mount path, port, network, and so on for each container in the container cluster.

In order to build the next environment to illustrate, below I will build a PHP7 development environment, need Nginx/ php-fpm /MySQL/Redis to cooperate. So you need to start four containers. We also need four mirror images. The directory structure for the entire environment is as follows:

├── ├─ data ├─ ├─ all exercises, all exercises, all exercises, all exercises, all exercises ├ ─ ─ the conf │ └ ─ ─ the SRC ├ ─ ─ php7 │ ├ ─ ─ Dockerfile │ ├ ─ ─ docker - compose. Yml │ ├ ─ ─ etc │ ├ ─ ─ extensions │ └ ─ ─ the SRC ├ ─ ─ redis │ ├ ─ ─ Dockerfile │ ├ ─ ─ the conf │ ├ ─ ─ data │ └ ─ ─ the SRC ├ ─ ─ start. Sh ├ ─ ─ stop. Sh └ ─ ─ WWW └ ─ ─ the ABCCopy the code

My MySQL is directly used by the official provided image resources, while Redis/Nginx/PHP for customization, I use Dockerfile for customization. Each individual service is described separately.

The MySQL service

Dev. Mysql. The SRV: image: mysql: 5.7.22 volumes: -)/mysql/data: / var/lib/mysql - / mysql/conf: / etc/mysql/conf. D ports: -"3307:3306"
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: 123123
  networks:
    - default
Copy the code
  • Image: specifies the dependent image. Here is the image resource provided by Docker.
  • Volumes: Set the paths for mounting data volumes, including the paths for storing MySQL data and configuration files. Its main function is to persist data and avoid internal data loss after container destruction.
  • Ports: Ports exposed to the host. This is not much to say, just to be able to access the services inside the container from the host;
  • Restart: This command is used to set the container restart policy if the container exits under certain circumstances.
  • The environment:To set the environment variables for the mirror, you can enter the mirror by:echo $MYSQL_ROOT_PASSWORDView its value;
  • Networks: Set up the network, so that all containers in a network, convenient container communication.

One other thing to note here is that dev.mysql.srv is the name I gave to the mysql service. Choose a good name for subsequent container interconnections, such as this name, if the PHP code needs to fill in mysql host. I can just use it.

Other services

The remaining three services are configured in much the same way. Let me put it together.

Dev. Nginx. SRV: image: lei_nginx:1.14.0 Build:./nginx volumes: - ./nginx/conf:/home/work/app/nginx/conf - ./www:/home/work/www ports: -"80:8080"
      - "443:443"Restart: always networks: -default depends_on: -dev.php -fpm.srv dev.redis. SRV: image: lei_redis:3.2.11 build: ./redis volumes: - ./redis/conf:/home/work/app/redis/conf - ./redis/data:/home/work/app/redis/data ports: -"6379:6379"Restart: always networks: -default dev.php-fpm. SRV: image: lei_php:7.2.6 build:./php7 volumes: - ./php7/etc:/home/work/app/php/etc - ./www:/home/work/www ports: -"9000:9000"
  restart: always
  networks:
    - default
Copy the code

One of the biggest differences with MySQL is the build option. So that’s what I’m talking about these three images that I customized with Dockerfile. You can see Dockerfile in the corresponding build directory.

Another thing to note is the depends_on option configured in Nginx, which specifies the dependency because I configured PHP-fPM in Nginx. So before it starts, make sure that phP-fpm is started before it can start normally.

Start the

To cut down too much on the conceptual stuff, you can pull this configuration directly from Github and run it

➜ ~/home/dockerenv >./start.sh Starting dockerenv_dev.php-fpm.srv_1...done
Starting dockerenv_dev.redis.srv_1   ... done
Starting dockerenv_dev.mysql.srv_1   ... done
Creating dockerenv_dev.nginx.srv_1   ... done
Copy the code

If you are running start.sh for the first time, it will also pull up the image from the Docker Hub and customize the image from the Dockerfile. So there’s a lot of output.

Then visit: http://localhost. You should be able to see phpInfo () output.

conclusion

Through my own familiarity, I have a full understanding of basic Docker commands and the three basic concepts of Docker: warehouse, image and container. After building an environment with Docker compose, I have a deeper understanding of the interconnection of containers, and have a deeper understanding of the system by running one process per container. In the process of using Dockerfile to customize images, through repeated build images, we have a lot of practice in understanding container layering, how to ensure smaller image volume, especially in the setup of ENTRYPOINT, we have realized the startup process of Docker.

Due to the space limitation, the content of Dockerfile is separated into a paper to explain, and some problems encountered are also sorted out.

My Docker environment: github.com/helei112g/d…

After replacement machine replacement machine, no longer worry

Wechat Official Account:



References:

  • Yeasy. Gitbooks. IO/docker_prac…