Docker 容器安装及其相关操作

安装环境:

# 需要两台几点进行安装
[root@docker01 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@docker01 ~]# uname -r
3.10.0-327.el7.x86_64
[root@docker01 ~]# hostname -I
10.0.0.100 172.16.1.100
[root@docker02 ~]# hostname -I
10.0.0.101 172.16.1.101

在两个节点上都进行操作

wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
sed -i ‘s#download.docker.com#mirrors.ustc.edu.cn/docker-ce#g’ /etc/yum.repos.d/docker-ce.repo
yum install docker-ce -y

修改在 docker01 配置:

# 修改启动文件,监听远程端口
vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://10.0.0.100:2375
systemctl daemon-reload
systemctl enable docker.service
systemctl restart docker.service
# ps -ef 检查进行,是否启动

在 docker02 测试

[root@docker02 ~]# docker -H 10.0.0.100 info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.12.0-ce
Storage Driver: devicemapper

Docker基础命令操作

查看 docker 相关信息

[root@docker01 ~]# docker version
Client:
Version: 17.12.0-ce
API version: 1.35
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:10:14 2017
OS/Arch: linux/amd64
Server:
Engine:
Version: 17.12.0-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:12:46 2017
OS/Arch: linux/amd64
Experimental: false

配置 docker 镜像加速

vi /etc/docker/daemon.json
{
“registry-mirrors”: [“https://registry.docker-cn.com”]
}

启动Docker容器

[root@docker01 ~]# docker run -d -p 80:80 nginx
Unable to find image ‘nginx:latest’ locally
latest: Pulling from library/nginx
e7bb522d92ff: Pull complete
6edc05228666: Pull complete
cd866a17e81f: Pull complete
Digest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d
Status: Downloaded newer image for nginx:latest
8d8f81da12b5c10af6ba1a5d07f4abc041cb95b01f3d632c3d638922800b0b4d
# 容器启动后,在浏览器进行访问测试

#参数说明

run :创建并运行一个容器

-d : 放入后台

-p: 映射端口

nginx : 镜像名称

搜索官方仓库镜像

[root@docker01 ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 3992 [OK]
ansible/centos7-ansible Ansible on Centos7 105 [OK]

获取镜像

根据镜像名称拉取镜像

[root@docker01 ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
af4b0a2388c6: Downloading 34.65MB/73.67MB

查看当前主机镜像列表

[root@docker01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest ff426288ea90 3 weeks ago 207MB
nginx latest 3f8a4339aadd 5 weeks ago 108MB

拉第三方镜像方法

docker pull index.tenxcloud.com/tenxcloud/httpd

导出镜像

[root@docker01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest ff426288ea90 3 weeks ago 207MB
nginx latest 3f8a4339aadd 5 weeks ago 108MB
# 导出
[root@docker01 ~]# docker image save centos > docker-centos.tar.gz

删除镜像

[root@docker01 ~]# docker image rm centos:latest
[root@docker01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 3f8a4339aadd 5 weeks ago 108MB

导入镜像

[root@docker01 ~]# docker image load -i docker-centos.tar.gz
e15afa4858b6: Loading layer 215.8MB/215.8MB
Loaded image: centos:latest
[root@docker01 ~]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest ff426288ea90 3 weeks ago 207MB
nginx latest 3f8a4339aadd 5 weeks ago 108MB

查看镜像的详细信息

[root@docker01 ~]# docker image inspect centos

容器的日常管理

容器的起/停

#常用

[root@docker01 ~]# docker run nginx

#不常用

[root@docker01 ~]# docker create centos:latest /bin/bash
bb7f32368ecf0492adb59e20032ab2e6cf6a563a0e6751e58930ee5f7aaef204
[root@docker01 ~]# docker start stupefied_nobel
stupefied_nobel

#快速启动容器

[root@docker01 ~]# docker run centos:latest /usr/bin/sleep 20;

容器内的第一个进程必须一直处于运行的状态,否则这个容器,就会处于退出状态!

#查看正在运行的容器

[root@docker01 ~]# docker container ls

#查看正在运行的容器

[root@docker01 ~]# docker ps

#查看容器详细信息/ip

[root@docker01 ~]# docker container inspect 容器名称/id

#查看所有容器(包括未运行的)

[root@docker01 ~]# docker ps -a

停止容器

[root@docker01 ~]# docker stop 容器名称/id

或者

[root@docker01 ~]# docker container kill 容器名称/id

进入容器方法

#启动时进去方法

[root@docker01 ~]# docker run -it #参数:-it 可交互终端
[root@docker01 ~]# docker run -it nginx:latest /bin/bash
root@79241093859e:/#

#退出/离开容器

ctrl+p & ctrl+q

启动后进入容器的方法

[root@docker01 ~]# docker run -it centos:latest
[root@1bf0f43c4d2f /]# ps -ef

attach 进入容器,使用 pts/0 ,会让所用通过此方法进如放入用户看到同样的操作。

[root@docker01 ~]# docker attach 1bf0f43c4d2f
[root@1bf0f43c4d2f /]# ps -ef

自命名启动一个容器 –name

[root@docker01 ~]# docker attach 1bf0f43c4d2f
[root@1bf0f43c4d2f /]# ps -ef

exrc 进入容器方法(推荐使用)

[root@docker01 ~]# docker exec -it clsn1 /bin/bash
[root@b20fa75b4b40 /]# 重新分配一个终端
[root@b20fa75b4b40 /]# ps -ef

删除所有容器

[root@docker01 ~]# docker rm -f `docker ps -a -q`
# -f 强制删除

启动时进行端口映射

#-p 参数端口映射

#指定多个:-p  81:80 –p 443:443

[root@docker01 ~]# docker run -d -p 8888:80 nginx:latest

#)# 需要镜像支持

docker run -P (大写的 P)

© 版权声明

☆ END ☆
喜欢就点个赞吧
点赞0 分享
图片正在生成中,请稍后...