其他分享
首页 > 其他分享> > docker常用命令

docker常用命令

作者:互联网

一、docker自动安装脚本

auto_install_docker.sh

#!/bin/bash

#Author:qye

#Date:2020-2-28

#version:1.0

#Description:auto install docker

 

Os_Release=$(cat /etc/redhat-release |awk -F "[ .]+" '{print $4}')

 

#configure environment and yum source

function Env_Yum(){

if [ $Os_Release -ne 7 ] ;then

  echo "please use centos 7"

  exit 1

fi

 

sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

systemctl stop firewalld

 

cd /etc/yum.repos.d/

rm -rf *

curl -o /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo

curl -o /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum clean all

yum makecache

}

 

#uninstall docker if docker was installed

function Uninstall_Docker(){

yum remove docker \

                  docker-client \

                  docker-client-latest \

                  docker-common \

                  docker-latest \

                  docker-latest-logrotate \

                  docker-logrotate \

                  docker-engine

 

}

 

#install docker

function Install_Docker(){

yum install yum-utils device-mapper-persistent-data lvm2 -y

yum-config-manager --add-repo \

http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum makecache fast

 

yum install docker-ce -y

systemctl start docker

systemctl enable docker

 

mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'

{

  "graph":"/data/docker",

  "storage-driver":"overlay2",

  "insecure-registries":["registry.access.redhat.com","quay.io"],

  "registry-mirrors":["https://2mym9yne.mirror.aliyuncs.com"],

  "bip":"172.16.3.1/24",

  "exec-opts":["native.cgroupdriver=systemd"],

  "live-restore":true

}

EOF

systemctl restart docker

docker info

}

 

Env_Yum

Uninstall_Docker

Install_Docker

 

二、docker启动和关闭

systemctl start docker

systemctl stop docker

systemctl status docker

systemctl restart docker

 

三、镜像操作

镜像登录(docker hub)

 docker login docker.io

搜索镜像

docker search alpine

拉取镜像

docker pull alpine

docker pull alpine:3.10.3

docker pull docker.io/library/alpine:3.10.3

查看镜像

docker images

给镜像打标签

docker tag 965ea09ff2eb docker.io/library/alpine:v3.10.3

docker tag 965ea09ff2eb library/alpine:lastest

docker tag nginx:alpine 192.168.1.222:5000/nginx:alpine

推送镜像

docker push docker.io/library/alpine:v3.10.3

删除镜像

docker rmi -f docker.io/library/alpine:latest

docker rmi -f 965ea09ff2eb

 

四、容器操作

列出容器进程

docker ps -a

创建容器

docker run --name my-nginx-alpine -d nginx:alpine

创建并启动容器

docker run -it docker.io/library/alpine:v3.10.1 /bin/sh

运行容器执行命令后删除容器(非交互式)

docker run --rm alpine:v3.10.1 /bin/echo hello

运行容器在后台

docker run --name myalpine -d alpine:v3.10.1

docker run --name myalpine1 -d alpine:v3.10.1 /bin/sleep 300

映射端口(宿主机:容器)

docker run --name my-nginx-alpine -d -p 8080:80 nginx:alpine

数据持久化(宿主机:容器)

docker run --name nginx -d  -v /opt:/opt  nginx:alpine

docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d -v /opt/mysql/:/var/lib/mysql mysql:5.7

资源限制,最大可用内存500M

docker run --memory=500m nginx:alpine

主机与容器之间拷贝数据

主机拷贝到容器

docker cp /tmp/test.txt nginx:/tmp

容器拷贝到主机

docker cp nginx:/tmp/test.txt ./

拷贝目录下文件,源文件只能在同级,不能拷贝制定目录下的所有文件。

进入指定容器

docker exec -it e7702bfd2d75 /bin/sh

启动和关闭容器

docker stop e7702bfd2d75

docker start e7702bfd2d75

删除容器

docker rm -f myalpine

批量删除exited的容器

for i in `docker ps -a | grep -i exited |awk '{print $1}' `;do docker rm -f $i;done

 

五、镜像操作补充

修改镜像并生产新镜像

docker commit -p myalpine1 alpline:v3.10.3_with_1.txt

导出新镜像

docker save 00dfc6cb1b12 > alpine:v3.10.3_with_1.txt.tar

docker save nginx:alpine -o nginx-alpine.tar

导入新镜像并配置tag

docker load < alpine:v3.10.3_with_1.txt.tar

docker load -i nginx-alpine.tar

docker tag 00dfc6cb1b12 qye/alpine:v3.10.3_with_1.txt

 

六、日志操作

## 查看全部日志

docker logs nginx

## 实时查看最新日志

docker logs -f nginx

## 从最新的100条开始查看

docker logs --tail=100 -f nginx

## 查看容器配置信息,包括容器IP地址等

docker inspect nginx

## 查看镜像的明细信息

docker inspect nginx:alpine

 

七、高级操作

nginx为例

端口映射

拉取镜像

docker pull nginx:1.12.2

添加标签

docker tag 4037a5562b03 qye/nginx:v1.12.2

创建并运行容器映射端口,宿主机81,容器80

docker run --rm --name mynginx -d -p81:80 qye/nginx:v1.12.2

挂载数据卷

在宿主机上创建目录

mkdir \root\html

cd \root\html

下载百度首页

wget www.baidu.com -O index.html

将宿主机目录挂载到容器中

docker run -d --rm --name nginx_with_baidu -p82:80 -v/root/html:/usr/share/nginx/html qye/nginx:v1.12.2

查看

docker inspect 32f444edca91

传递环境变量(键值对)

docker run --rm -e E_OPTS:abcde qye/nginx:v1.12.2 printenv

传多个变量

docker run --rm -e E_OPTS:abcde -e C_OPTS:12345 qye/nginx:v1.12.2 printenv

容器内安装软件

进入容器

docker exec -it 32f444edca91 /bin/sh

配置apt-get源

tee /etc/apt/sources.list <<EOF

deb http://mirrors.163.com/debian/ jessie main non-free contrib

deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib

EOF

安装软件curl

apt-get update && apt-get install curl -y

制作新镜像

docker commit -p 32f444edca91 qye/nginx:curl

推入公网仓库

docker push qye/nginx:curl

 

九、dockerfile

创建dockerfile

USER(当前用户)和WORKDIR(当前目录)指令

mkdir -p /data/dockerfile

cd /data/dockerfile

vim Dockerfile

FROM qye/nginx:v1.12.2

USER nginx

WORKDIR /usr/share/nginx/html/

构建镜像

docker build . -t qye/nginx:v1.12.2_with_user_workdir

检查

docker run --rm -it qye/nginx:v1.12.2_with_user_workdir /bin/bash

 

ADD(复制)和EXPOSE(容器端口映射)指令

vim Dockerfile

FROM qye/nginx:v1.12.2

ADD index.html /usr/share/nginx/html/index.html          #copy

EXPOSE 80                                          #容器内端口80

构建镜像

docker build . -t qye/nginx:v1.12.2_with_add_expose

创建容器(P表示在宿主机上随意使用一个端口)

docker run --rm -d --name nginx123 -P qye/nginx:v1.12.2_with_add_expose

nginx启动命令

nginx -g "daemon off;"

 

RUN(构建镜像时运行命令)和ENV(环境变量)指令

vim Dockerfile

FROM centos:7                         #本地镜像没有从远程pull

ENV VER 9.11.4                         #VER=9.11.4

RUN yum install bind-$VER -y

构建镜像(没有原始镜像则从远程仓库下载)

docker build . -t qye/bind:v9.11.4_with_run_env

创建容器并进入

docker run -it --rm qye/bind:v9.11.4_with_run_env /bin/bash

cat /etc/redhat-release

printenv

rpm -qa bind

 

CMD(启动容器时运行命令)和ENTRYPOINT(不指定cmd默认执行)指令

CMD可被docker run中的命令覆盖

vim Dockerfile

FROM centos:7

RUN yum install httpd -y

CMD ["httpd","-D","FOREGROUND"]            #启动容器启动httpd服务

构建镜像

docker build . -t qye/httpd:test

创建容器

docker run -d --rm --name myhttpd -p83:80 qye/httpd:test

 

ENTRYPOINT没有cmd命令默认找到/entrypoint.sh ,注意执行权限,不会被覆盖

vim Dockerfile

FROM centos:7

ADD entrypoint.sh /entrypoint.sh

RUN yum install epel-release -q -y && yum install nginx -y

ENTRYPOINT /entrypoint.sh

 

vim entrypoint.sh

#!/bin/bash

/sbin/nginx -g "daemon off;"         #在docker中启动nginx命令

chmod +x entrypoint.sh

构建镜像

docker build . -t qye/nginx:mynginx

创建容器,没指定名字随机分配一个

docker run --rm -p84:80 qye/nginx:mynginx

 

进入容器

docker exec -it priceless_yalow /bin/bash

 

 

综合实验(多用debian系统,没有状态)

vim /data/dockerfile/ Dockerfile

FROM qye/nginx:v1.12.2

USER root

ENV WWW /usr/share/nginx/html

ENV CONF /etc/nginx/conf.d

RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\

    echo 'Asia/Shanghai' >/etc/timezone

WORKDIR $WWW

ADD index.html $WWW/index.html

ADD demo.od.com.conf $CONF/demo.od.com.conf

EXPOSE 80

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

 

cd /data/dockerfile/

wget www.baidu.com -O index.html

vim demo.od.com.conf

server {

  listen 80;

  server_name demo.od.com;

  root /usr/share/nginx/html;

}

构建镜像

docker build . -t qye/nginx:baidu

创建容器

docker run --rm -p80:80 qye/nginx:baidu

创建主机映射

在宿主机上登录

demo.od.com

搜索

复制

标签:容器,--,nginx,常用命令,qye,docker,alpine
来源: https://www.cnblogs.com/poo007/p/16485146.html