Docker Compose of Docker series

Table of contents

1. Docker Compose Overview (YML)

2. Docker Compose installation 

3. Docker Compose configuration common fields

4. Docker Compose common commands 

5. Create an image based on Compose 

1. First install Compose

2. Use Dockerfile environment:

3. Use docker-compose.yml to define the services that make up the application 

4. Execute the docker-compose up command

5. Check image and container status

6. Access the test after editing the homepage file


1. Docker Compose Overview (YML)

When we use Docker, we define the Dockerfile file and then use docker build, docker run and other commands to generate images and start containers.

However, the application system of microservice architecture generally contains several microservices, and each microservice usually deploys multiple instances. If each microservice has to be started and stopped manually, the efficiency will be low and the amount of maintenance will be heavy.

Containers can be managed easily and efficiently with Docker Compose, an application tool for defining and running multi-container Docker

Docker Compose uses YML files to configure all services required by the application, which is very suitable for scenarios where multiple containers are combined for development, and there is no need to use shell scripts to start the containers.
 

YML (emphasis):

1.YAML is an intuitive data serialization format with a markup language. Files usually have a .yml or .yaml suffix and are very readable.
2. Not supported Tab key indentation, you need to use spaces for indentation, use indentation to indicate hierarchical relationships
3. Usually 2 spaces are indented at the beginning, the number of indented spaces is not important, as long as they are at the same level The elements can be left aligned
4. Indent a space after the characters, such as colon, comma, and horizontal bar. If special characters are included, enclose them in single quotes
5. Use '#' to indicate comments

2. Docker Compose installation 

yum install -y  docker-ce

cd /usr/local/bin/ Drag the local software package into docker-compose
or
curl -L https://github. com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose -v        

3. Docker Compose configuration common fields

 4. Docker Compose common commands 

'Command format:'
docker-compose [options] [command] [parameters]

'Common options:'
-verbose: Output more debugging information
-version: Print version and exit< /span> -p: Specify the project name, the default is the directory name
-f: Use a specific compose template file, the default is docker-compose.yml

5. Create an image based on Compose 

 

1. First install Compose

2. Use Dockerfile environment:

mkdir -p /root/compose_nginx/nginx
cd /compose_nginx/nginx/
nginx-1.12.2.tar.gz

vim Dockerfile

FROM centos:7

MAINTAINER dark

RUN yum -y install pcre-devel openssl-devel zlib-devel gcc gcc-c++ make 

RUN useradd -M -s /sbin/nologin nginx

ADD nginx-1.12.2.tar.gz /usr/local/src

WORKDIR /usr/local/src/nginx-1.12.2
RUN ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module

RUN make && make install

RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
RUN ln -s /usr/local/nginx/conf/nginx.conf /etc/


RUN rm -rf /lib/systemd/system/nginx.service

ADD nginx.service /lib/systemd/system/nginx.service
RUN chown nginx.nginx /lib/systemd/system/nginx.service

RUN chmod 754 /lib/systemd/system/nginx.service

EXPOSE 80
EXPOSE 443


RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf

CMD ["/usr/local/nginx/sbin/nginx"]

3. Use docker-compose.yml to define the services that make up the application 

vim /root/compose_nginx/docker-compose.yml 
version: '3'
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 1111:80
      - 2222:443
    networks:
      - cluster
    volumes:
      - /root/compose_nginx/index.html:/usr/local/nginx/index.llhtml
networks:
  cluster:

----Detailed explanation----
version: '3' > networks:                                                                                                                                                                                                                                                                                                 networks: 12>                                                                                                                                                                                                                            of been been of been been been been been been been been been been developing been been been been been been. a i=14> - /root/compose_nginx/html:/usr/local/nginx/html '//Associate the files in the host's warehouse with the nginx homepage file (container)'       - 22222:443       -1111: 80 ' // ) port'     Ports: ' // Provide ports: '       dockerfile: Dockerfile '//Specify the Dockerfile file& #39;
services:                                                                                 ' 4> hostname: nginx                                                                                                                 ' 6> context: ./nginx '//The required materials are all in this directory (warehouse)'













4. Execute the docker-compose up command

docker-compose -f docker-compose.yml up -d

5. Check image and container status

docker images 

docker ps -a

6. Access the test after editing the homepage file

echo "<h1>hello world</h1>" > /root/compose_nginx/index.html //Edit web content Browser access [http://192.168.40.30:1111]

 

 

 

Guess you like

Origin blog.csdn.net/weixin_52269501/article/details/130331667