Docker实例(3)

runoob-docker教程

总结

查看ip

docker inspect 容器ID或容器名 | grep '"IPAddress"'

查看容器运行状态(cpu和内存这些)

docker stats

进入容器内进行命令行操作

docker exec -it content-name-or-id /bin/bash

使用nginx

docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -d nginx

使用php

docker run -p 9000:9000 --name myphp-fpm -v /home/tian_projects/docker_projects/nginx/www:/www -v $PWD/conf:/usr/local/etc/php -v $PWD/logs:/phplogs -d php:7.2-fpm

php和nginx结合

# 复制主机文件到容器里面去
docker cp /home/custom_packages/80.conf d12928c083d3:/etc/nginx/conf.d

# 添加文件/etc/nginx/conf.d/80.conf
server {
  listen 80;
  server_name localhost;
  #server_name _;
  error_log  /wwwlogs/error_nginx.log  error;
  access_log /wwwlogs/access_nginx.log combined;
  #access_log /data/wwwlogs/access_nginx.log combined;
  #root /data/wwwroot/default/weiqing;
  root /www;
  index index.html index.htm index.php;
  #error_page 404 /404.html;
  #error_page 502 /502.html;
  location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
  }
  location ~ [^/]\.php(/|$) {
    try_files $uri =404;
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_index index.php;
    fastcgi_pass 172.18.0.4:9000;#docker inspect 容器ID或容器名 | grep '"IPAddress"'
    fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;
    include fastcgi_params;
    #include fastcgi.conf;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
  }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
  }
  location ~ /\.ht {
    deny all;
  }
}

使用mysql

docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21

连接mysql:
mysql -uroot -p -h 172.18.0.2

使用redis

docker run -p 6379:6379 -v $PWD/data:/data  -d redis:4.0.9 redis-server --appendonly yes

Docker 安装 Nginx

方法一、通过 Dockerfile构建【没走通】

创建Dockerfile

首先,创建目录nginx,用于存放后面的相关东西。

[root@VM_0_7_centos docker_projects]# mkdir nginx
[root@VM_0_7_centos docker_projects]# ls
nginx  thc_centos
[root@VM_0_7_centos docker_projects]# cd nginx/
[root@VM_0_7_centos nginx]# mkdir -p www logs conf
[root@VM_0_7_centos nginx]# ls
conf  logs  www
[root@VM_0_7_centos nginx]# 

www 目录将映射为 nginx 容器配置的虚拟目录

logs 目录将映射为 nginx 容器的日志目录

conf 目录里的配置文件将映射为 nginx 容器的配置文件

进入创建的 nginx 目录,创建 Dockerfile

FROM debian:jessie

MAINTAINER NGINX Docker Maintainers "[email protected]"

ENV NGINX_VERSION 1.10.1-1~jessie

RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 \
        && echo "deb http://nginx.org/packages/debian/ jessie nginx" >> /etc/apt/sources.list \
        && apt-get update \
        && apt-get install --no-install-recommends --no-install-suggests -y \
                                                ca-certificates \
                                                nginx=${NGINX_VERSION} \
                                                nginx-module-xslt \
                                                nginx-module-geoip \
                                                nginx-module-image-filter \
                                                nginx-module-perl \
                                                nginx-module-njs \
                                                gettext-base \
        && rm -rf /var/lib/apt/lists/*

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
        && ln -sf /dev/stderr /var/log/nginx/error.log

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

通过 Dockerfile 创建一个镜像,替换成你自己的名字

docker build -t lwj-nginx .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

[root@VM_0_7_centos nginx]# docker images lwj-nginx

方法二、docker pull nginx

查找Docker Hub上的nginx镜像

[root@VM_0_7_centos nginx]# docker search nginx
INDEX       NAME                                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                                                  Official build of Nginx.                        8341      [OK]       
docker.io   docker.io/jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker c...   1316                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable ...   546                  [OK]
docker.io   docker.io/jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as...   348                  [OK]
docker.io   docker.io/kong                                                   Open-source Microservice & API Management ...   176       [OK]       
docker.io   docker.io/webdevops/php-nginx                                    Nginx with PHP-FPM                              99                   [OK]
docker.io   docker.io/kitematic/hello-world-nginx                            A light-weight nginx container that demons...   97                   
docker.io   docker.io/zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server ...   50                   [OK]
docker.io   docker.io/bitnami/nginx                                          Bitnami nginx Docker Image                      48                   [OK]
docker.io   docker.io/linuxserver/nginx                                      An Nginx container, brought to you by Linu...   34                   
docker.io   docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          33                   [OK]
docker.io   docker.io/tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   19                   [OK]
docker.io   docker.io/nginxdemos/nginx-ingress                               NGINX Ingress Controller for Kubernetes . ...   10                   
docker.io   docker.io/wodby/drupal-nginx                                     Nginx for Drupal container image                9                    [OK]
docker.io   docker.io/blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          8                    [OK]
docker.io   docker.io/webdevops/nginx                                        Nginx container                                 8                    [OK]
docker.io   docker.io/centos/nginx-18-centos7                                Platform for running nginx 1.8 or building...   6                    
docker.io   docker.io/nginxdemos/hello                                       NGINX webserver that serves a simple page ...   6                    [OK]
docker.io   docker.io/1science/nginx                                         Nginx Docker images that include Consul Te...   4                    [OK]
docker.io   docker.io/centos/nginx-112-centos7                               Platform for running nginx 1.12 or buildin...   3                    
docker.io   docker.io/behance/docker-nginx                                   Provides base OS, patches and stable nginx...   2                    [OK]
docker.io   docker.io/pebbletech/nginx-proxy                                 nginx-proxy sets up a container running ng...   2                    [OK]
docker.io   docker.io/toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on sa...   1                    [OK]
docker.io   docker.io/travix/nginx                                           NGinx reverse proxy                             1                    [OK]
docker.io   docker.io/mailu/nginx                                            Mailu nginx frontend                            0                    [OK]

这里我们拉取官方的镜像

[root@VM_0_7_centos nginx]# docker pull nginx

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORYnginx 的镜像。

使用nginx镜像

运行容器

docker 部署简单nginx服务器, 用来查看nginx的配置

[root@VM_0_7_centos nginx]# docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs  -d nginx
d72dae16fc28922fa2a6b9c59c1e8051fc40414d5e104d0ba525e4362461714a

docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -d nginx

命令说明:

  • -p 80:80:将容器的80端口映射到主机的80端口

  • --name mynginx:将容器命名为mynginx

  • -v $PWD/www:/www:将主机中当前目录下的www挂载到容器的/www

  • -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf:将主机中当前目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf

  • -v $PWD/logs:/wwwlogs:将主机中当前目录下的logs挂载到容器的/wwwlogs

查看容器启动情况

docker ps

问题

CentOS7 环境下因为宿主的 SELINUX,导致在nginx容器内无法访问配置文件(default.conf),进而容器无法提供web服务

#############方法一#############
#在宿主主机关闭SELINUX
#临时关闭
setenforce 0
#永久关闭 修改/etc/sysconfig/selinux文件
SELINUX=disabled
 
#############方法二#############
#以特权方式运行容器
#--privileged参数为true
docker run -it --privileged=true -d nginx

Docker 安装 PHP

安装 PHP 镜像

方法一、通过 Dockerfile 构建(没走过)

创建Dockerfile

首先,创建目录 php-fpm,用于存放后面的相关东西。

logs 目录将映射为 php-fpm 容器的日志目录

conf 目录里的配置文件将映射为 php-fpm 容器的配置文件

进入创建的 php-fpm 目录,创建Dockerfile

方法二、docker pull php

查找Docker Hub上的php镜像

[root@VM_0_7_centos php-fpm]# docker search php
INDEX       NAME                                                                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/php                                                       While designed for web development, the PH...   3306      [OK]       
docker.io   docker.io/richarvey/nginx-php-fpm                                   Container running Nginx + PHP-FPM capable ...   546                  [OK]
docker.io   docker.io/phpmyadmin/phpmyadmin                                     A web interface for MySQL and MariaDB.          499                  [OK]
docker.io   docker.io/php-zendserver                                            Zend Server - the integrated PHP applicati...   139       [OK]       
docker.io   docker.io/eboraas/apache-php                                        PHP5 on Apache (with SSL support), built o...   136                  [OK]
docker.io   docker.io/webdevops/php-nginx                                       Nginx with PHP-FPM                              99                   [OK]
docker.io   docker.io/adminer                                                   Database management in a single PHP file.       89        [OK]       
docker.io   docker.io/webdevops/php-apache                                      Apache with PHP-FPM (based on webdevops/php)    61                   [OK]
docker.io   docker.io/nazarpc/phpmyadmin                                        phpMyAdmin as Docker container, based on o...   57                   [OK]
docker.io   docker.io/phpunit/phpunit                                           PHPUnit is a programmer-oriented testing f...   57                   [OK]
docker.io   docker.io/webdevops/php-apache-dev                                  PHP with Apache for Development (eg. with ...   50                   [OK]
docker.io   docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          33                   [OK]
docker.io   docker.io/tetraweb/php                                              PHP 5.5, 5.6, 7.0, 7.1 for CI and running ...   28                   [OK]
docker.io   docker.io/webdevops/php                                             PHP (FPM and CLI) service container             19                   [OK]
docker.io   docker.io/wodby/drupal-php                                          PHP for Drupal                                  18                   [OK]
docker.io   docker.io/centos/php-56-centos7                                     Platform for building and running PHP 5.6 ...   12                   
docker.io   docker.io/clinta/phpipam                                            phpIPAM web IP address management applicat...   10                   [OK]
docker.io   docker.io/phpdocker/phpdocker                                       PHP, MariaDB, Composer, PHP CodeSniffer.        10                   [OK]
docker.io   docker.io/graze/php-alpine                                          Smallish php7 alpine image with some commo...   9                    [OK]
docker.io   docker.io/antage/apache2-php5                                       Docker image for running Apache 2.x with P...   7                    [OK]
docker.io   docker.io/appsvc/php                                                Azure App Service php dockerfiles               5                    [OK]
docker.io   docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       4                    [OK]
docker.io   docker.io/lephare/php                                               PHP container                                   4                    [OK]
docker.io   docker.io/phundament/php                                            [outdated] Phundament 4 php:cli and php:fp...   2                    
docker.io   docker.io/isotopab/php                                              Docker PHP                                      0                    [OK]

这里我们拉取官方的镜像,标签为 7.2-fpm

[root@VM_0_7_centos php-fpm]# docker pull php:7.2-fpm
Trying to pull repository docker.io/library/php ... 
7.2-fpm: Pulling from docker.io/library/php
2a72cbf407d6: Already exists 
273cd543cb15: Pull complete 
ec5ac8875de7: Pull complete 
9106e19b56c1: Pull complete 
f5bfa49de57b: Pull complete 
02533b64e490: Pull complete 
14b800eb89aa: Pull complete 
62e13360729f: Pull complete 
00641a36dbf8: Pull complete 
9afd84ead553: Pull complete 
53f2faacdb80: Pull complete 
Digest: sha256:4a5e68ee8d642e9e5931002edb1c21f38c9b49fc27509210fedee5f165a02326
Status: Downloaded newer image for docker.io/php:7.2-fpm

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORYphp ,标签为 7.2-fpm 的镜像。

[root@VM_0_7_centos php-fpm]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
                                                                8c6094b2c286        18 hours ago        123 MB
runoob/centos                                           6.7                 796850755210        22 hours ago        191 MB
runoob/centos                                           dev                 796850755210        22 hours ago        191 MB
runoob/ubuntu                                           v2                  4ddf6452c51c        23 hours ago        137 MB
docker.io/nginx                                         latest              b175e7467d66        6 days ago          109 MB
docker.io/php                                           7.2-fpm             b2ac9b4ec07b        12 days ago         372 MB
docker.io/mysql                                         5.7                 5195076672a7        4 weeks ago         371 MB
docker.io/debian                                        jessie              5dd74d62fab8        4 weeks ago         123 MB
registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn   v0.10               62db6828e252        5 weeks ago         1.23 GB
docker.io/brookshi/hitchhiker                           v0.10               ef0575e6ef11        5 weeks ago         1.23 GB
docker.io/centos                                        6.7                 000c5746fa52        5 months ago        191 MB
docker.io/ubuntu                                        15.10               9b9cb95443b5        21 months ago       137 MB
docker.io/training/webapp                               latest              6fae60ef3446        2 years ago         349 MB
docker.io/ubuntu                                        13.10               7f020f7bf345        3 years ago         185 MB

使用php-fpm镜像

运行容器

[root@VM_0_7_centos php-fpm]# docker run -p 9000:9000 --name  myphp-fpm -v /home/tian_projects/docker_projects/nginx/www:/www -v $PWD/conf:/usr/local/etc/php -v $PWD/logs:/phplogs -d php:7.2-fpm
375bc0c12e94440f0464e5f85727093cb9752c006048f4029f10554521b8e622

docker run -p 9000:9000 --name  myphp-fpm -v /home/tian_projects/docker_projects/nginx/www:/www -v $PWD/conf:/usr/local/etc/php -v $PWD/logs:/phplogs -d php:7.2-fpm

runoob@runoob:~/php-fpm$ docker run -p 9000:9000 --name  myphp-fpm -v ~/nginx/www:/www -v $PWD/conf:/usr/local/etc/php -v $PWD/logs:/phplogs   -d php:5.6-fpm
00c5aa4c2f93ec3486936f45b5f2b450187a9d09acb18f5ac9aa7a5f405dbedf

命令说明:

  • -p 9000:9000 :将容器的9000端口映射到主机的9000端口

  • --name myphp-fpm :将容器命名为 myphp-fpm

  • -v ~/nginx/www:/www :将主机中项目的目录 www 挂载到容器的 /www

  • -v $PWD/conf:/usr/local/etc/php :将主机中当前目录下的 conf 目录挂载到容器的 /usr/local/etc/php

  • -v $PWD/logs:/phplogs :将主机中当前目录下的 logs 目录挂载到容器的 /phplogs

查看容器启动情况

[root@VM_0_7_centos php-fpm]# docker ps
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED             STATUS              PORTS                                              NAMES
375bc0c12e94        php:7.2-fpm                                                   "docker-php-entryp..."   4 seconds ago       Up 3 seconds        0.0.0.0:9000->9000/tcp                             myphp-fpm
a1b4a3793088        registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn:v0.10   "pm2-docker index.js"    24 hours ago        Up 8 hours          0.0.0.0:8080->8080/tcp, 0.0.0.0:11010->11010/tcp   hitchhiker
92e926a4c155        mysql:5.7                                                     "docker-entrypoint..."   26 hours ago        Up 8 hours          3306/tcp                                           hitchhiker-mysql

通过浏览器访问 phpinfo()

PS:此处是通过 nginx+php 实现web服务,nginx 配置文件的 fastcgi_pass 应该配置为 myphp-fpm 容器的IP。

fastcgi_pass  172.17.0.4:9000;
fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;# 特别注意这个是容器里面的Nginx 配置中的 SCRIPT_FILENAME 要和容器中保持一致才行(docker run -p 9000:9000 --name  myphp-fpm -v /home/tian_projects/docker_projects/nginx/www:/www -v $PWD/conf:/usr/local/etc/php -v $PWD/logs:/phplogs -d php:7.2-fpm)
include fastcgi_params;

try_files $uri =404;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;

[参考](https://icewing.cc/post/docker-use-php-fpm.html)

容器IP的查方法

docker inspect 容器ID或容器名 |grep '"IPAddress"'

查看加载的 php.ini

root@375bc0c12e94:/usr/local/etc/php# php -i|grep php.ini
Configuration File (php.ini) Path => /usr/local/etc/php

Docker 安装 MySQL

方法一、通过 Dockerfile构建

创建Dockerfile

首先,创建目录mysql,用于存放后面的相关东西。

[root@VM_0_7_centos docker_projects]# mkdir mysql
[root@VM_0_7_centos docker_projects]# cd mysql/
[root@VM_0_7_centos mysql]# mkdir -p data logs conf
[root@VM_0_7_centos mysql]# ls
conf  data  logs

data 目录将映射为mysql容器配置的数据文件存放路径

logs 目录将映射为mysql容器的日志目录

conf 目录里的配置文件将映射为mysql容器的配置文件

进入创建的 mysql 目录,创建 Dockerfile

Dockerfile 内容

通过Dockerfile创建一个镜像,替换成你自己的名字

docker build -t mysql .

方法二、docker pull mysql

查找Docker Hub上的mysql镜像

[root@VM_0_7_centos mysql]# docker search mysql
INDEX       NAME                                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                                                  MySQL is a widely used, open-source relati...   6009      [OK]       
docker.io   docker.io/mariadb                                                MariaDB is a community-developed fork of M...   1893      [OK]       
docker.io   docker.io/mysql/mysql-server                                     Optimized MySQL Server Docker images. Crea...   428                  [OK]
docker.io   docker.io/percona                                                Percona Server is a fork of the MySQL rela...   335       [OK]       
docker.io   docker.io/zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       94                   [OK]
docker.io   docker.io/hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          80 

这里我们拉取官方的镜像,标签为 5.7.21

[root@VM_0_7_centos default]# docker pull mysql:5.7.21
Trying to pull repository docker.io/library/mysql ... 
5.7.21: Pulling from docker.io/library/mysql
Digest: sha256:691c55aabb3c4e3b89b953dd2f022f7ea845e5443954767d321d5f5fa394e28c
Status: Downloaded newer image for docker.io/mysql:5.7.21

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORYmysql ,标签为 5.7.21 的镜像。

[root@VM_0_7_centos vhost]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
                                                                8c6094b2c286        36 hours ago        123 MB
runoob/centos                                           6.7                 796850755210        40 hours ago        191 MB
runoob/centos                                           dev                 796850755210        40 hours ago        191 MB
runoob/ubuntu                                           v2                  4ddf6452c51c        41 hours ago        137 MB
docker.io/nginx                                         latest              b175e7467d66        7 days ago          109 MB
docker.io/php                                           7.2-fpm             b2ac9b4ec07b        13 days ago         372 MB
docker.io/mysql                                         5.7                 5195076672a7        4 weeks ago         371 MB
docker.io/mysql                                         5.7.21              5195076672a7        4 weeks ago         371 MB
docker.io/debian                                        jessie              5dd74d62fab8        5 weeks ago         123 MB
registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn   v0.10               62db6828e252        5 weeks ago         1.23 GB
docker.io/brookshi/hitchhiker                           v0.10               ef0575e6ef11        5 weeks ago         1.23 GB
docker.io/centos                                        6.7                 000c5746fa52        5 months ago        191 MB
docker.io/ubuntu                                        15.10               9b9cb95443b5        21 months ago       137 MB
docker.io/training/webapp                               latest              6fae60ef3446        2 years ago         349 MB
docker.io/ubuntu                                        13.10               7f020f7bf345        3 years ago         185 MB

使用mysql镜像

运行容器

[root@VM_0_7_centos mysql]# docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21
d6a2377f2eebd967687631f4e15ab86ed63711bf58bd98676668e89298da9d0d

命令说明:

  • -p 3306:3306:将容器的3306端口映射到主机的3306端口

  • -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf

  • -v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs

-v $PWD/data:/mysql_data:将主机当前目录下的 data 目录挂载到容器的 /mysql_data

-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码

查看容器启动情况

[root@VM_0_7_centos vhost]# docker ps
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED              STATUS              PORTS                                              NAMES
52fe1969cce5        mysql:5.7.21                                                  "docker-entrypoint..."   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp                             mymysql
a1b4a3793088        registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn:v0.10   "pm2-docker index.js"    42 hours ago         Up 26 hours         0.0.0.0:8080->8080/tcp, 0.0.0.0:11010->11010/tcp   hitchhiker
92e926a4c155        mysql:5.7

容器IP的查方法

docker inspect 容器ID或容器名 |grep '"IPAddress"'

登录mysql

[root@VM_0_7_centos vhost]# docker inspect 52fe1969cce5 |grep '"IPAddress"'
            "IPAddress": "172.18.0.2",
                    "IPAddress": "172.18.0.2",

[root@VM_0_7_centos vhost]# mysql -uroot -p -h 172.18.0.2
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

MySQL [(none)]> 

Docker 安装 Python

方法一、通过 Dockerfile 构建

创建Dockerfile

首先,创建目录python,用于存放后面的相关东西。

[root@VM_0_7_centos docker_projects]# mkdir python
[root@VM_0_7_centos docker_projects]# cd python/
[root@VM_0_7_centos python]# ls
[root@VM_0_7_centos python]# mkdir -p myapp
[root@VM_0_7_centos python]# ls
myapp

myapp 目录将映射为python容器配置的应用目录

进入创建的python目录,创建Dockerfile

Dockerfile 文件

通过Dockerfile创建一个镜像,替换成你自己的名字

docker build -t python:3.5 .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

docker images python:3.5 

方法二、docker pull python:3.6

[root@VM_0_7_centos python]# docker pull python:3.6
Trying to pull repository docker.io/library/python ... 
3.6: Pulling from docker.io/library/python
f2b6b4884fc8: Already exists 
4fb899b4df21: Pull complete 
74eaa8be7221: Pull complete 
2d6e98fe4040: Pull complete 
414666f7554d: Pull complete 
7e765f6e07f8: Pull complete 
6f02823cd02e: Pull complete 
9239d3c3d426: Pull complete 
Digest: sha256:068691633d8f128035ffcca41dc7cf616ca776486a0ff932a61ea131f2d0a2f9
Status: Downloaded newer image for docker.io/python:3.6

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORYpython ,标签为 3.6 的镜像。

[root@VM_0_7_centos python]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
                                                                8c6094b2c286        36 hours ago        123 MB
runoob/centos                                           6.7                 796850755210        40 hours ago        191 MB
runoob/centos                                           dev                 796850755210        40 hours ago        191 MB
runoob/ubuntu                                           v2                  4ddf6452c51c        41 hours ago        137 MB
docker.io/nginx                                         latest              b175e7467d66        7 days ago          109 MB
docker.io/php                                           7.2-fpm             b2ac9b4ec07b        13 days ago         372 MB
docker.io/python                                        3.6                 efb6baa1169f        2 weeks ago         691 MB
docker.io/mysql                                         5.7                 5195076672a7        4 weeks ago         371 MB
docker.io/mysql                                         5.7.21              5195076672a7        4 weeks ago         371 MB
docker.io/debian                                        jessie              5dd74d62fab8        5 weeks ago         123 MB
registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn   v0.10               62db6828e252        5 weeks ago         1.23 GB
docker.io/brookshi/hitchhiker                           v0.10               ef0575e6ef11        5 weeks ago         1.23 GB
docker.io/centos                                        6.7                 000c5746fa52        5 months ago        191 MB
docker.io/ubuntu                                        15.10               9b9cb95443b5        21 months ago       137 MB
docker.io/training/webapp                               latest              6fae60ef3446        2 years ago         349 MB
docker.io/ubuntu                                        13.10               7f020f7bf345        3 years ago         185 MB

使用python镜像

~/python/myapp 目录下创建一个 helloworld.py 文件,代码如下:

#!/usr/bin/python

print("Hello, World!");

运行容器

[root@VM_0_7_centos python]# docker run -v $PWD/myapp:/usr/src/myapp -w /usr/src/myapp python:3.6 python helloworld.py
Hello, World!

命令说明:

  • -v $PWD/myapp:/usr/src/myapp :将主机中当前目录下的 myapp 挂载到容器的 /usr/src/myapp

  • -w /usr/src/myapp :指定容器的 /usr/src/myapp 目录为工作目录

  • python helloworld.py :使用容器的python命令来执行工作目录中的 helloworld.py 文件

Docker 安装 Redis

方法一、通过 Dockerfile 构建

创建Dockerfile

首先,创建目录 redis ,用于存放后面的相关东西。

[root@VM_0_7_centos docker_projects]# mkdir redis
[root@VM_0_7_centos docker_projects]# ls
mysql  nginx  php-fpm  python  redis  thc_centos
[root@VM_0_7_centos docker_projects]# cd redis/
[root@VM_0_7_centos redis]# mkdir -p data
[root@VM_0_7_centos redis]# ls
data

data 目录将映射为redis容器配置的 /data 目录,作为redis数据持久化的存储目录

进入创建的redis目录,创建Dockerfile

Dockerfile文件

通过Dockerfile创建一个镜像,替换成你自己的名字

docker build  -t redis:4.0.9 .

方法二、docker pull redis:4.0.9

查找Docker Hub上的redis镜像

[root@VM_0_7_centos vhost]# docker search redis
INDEX       NAME                                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/redis                                Redis is an open source key-value store th...   5041      [OK]       
docker.io   docker.io/bitnami/redis                        Bitnami Redis Docker Image                      71                   [OK]
docker.io   docker.io/sameersbn/redis                                                                      66                   [OK]
docker.io   docker.io/tenstartups/redis-commander                                                          32                   [OK]
docker.io   docker.io/hypriot/rpi-redis                    Raspberry Pi compatible redis image             31                   
docker.io   docker.io/kubeguide/redis-master               redis-master with "Hello World!"                21                   
docker.io   docker.io/kubeguide/guestbook-redis-slave      Guestbook redis slave                           17                   
docker.io   docker.io/tutum/redis                          Base docker image to run a Redis server         10                   
docker.io   docker.io/webhippie/redis                      Docker images for redis                         8                    [OK]
docker.io   docker.io/arm32v7/redis                        Redis is an open source key-value store th...   5                    
docker.io   docker.io/rtoma/logspout-redis-logstash        Logspout including Redis adapter for sendi...   5                    
docker.io   docker.io/oliver006/redis_exporter              Prometheus Exporter for Redis Metrics. Su...   4                    
docker.io   docker.io/rediscommander/redis-commander       Alpine image for redis-commander - Redis m...   3                    [OK]
docker.io   docker.io/centos/redis-32-centos7              Redis in-memory data structure store, used...   2                    
docker.io   docker.io/dynomitedb/redis                     Redis backend for DynomiteDB.                   2                    [OK]
docker.io   docker.io/frodenas/redis                       A Docker Image for Redis                        2                    [OK]
docker.io   docker.io/tomesar/redis-arm                    Redis for ARM!                                  2                    [OK]
docker.io   docker.io/google/guestbook-python-redis        A simple guestbook example written in Pyth...   1                    
docker.io   docker.io/kilsoo75/redis-master                This image is for the redis master of SK C...   1                    
docker.io   docker.io/tiredofit/redis                      Redis Server w/ Zabbix monitoring and S6 O...   1                    [OK]
docker.io   docker.io/ajmath/fluentd-redis                 Use fluentd logs to send docker logs to re...   0                    [OK]
docker.io   docker.io/anchorfree/redis                     redis cache server for logging                  0                    
docker.io   docker.io/brendangibat/docker-logspout-redis   Docker Logspout container with Logspout-Re...   0                    [OK]
docker.io   docker.io/iadvize/redis                                                                        0                    
docker.io   docker.io/sstarcher/fluent-redis-aws           https://github.com/sstarcher/docker-fluent...   0                    [OK]

这里我们拉取官方的镜像,标签为 4.0.9

[root@VM_0_7_centos vhost]# docker pull  redis:4.0.9
Trying to pull repository docker.io/library/redis ... 
4.0.9: Pulling from docker.io/library/redis
b0568b191983: Pull complete 
6637dc5b29fe: Pull complete 
7b4314315f15: Pull complete 
2fd86759b5ff: Pull complete 
0f04862b5a3b: Pull complete 
2db0056aa977: Pull complete 
Digest: sha256:d33df422e02cd24de510ebb60a4095875dcfb5f12c1df921c54cedc7cecf1bc0
Status: Downloaded newer image for docker.io/redis:4.0.9

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORYredis ,标签为 4.0.9 的镜像。

[root@VM_0_7_centos vhost]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
runoob/centos                                           6.7                 796850755210        42 hours ago        191 MB
runoob/centos                                           dev                 796850755210        42 hours ago        191 MB
runoob/ubuntu                                           v2                  4ddf6452c51c        43 hours ago        137 MB
docker.io/nginx                                         latest              b175e7467d66        7 days ago          109 MB
docker.io/php                                           7.2-fpm             b2ac9b4ec07b        13 days ago         372 MB
docker.io/python                                        3.6                 efb6baa1169f        2 weeks ago         691 MB
docker.io/redis                                         4.0.9               c5355f8853e4        3 weeks ago         107 MB
docker.io/mysql                                         5.7                 5195076672a7        4 weeks ago         371 MB
docker.io/mysql                                         5.7.21              5195076672a7        4 weeks ago         371 MB
docker.io/debian                                        jessie              5dd74d62fab8        5 weeks ago         123 MB
registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn   v0.10               62db6828e252        5 weeks ago         1.23 GB
docker.io/brookshi/hitchhiker                           v0.10               ef0575e6ef11        5 weeks ago         1.23 GB
docker.io/centos                                        6.7                 000c5746fa52        5 months ago        191 MB
docker.io/ubuntu                                        15.10               9b9cb95443b5        21 months ago       137 MB
docker.io/training/webapp                               latest              6fae60ef3446        2 years ago         349 MB
docker.io/ubuntu                                        13.10               7f020f7bf345        3 years ago         185 MB

使用redis镜像

运行容器

[root@VM_0_7_centos redis]# docker run -p 6379:6379 -v $PWD/data:/data  -d redis:4.0.9 redis-server --appendonly yes
54f7813a4eab5c7d995d83dbbb735c1f5f1848d18089133d896e2655c2386b33

命令说明:

  • -p 6379:6379 : 将容器的 6379 端口映射到主机的 6379 端口

  • -v $PWD/data:/data : 将主机中当前目录下的 data 挂载到容器的 /data

  • redis-server --appendonly yes : 在容器执行 redis-server 启动命令,并打开redis持久化配置

查看容器启动情况

[root@VM_0_7_centos redis]# docker ps
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED              STATUS              PORTS                                              NAMES
54f7813a4eab        redis:4.0.9                                                   "docker-entrypoint..."   About a minute ago   Up About a minute   0.0.0.0:6379->6379/tcp                             romantic_goldstine
a1b4a3793088        registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn:v0.10   "pm2-docker index.js"    44 hours ago         Up 28 hours         0.0.0.0:8080->8080/tcp, 0.0.0.0:11010->11010/tcp   hitchhiker
92e926a4c155        mysql:5.7                                                     "docker-entrypoint..."   46 hours ago         Up 28 hours         3306/tcp                                           hitchhiker-mysql

连接、查看容器

容器IP的查方法

docker inspect 容器ID或容器名 |grep '"IPAddress"'

使用redis镜像执行 redis-cli 命令连接到刚启动的容器,主机IP为 172.18.0.2

[root@VM_0_7_centos redis]# docker inspect 54f7813a4eab |grep '"IPAddress"'
            "IPAddress": "172.18.0.2",
                    "IPAddress": "172.18.0.2",

[root@VM_0_7_centos redis]# docker exec -it 54f7813a4eab redis-cli
127.0.0.1:6379> keys *

redis-cli -h xxxxx -p xxxx

Docker 安装 MongoDB

方法一、通过 Dockerfile 构建

创建Dockerfile

首先,创建目录mongo,用于存放后面的相关东西。

[root@VM_0_7_centos docker_projects]# mkdir -p mongo mongo/db
[root@VM_0_7_centos docker_projects]# ls
mongo  mysql  nginx  php-fpm  python  redis  thc_centos

db 目录将映射为mongo容器配置的 /data/db 目录,作为mongo数据的存储目录

进入创建的mongo目录,创建Dockerfile

Dockerfile文件

通过Dockerfile创建一个镜像,替换成你自己的名字

docker build -t mongo:3.7 .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

docker images  mongo:3.7

方法二、docker pull mongo:3.7

查找Docker Hub上的mongo镜像

[root@VM_0_7_centos vhost]# docker search mongo
INDEX       NAME                                          DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mongo                               MongoDB document databases provide high av...   4356      [OK]       
docker.io   docker.io/mongo-express                       Web-based MongoDB admin interface, written...   241       [OK]       
docker.io   docker.io/tutum/mongodb                       MongoDB Docker image – listens in port 270...   223                  [OK]
docker.io   docker.io/mvertes/alpine-mongo                light MongoDB container                         72                   [OK]
docker.io   docker.io/mongoclient/mongoclient             Official docker image for Mongoclient, fea...   50                   [OK]
docker.io   docker.io/bitnami/mongodb                     Bitnami MongoDB Docker Image                    31                   [OK]
docker.io   docker.io/frodenas/mongodb                    A Docker Image for MongoDB                      17                   [OK]
docker.io   docker.io/mongooseim/mongooseim               Small docker image for MongooseIM - robust...   15                   
docker.io   docker.io/mongooseim/mongooseim-docker        MongooseIM server the latest stable version     10                   [OK]
docker.io   docker.io/centos/mongodb-26-centos7           MongoDB NoSQL database server                   5                    
docker.io   docker.io/istepanov/mongodump                 Docker image with mongodump running as a c...   5                    [OK]
docker.io   docker.io/centos/mongodb-32-centos7           MongoDB NoSQL database server                   4                    
docker.io   docker.io/cvallance/mongo-k8s-sidecar         Kubernetes side car to setup and maintain ...   4                    [OK]
docker.io   docker.io/eses/mongodb_exporter               mongodb exporter for prometheus                 4                    [OK]
docker.io   docker.io/khezen/mongo                        MongoDB Docker image supporting RocksDB st...   4                    [OK]
docker.io   docker.io/19hz/mongo-container                Mongodb replicaset for coreos                   1                    [OK]
docker.io   docker.io/alino/coreos-mongodb-cluster        mongo 3.0                                       1                    [OK]
docker.io   docker.io/circleci/mongo                      MongoDB document databases provide high av...   1                    
docker.io   docker.io/ekesken/mongo                       docker image for mongo that is configurabl...   1                    [OK]
docker.io   docker.io/extremeprog/mongo-sites-api         mongo-sites-api automated build                 1                    [OK]
docker.io   docker.io/openshift/mongodb-24-centos7        DEPRECATED: A Centos7 based MongoDB v2.4 i...   1                    
docker.io   docker.io/ansibleplaybookbundle/mongodb-apb   An APB to deploy MongoDB.                       0                    [OK]
docker.io   docker.io/os33/go-mongo                       go and mongo package for testing                0                    
docker.io   docker.io/quilt/mongo                         MongoDB container for quilt.io                  0                    [OK]
docker.io   docker.io/xogroup/mongodb_backup_gdrive       Docker image to create a MongoDB database ...   0                    [OK]

这里我们拉取官方的镜像,标签为 3.7

[root@VM_0_7_centos vhost]# docker pull mongo:3.7
Trying to pull repository docker.io/library/mongo ... 
3.7: Pulling from docker.io/library/mongo
b0568b191983: Already exists 
1e8b5b4e67a0: Pull complete 
a87b29dbb553: Pull complete 
908c259a6a99: Pull complete 
2fdec16e62a3: Pull complete 
5cb50b2324f0: Pull complete 
11cea85d02c1: Pull complete 
84e65a0fdd30: Pull complete 
5bdba759557f: Pull complete 
07e712ef3a90: Pull complete 
Digest: sha256:56ad872fa90fd3254a43f1dedb52b25ecf5694a54d08b673f9f8f2aebc4a0b83
Status: Downloaded newer image for docker.io/mongo:3.7

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORYmongo,标签为 3.7 的镜像。

[root@VM_0_7_centos vhost]# docker images mongo
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/mongo     3.7                 b42dafd89c83        4 weeks ago         375 MB
[root@VM_0_7_centos vhost]# docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             SIZE
runoob/centos                                           6.7                 796850755210        43 hours ago        191 MB
runoob/centos                                           dev                 796850755210        43 hours ago        191 MB
runoob/ubuntu                                           v2                  4ddf6452c51c        43 hours ago        137 MB
docker.io/nginx                                         latest              b175e7467d66        7 days ago          109 MB
docker.io/php                                           7.2-fpm             b2ac9b4ec07b        13 days ago         372 MB
docker.io/python                                        3.6                 efb6baa1169f        2 weeks ago         691 MB
docker.io/redis                                         4.0.9               c5355f8853e4        3 weeks ago         107 MB
docker.io/mongo                                         3.7                 b42dafd89c83        4 weeks ago         375 MB
docker.io/mysql                                         5.7                 5195076672a7        4 weeks ago         371 MB
docker.io/mysql                                         5.7.21              5195076672a7        4 weeks ago         371 MB
docker.io/debian                                        jessie              5dd74d62fab8        5 weeks ago         123 MB
registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn   v0.10               62db6828e252        5 weeks ago         1.23 GB
docker.io/brookshi/hitchhiker                           v0.10               ef0575e6ef11        5 weeks ago         1.23 GB
docker.io/centos                                        6.7                 000c5746fa52        5 months ago        191 MB
docker.io/ubuntu                                        15.10               9b9cb95443b5        21 months ago       137 MB
docker.io/training/webapp                               latest              6fae60ef3446        2 years ago         349 MB
docker.io/ubuntu                                        13.10               7f020f7bf345        3 years ago         185 MB

使用mongo镜像

运行容器

[root@VM_0_7_centos mongo]# docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo:3.7
8899031988d12f653de71a75ce22730d11c71cdc74b6376fe6968e03eda35c2a

命令说明:

  • -p 27017:27017 :将容器的27017 端口映射到主机的27017 端口

  • -v $PWD/db:/data/db :将主机中当前目录下的db挂载到容器的 /data/db ,作为mongo数据存储目录

查看容器启动情况

[root@VM_0_7_centos mongo]# docker ps
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED             STATUS              PORTS                                              NAMES
8899031988d1        mongo:3.7                                                     "docker-entrypoint..."   47 seconds ago      Up 46 seconds       0.0.0.0:27017->27017/tcp                           angry_cori
a1b4a3793088        registry.cn-hangzhou.aliyuncs.com/brook/hitchhiker-cn:v0.10   "pm2-docker index.js"    44 hours ago        Up 28 hours         0.0.0.0:8080->8080/tcp, 0.0.0.0:11010->11010/tcp   hitchhiker
92e926a4c155        mysql:5.7                                                     "docker-entrypoint..."   46 hours ago        Up 28 hours         3306/tcp                                           hitchhiker-mysql

查看容器ip

docker inspect 8899031988d1 |grep '"IPAddress"'

            "IPAddress": "172.18.0.2",
                    "IPAddress": "172.18.0.2",

使用mongo镜像执行mongo 命令连接到刚启动的容器,主机IP为 172.18.0.2

docker run -it mongo:3.7 mongo --host 172.18.0.2

[root@VM_0_7_centos mongo]# docker run -it mongo:3.7 mongo --host 172.18.0.2
MongoDB shell version v3.7.3
connecting to: mongodb://172.18.0.2:27017/
MongoDB server version: 3.7.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
2018-04-18T06:20:36.043+0000 I STORAGE  [main] In File::open(), ::open for '/home/mongodb/.mongorc.js' failed with No such file or directory
Server has startup warnings: 
2018-04-18T06:17:02.156+0000 I STORAGE  [initandlisten] 
2018-04-18T06:17:02.156+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-04-18T06:17:02.156+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] 
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] ** NOTE: This is a development version (3.7.3) of MongoDB.
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] **       Not recommended for production.
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] 
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] 
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] 
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] 
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-04-18T06:17:07.808+0000 I CONTROL  [initandlisten] 
> 

你可能感兴趣的:(Docker实例(3))