docker系列1:centos/ubunt 安装和基本使用篇

docker 简介

docker中文网站:http://www.docker.org.cn/book/docker/what-is-docker-16.html
docker系列1:centos/ubunt 安装和基本使用篇_第1张图片

基本概念: container, image

镜像: is an executable package that includes everything needed to run an application–the code, a runtime, libraries, environment variables, and configuration files.
容器: is launched by running an image,is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with state, or a user process)

1,docker 安装配置

a, ubuntu 安装docker

# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

b,centos7 安装docker

#卸载旧的版本
rpm -qa |grep docker |xargs -n 1  rpm -e --nodeps

#1, 配置docker下载资源
yum -y install yum-utils device-mapper-persisten-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum list docker-ce
###docker.repo
# [dockerrepo]
# name=Docker Repository
# baseurl=https://yum.dockerproject.org/repo/main/centos/7/
# enabled=1
# gpgcheck=1
# gpgkey=https://yum.dockerproject.org/gpg
####docker-ce.repo
# [docker-ce-stable]
# name=Docker CE Stable - $basearch
# baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
# enabled=1
# gpgcheck=1
# gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

#2, 下载安装docker
yum -y install docker-ce
systemctl start docker
systemctl enable docker

#3,查看安装后的dock版本信息
docker version

docker系列1:centos/ubunt 安装和基本使用篇_第2张图片

c, 安装docker-compose

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

d, [配置: 使用阿里云镜像加速]

在阿里云注册docker仓库: https://cr.console.aliyun.com

# repo addr:   registry.cn-beijing.aliyuncs.com/docker_eyeofeagle/
sudo docker login --username=18010031897 registry.cn-beijing.aliyuncs.com

镜像加速 local config :

#1,创建配置文件
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://yywkvob3.mirror.aliyuncs.com"]
}
EOF
#或者编辑 /usr/lib/systemd/system/docker.service文件
#ExecStart=/usr/bin/dockerd --insecure-registry a.a.a.a:5000 --registry-mirrors https://yywkvob3.mirror.aliyuncs.com


#2, 重启docker服务
#sudo systemctl daemon-reload
#sudo systemctl restart docker
service docker restart

# 3, 查看配置是否生效
################# docker info 显示的前后变化############
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Registry Mirrors:					 ##### 新增了一条信息###########
 https://yywkvob3.mirror.aliyuncs.com/  ##### 新增了一条信息###########
Live Restore Enabled: false
Product License: Community Engine

2,docker 镜像管理

root@wang-GA-MA770T-UD3P:/home/wang/txt#    docker images  |grep registry.cn-beijing.aliyuncs.com/kube_eyeofeagle/  |awk '{print $1":"$2}'
registry.cn-beijing.aliyuncs.com/kube_eyeofeagle/kube-proxy:v1.13.1

root@wang-GA-MA770T-UD3P:/home/wang/txt# docker images  |grep registry.cn-beijing.aliyuncs.com/kube_eyeofeagle/  |awk '{print $1":"$2}'|xargs -n 1 docker push
The push refers to repository [registry.cn-beijing.aliyuncs.com/kube_eyeofeagle/kube-proxy]
f9cdaf1489a0: Pushed 
5fe6d025ca50: Pushed 

a, docker 查看本地镜像

docker系列1:centos/ubunt 安装和基本使用篇_第3张图片

b,docker 运行程序

i, nignx

如上有一个nginx镜像: 【 nginx latest 568c4670fa80 2 weeks ago 109MB 】
找到 IMAGE ID , 也就是568c4670fa80,运行它,创建一个新的容器
在这里插入图片描述![在这里插入图片描述](https://img-blog.csdnimg.cn/20181215172652751.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2V5ZW9mZWFnbGU=,size_16,color_FFFFFF,t_70

ii, mysql

root@wang-pc:/home/wang# docker run -itd --name mysql2 -e MYSQL_ROOT_PASSWORD=123456 -p 33060:3306 mysql
docker: Error response from daemon: Conflict. The container name "/mysql2" is already in use by container 43ef314a49e54b1e98e568d7e58f9c3ce25b7f938415822da800cfb46787f218. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.

root@wang-pc:/home/wang# lsof -i:33060
COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
docker-pr 26152 root    4u  IPv6 1439331      0t0  TCP *:33060 (LISTEN)

root@wang-pc:/home/wang#  mysql -uroot -p123456 -h0.0.0.0
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '0.0.0.0' (111)
root@wang-pc:/home/wang# mysql -uroot -p123456 -h0.0.0.0 -P33060
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.13 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> 
#修改 远程登陆权限: mysql8 语法有些不同
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> flush privileges;

3, docker容器和主机的交互:

A,文件拷贝

在这里插入图片描述docker系列1:centos/ubunt 安装和基本使用篇_第4张图片

B, 由主机进入容器中

在这里插入图片描述

4, docker 管理容器

查看运行中的所有容器, 停止容器,删除容器
docker系列1:centos/ubunt 安装和基本使用篇_第5张图片

你可能感兴趣的:(docker)