Querying Container Information

docker inspcet
Copy the code

Query information, including running status, storage location, configuration parameters, and network Settings.

Example Query the running status of a container

Docker inspect -f {{.state. Status}}Copy the code

Example Query the IP address of a container

Docker inspect - f {{. NetworkSettings. IPAddress}} [container]Copy the code

Example Query container log information ω

Docker logsCopy the code

-f Displays the latest logs in real time

Docker Stats looks at system resources such as CPU usage, memory, network, and disk overhead occupied by the container in real time

Log in to Docker Exec natively with container commands

Docker exec + container name + command executed in containerCopy the code

For example, to query all processes in container PHP:

docker exec php7-dev ps -ef
Copy the code

If you run multiple commands in the container consecutively, you can add the “it” parameter to the command. It is equivalent to logging in to the container as the root user. You can run consecutive commands and run exit to exit the container.

docker exec -it php7-dev /bin/bash
Copy the code

Multiple container management Multiple containers have data interaction, dependency, startup mode must be sequential, such as:

# db container takes precedence over WordPress startup
docker start db docker start WordPress
Copy the code

Docker Composer is a container orchestration tool that allows users to define a set of associated application containers in a template (YAML format). This set of containers will automatically sort startup priorities based on parameters such as “–link” in the configuration template. Multiple containers in the same service can be created and started in sequence by simply performing a single “Docker-Composer up”.

Installation mode, refer to the official

sudo curl -L "Https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s) - $(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Check out the Compose version
docker-compose --version
Docker-compose version 1.23.1, build b02F1306
Copy the code

For example, start a wordpress project, create a wordpress folder, and create a docker-composer. Yml file in the folder.

wordpress:
    image: wordpress
    links:
        - db:mysql
    ports:
        - 8080:80
db:
    image: mariadb
    environment:
        MYSQL_ROOT_PASSWORD: example
Copy the code

Links, ports, environment, and volumes correspond to “–links”, “-p”, “-e”, and “-v” in Docker Run respectively. Restart: always Always online

Docker-compose up: docker-compose up: docker-compose up: docker-compose up: docker-compose up Docker-compose up -d –no-create: docker-compose up -d –no-create: docker-compose up -d –no-create: docker-compose up -d –no-create Docker-compose will not be rebuilt. Docker-compose start docker-compose stop Docker-compose ps

The other options

  • Container_name Specifies the name of the container
image: php:7.0-fpm
   container_name:  php7-dev
Copy the code

The environment variable can be an array or a dictionary. The environment variable with only one key can be found on the Compose machine, which is useful for encrypting or special host values

environment:
  RACK_ENV: development
  SESSION_SECRET:
environments:
  - RACK_ENV=development
  - SESSION_SECRET
Copy the code

Env_file adds environment variables from a file, either a single value or a list, that will be overridden by the environment variables specified in the environment

env_file:
  - .env


RACK_ENV: development
Copy the code
  • net

Network mode, you can specify these values in the –net parameter of the Docker client

net: "bridge"
net: "none"
net: "container:[name or id]"
net: "host"
Copy the code
  • dns

Custom DNS service, can be a single value or a list

dns: 8.8.8.8
dns:
  - 8.8.8.8
  - 9.9.9.9
Copy the code

It can be seen that docker-compose is grounded in specific projects. When specific projects need to depend on the environment changes or production is deployed, docker-compose can be established in the root directory of these projects to solve the problem of how to manage docker-compose: Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose: docker-compose

  • Docker-compose contains basic docker commands:
Build build or rebuild the service help kill Kill the container logs Display the output of the container port Print bound open ports ps display the container pull pull the service image restart restart the service rm Delete the stopped container run Run a one-time command scale to set the number of containers for the service start Start the service stop Stop the service up Create and start the containerCopy the code

Such as restart nginx

docker-compose restart nginx
Copy the code

dockerfile

FROM image # indicates that the new image is from the base image imageMAINTAINER: specifies the image creator ENV: specifies environment variables COPY: copies local files of the compiler to the image file system EXPOSE: specifies the listening port ENTERPOINT: creates a container using the image command, which is executed when the container is started, for example, ENTRYPOINT ["php"."/var/www/code/easyswoole"."start"] RUN: Runs the shell commandCopy the code

easyswoole dockerfile

FROM php:7.1

# Version
ENV PHPREDIS_VERSION 4.0.1
ENV SWOOLE_VERSION 4.3.0
ENV EASYSWOOLE_VERSION 3.x-dev

# Timezone
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' > /etc/timezone

# Libs
RUN apt-get update \
    && apt-get install -y \
    curl \
    wget \
    git \
    zip \
    libz-dev \
    libssl-dev \
    libnghttp2-dev \
    libpcre3-dev \
    && apt-get clean \
    && apt-get autoremove

# Composer
RUN curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/local/bin/composer \
    && composer self-update --clean-backups

# PDO extension
RUN docker-php-ext-install pdo_mysql

# Bcmath extension
RUN docker-php-ext-install bcmath

# Redis extension
RUN wget http://pecl.php.net/get/redis-${PHPREDIS_VERSION}.tgz -O /tmp/redis.tar.tgz \
    && pecl install /tmp/redis.tar.tgz \
    && rm -rf /tmp/redis.tar.tgz \
    && docker-php-ext-enable redis

# Swoole extension
RUN wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
    && mkdir -p swoole \
    && tar -xf swoole.tar.gz -C swoole --strip-components=1 \
    && rm swoole.tar.gz \
    && ( \
    cd swoole \
    && phpize \
    && ./configure --enable-async-redis --enable-mysqlnd --enable-openssl --enable-http2 \
    && make -j$(nproc) \
    && make install \
    ) \
    && rm -r swoole \
    && docker-php-ext-enable swoole

WORKDIR /var/www/code

# Install easyswoole
RUN cd /var/www/code \
    && composer require easyswoole/easyswoole=${EASYSWOOLE_VERSION} \
    && php vendor/bin/easyswoole install

EXPOSE 9501

ENTRYPOINT ["php"."/var/www/code/easyswoole"."start"]
Copy the code

Build image, go to the Dockerfile sibling directory:

docker buil -t easyswoole:1.0
Copy the code

With Dockerfile, maintenance is easy, just modify the contents of the file, rebuild it, -t can also specify the version tag.

There are a few commands worth keeping an eye on:

docker-php-source
Copy the code

Create a directory in the PHP container called /usr/src/php and put some of your own files in it. Think of it as a repository for PHP extension source code downloaded from the Internet. In fact, all PHP extensions are stored in /usr/src/php.ext.

docker-php-source extract | delete
Copy the code

Parameter Description:

  • Extract: create and initialize the /usr/src.php directory
  • Delete: deletes the /usr/src/php directory

Docker-php-ext-install install pdo_mysql docker-php-ext-install install pdo_mysql

Docker-php-ext-enable enables the extension to add php.ini configuration, such as docker-php-ext-enable pdo_mysql

docker-php-ext-configure
Copy the code

Docker-php-ext-install is usually used with docker-php-ext-install. What it does is, when you install an extension and you need to customize the configuration, you can use it to do it for you.

FROM php:7.1-fpm
RUN apt-get update \
    Dependencies must be installed manually
    && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
    # Install extension
    && docker-php-ext-install -j$(nproc) iconv mcrypt \
    # If the installed extension requires custom configuration
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd
Copy the code

Docker Hub is a repository for publishing images, users can register an account in hub.docker.com/, both can publish mirrors…

// Log in to docker Hub
docker login Username: sun Password: 123456 Email:[email protected] 

// Upload the image
docker push easyswoole:1.0
Copy the code

In addition, due to network problems, security problems, you can also use a private warehouse, the specific execution command is not discussed in this article.

Note that Docker compose can determine the dependencies between containers and generate the correct startup order. However, it is only the startup order, and the startup time of each container is not consistent. If there are dependencies, the normal interaction may lead to startup failure.

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you