• Docker 命令大全

  • 《【docker入门】10分钟,快速学会docker》 - https://www.bilibili.com/video/av58402749?spm_id_from=333.788.b_636f6d6d656e74.8

  • 《【docker入门2】实战~如何组织一个多容器项目docker-compose》 - https://www.bilibili.com/video/av61131351?from=search&seid=14332760623178684588

  • play with docker - https://labs.play-with-docker.com/

  • 《『中级篇』play with docker 的使用(44)》 - https://www.jianshu.com/p/38649ff222ac



1 基本指令


https://www.processon.com/view/link/5d836beae4b021bb66447353
倒计时, 默认用 4小时

# docker run -d -p 80:80 nginx

开启服务
-d 后台运行
-p 80:80 端口映射 宿主端口:docker端口

# docker exec -it 0915862262a94b78ff26d27eae5c3650ca6e4c5864b218894bea2adad37d7cb5 bash

进入容器

or docker exec -it 091586226 bash

-i即使没有附加也保持STDIN 打开 - 《什么是标准输入,标准输出(stdin,stdout)?》
(即保持可输入)

-t分配一个伪终端

# docker rm -f 0915862262a94b7

删除容器
-f force 强制删除

# docker commit 09 m1

提交容器,名字 m1

# Dockerfile

FROM nginx
ADD ./ /user/share/nginx/html/

基于 nginx
复制当前目录(./)到 nginx 目录(/user/share/nginx/html/

# docker bulid -t m2 .

--tag, -t : 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。

使用当前目录下 Dockerfile 创建镜像 , 标签为 m2

# docker save m2 > 1.tar

将容器(m2)保存到一个文件(1.tar)里

# docker rmi m2

删除镜像

# docker load < 1.tar

加载 文件(1.tar) 为镜像


2 组织一个多容器项目docker-compose

  • 开启 docker 会“生成”一个 docker0 网卡

  • 容器和docker0网卡处于同一网段

  • 宿主机访问容器要通过<mark>网卡进行转发,路由过去</mark>

# docker run -dit alpine

alpine 最小的linux 系统 只有 5M 大小
-d 后台运行
-it 阻塞运行

# docker exec -it b49 sh

sh 工具 的形式 打开 alpine

# docker run -d -p8881:80 --name myng nginx

后台开启 nginx 别名 myng
docker端口号 80 映射到 宿主机 端口号 8881

# docker run -dit --link myng:myngnix alpine

后台阻塞开启 alpine
link 别名 myngnix 作为 myng服务的域名

  • https://docs.docker.com/compose/install/

# docker-compose

安***r> sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
授权
sudo chmod +x /usr/local/bin/docker-compose

<?php
$dbhost = 'localhost';  // mysql服务器主机地址
$dbuser = 'root';            // mysql用户名
$dbpass = '123456';          // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Could not connect: ' . mysqli_error());
}
echo '数据库连接成功!';
mysqli_close($conn);
?>

nginx.conf


#user nobody;
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout  65;

    #gzip on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        #access_log logs/host.access.log main;

        location / { # 优先级最低 / 指所有请求
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }


        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        # proxy_pass http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
        # root html;
            fastcgi_pass   php:9000 ; # 127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        # deny all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    # listen 8000;
    # listen somename:8080;
    # server_name somename alias another.alias;

    # location / {
    # root html;
    # index index.html index.htm;
    # }
    #}


    # HTTPS server
    #
    #server {
    # listen 443 ssl;
    # server_name localhost;

    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.key;

    # ssl_session_cache shared:SSL:1m;
    # ssl_session_timeout 5m;

    # ssl_ciphers HIGH:!aNULL:!MD5;
    # ssl_prefer_server_ciphers on;

    # location / {
    # root html;
    # index index.html index.htm;
    # }
    #}

}

docker-compose.yml

version: "3"
services:
  nginx:
    image: nginx:alpine
    ports:
    - 8883:80
    volumes:
    - /root/html:/usr/share/nginx/html
    - /root/conf/nginx.conf:/etc/nginx/nginx.conf
  php:
    image: devilbox/php-fpm:8.0-work-0.104
    volumes:
    - /root/html:/var/www/html
  mysql:
    image: mysql:5.6
    environment:
    - MYSQL_ROOT_PASSWORD=123456


# docker-compose up -d

-d 后台启动