Configure docker for the project

Keywords: Linux MySQL Docker Nginx Redis

I've known about docker before, but I haven't configured it myself. This time I configure a docker for the project, and make a summary record here.

Dockerfile and docker-compose

First of all, in previous projects, docker is configured in the form of DockerFile + docker-compose. So, first of all, we need to understand the relationship between the two.

What we need to know is that if we only use dockerfile, we can complete the image configuration of docker. So why do we use docker-compose?

Initially, we can configure an image with just a few commands in docker. But there's a problem: it can't be saved. So we use dockerfile to record the process of creating the image, and then we just need to use:

docker build

You can create a mirror.

Now here's the problem: I want to create a complete project. It's not enough to have a mirror. For example, we need nginx, mysql, redis to start the project in our project.

Although we can also consider using a docker file and then write it as a mirror, this is not reusable. So it would be more reasonable for us to create a mirror for nginx, mysql and redis, and then use these images together.

docker-compose is to help us achieve this problem.

So, in summary, dockerfile is used to create a single image, while docker-compose is used to create a project.

Dockerfile

First, let's talk about the creation of Dockerfile. In this project, we only wrote one sentence:

FROM registry.cn-beijing.aliyuncs.com/mengyunzhi/nginx:1.13.12

FROM is the first command, and it is a necessary command, which specifies the base image.

Here, the basic image we use is a mirror hosted in the Ar docker warehouse.

We can also get mirrors from docker's official warehouse:

FROM nginx:1.13.12

But it may be slower to pull the mirror.

docker-compose

Docker-compose is implemented by configuring docker-compose.yml.

Declaration version

version: '3'

This is a statement of which version of the grammar we want to use. There are slightly different versions.

Declaration service

According to what we said above, we need three mirrors of nginx, mysql and redis, that is, three services:

services:
  alice.mysql:

  alice.nginx:

  alice.redis:

Then we take mysql as an example to illustrate what commands are needed to build the service.

First, we use the Docker file created above to build the captured image.

services:
  alice.mysql:
    build:
      context: ./mysql

The context option specifies the base image.

Then there's image:

services:
  alice.mysql:
    build:
      context: ./mysql
    image: mysql:5.7

Image specifies the name of the image used by the service. When we don't have the above build command, we will first look for local mirrors. If not, compose will try to pull the image.

ports:

services:
  alice.mysql:
    build:
      context: ./mysql
    image: mysql:5.7
    ports:
      - "3309:3306"

Map port 3306 of docker container to local port 3309.

environment:

services:
  alice.mysql:
    build:
      context: ./mysql
    image: mysql:5.7
    ports:
      - "3309:3306"
    environment:
      - MYSQL_USER=root
      - MYSQL_PASSWORD=
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_DATABASE=alice

In fact, I prefer to call this part environment variables, because this part is used to define variables.

Finally, because we want to combine multiple services to make the project run, we need to communicate between containers. So we need to use network to configure each service under the same LAN.

services:
  alice.mysql:
    build:
      context: ./mysql
    image: mysql:5.7
    ports:
      - "3309:3306"
    environment:
      - MYSQL_USER=root
      - MYSQL_PASSWORD=
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_DATABASE=alice
    networks:
      aliceNetwork:
        ipv4_address: 172.28.8.4

This uses ipv4_address to assign a static IP address to it.

Declaration network

Above we used the network, that is to define the network for services. In fact, before that, we need to define a network of our own:

networks:
  aliceNetwork:
    ipam:
      config:
        - subnet: 172.28.8.0/24

A subnet segment is declared here, so you can see that the mysql network above is 172.28.8.4, which is actually just a random selection from this segment.

Complete configuration file:

# version number
version: '3'

# service
services:
  alice.mysql:
    build:
      context: ./mysql
    image: mysql:5.7
    ports:
      - "3309:3306"
    environment:
      - MYSQL_USER=root
      - MYSQL_PASSWORD=
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_DATABASE=alice
    networks:
      aliceNetwork:
        ipv4_address: 172.28.8.4

  alice.nginx:
    build:
      context: ./nginx
    image: nginx:1.13.12
    volumes:
      - ./:/etc/nginx/conf.d
      - ./app:/usr/local/app
    ports:
      - 9000:80
      - 9001:81
    networks:
      aliceNetwork:
        ipv4_address: 172.28.8.3

  alice.redis:
    build:
      context: ./redis
    image: redis:alpine
    ports:
      - "6380:6379"
    networks:
      aliceNetwork:
        ipv4_address: 172.28.8.5

networks:
  aliceNetwork:
    ipam:
      config:
        - subnet: 172.28.8.0/24

Official Reference:
https://docs.docker.com/compo...
https://hub.docker.com/_/nginx

Posted by vicky57t on Fri, 17 May 2019 18:11:03 -0700