Docker

centos7.2 安装 docker

  yum install docker

centos7.2安装docker

在 docker 里禁用 selinux

vim /etc/sysconfig/docker
--selinux-enabled=false

CentOS7.2 安装Docker

centos7 安装 docker 并设置开机启动

systemctl enable docker

centos7安装docker并设置开机启动

docker 启动,重启,关闭命令

sudo service docker start

docker服务启动,重启,关闭命令

docker-compose 安装

https://github.com/docker/compose/releases

curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

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

Docker 镜像与容器

拉取镜像

  1. docker search nginx
  2. docker pull nginx
  3. docker run -p 80:80 –name ng_container -d nginx

Docker 删除 镜像

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

Docker 运行容器

  • -d: 后台运行容器,并返回容器 ID
  • -v: 主机的目录/app/tomcat/webapps 映射到容器的/app/tomcat/webapps
  • -t:进入终端
  • -i:获得一个交互式的连接,通过获取 container 的输入
docker run -d -p 8080:8080 -p 8009:8009 -v /app/tomcat/webapps:/app/tomcat/webapps tomcat

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

Docker 进入容器

docker exec -it container_id bash
docker exec -it container_name /sh

Docker 退出容器

  • exit
  • CTRL+D

Docker 删除容器

docker ps -a

docker stop container_id
docker rm container_id

docker 同时删除停止的容器

docker rm \$(docker container ls -f "status=exited" -q)

Docker 容器内安装 ifconfig netstat ping vim 等测试工具的方法

docker容器内安装ifconfig netstat ping vim 等测试工具的方法

apt-get install iputils-ping

apt-get install vim

查看容器详情

docker inspect container_id

Dockerfile 编写样例

https://github.com/ekoz/docker-kbase/blob/master/Dockerfile

Docker save 与 Docker export 的区别

docker save与docker export的区别

docker save \
ekozhan/nginx:1.0 \
ekozhan/elasticsearch:1.0 \
ekozhan/kibana:1.0 \
| gzip > docker_images_20201202_1.0.tar.gz

将容器打包成镜像并启动

[root@localhost mysql]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
cc72f2b081b9        mysql:8.0           "docker-entrypoint.s…"   3 weeks ago         Up 6 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-docker
[root@localhost mysql]# pwd
/opt/app/mysql
[root@localhost mysql]# ls
data  docker-compose.yml
[root@localhost mysql]# docker export -o mysql-export.tar cc72f2b081b9
[root@localhost mysql]# ls
data  docker-compose.yml  mysql-export.tar
[root@localhost mysql]# docker import mysql-export.tar ekozhan/mysql:0.1
sha256:cde3f783415f0dc7a76d7df89765ced2e59db38dff3e2534d859b476f5497e15
[root@localhost mysql]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
ekozhan/mysql            0.1                 cde3f783415f        5 seconds ago       538MB
mysql                    8.0                 8e85dd5c3255        3 weeks ago         544MB
wurstmeister/kafka       latest              40094a582680        2 months ago        435MB
wurstmeister/zookeeper   latest              3f43f72cb283        21 months ago       510MB
[root@localhost mysql]# docker run --name mysql3306 -e MYSQL_ROOT_PASSWORD=123456  -d -p 3308:3308 cde3f783415f docker-entrypoint.sh mysqld
dd344b94113c301930b5b0a5184ed41b4679e7a5b4e45adb69f894df1a870417
[root@localhost mysql]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
dd344b94113c        cde3f783415f        "docker-entrypoint.s…"   4 seconds ago       Up 3 seconds        0.0.0.0:3308->3308/tcp              mysql3306
cc72f2b081b9        mysql:8.0           "docker-entrypoint.s…"   3 weeks ago         Up 15 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-docker

Docker: 限制容器可用的内存

Docker: 限制容器可用的内存

Demo

# mysql

docker run -d -p 3306:3306 --restart always --privileged=true --name mysql3306 -e MYSQL_USER="u_teach" -e MYSQL_PASSWORD="u_teach" -e MYSQL_ROOT_PASSWORD="rOOt" -v /home/ekozhan/mysql/data:/var/lib/mysql -v /home/ekozhan/mysql/logs:/var/log/mysql 1e4405fe1ea9

# mongodb

docker run --name mongo_container -p 27017:27017 -e TZ="Asia/Shanghai" --restart always --privileged=true -v /opt/docker/mongodb/data/db:/data/db -v /opt/docker/mongodb/data/configdb:/data/configdb -v /opt/docker/mongodb/data/logs:/data/logs -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=rOOt -d 57c2f7e05108 --config /opt/docker/mongodb/data/mongod.conf

Docker 使用国内镜像仓库

vim /etc/docker/daemon.json

{
    "registry-mirrors" : [
        "https://registry.docker-cn.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://hub-mirror.c.163.com",
        "https://cr.console.aliyun.com/"
    ]
}

Dockerfile 中使用国内镜像仓库demo

alpine:latest

FROM alpine:latest

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
    apk --update add curl && rm -rf /var/cache/apk/*

python

FROM python:3.9.0

RUN pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/

RUN pip config set install.trusted-host mirrors.aliyun.com

WORKDIR /opt

docker-compose

注意

项目中使用最多的就是 docker-compose


docker-compose down
docker-compose build
docker-compose up -d

相关资料

https://yeasy.gitbooks.io/docker_practice/

https://www.runoob.com/docker/docker-compose.html

CentOS8 安装 docker 遇到的报错

yum update

yum install -y yum-utils

yum-config-manager  --add-repo   https://download.docker.com/linux/centos/docker-ce.repo

yum install -y https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm

yum install -y docker-ce containerd.io

CentOS8安装 Docker报错 package docker-ce-3:19.03.12-3.el7.x86_64 requires containerd.io 」= 1.2.2-3