【Docker】容器

目录

[1. 容器启动](#1. 容器启动)

[2. 容器启动/重启/停止](#2. 容器启动/重启/停止)

[3. 进入容器](#3. 进入容器)

[4. 容器查询](#4. 容器查询)

[5. docker 镜像的构建](#5. docker 镜像的构建)

[方式一:docker 容器 commit](#方式一:docker 容器 commit)

[方式二:Dockerfile 定制镜像](#方式二:Dockerfile 定制镜像)


1. 容器启动

复制代码
docker run --it/-d --p/P --name imageID/name

2. 容器启动/重启/停止

bash 复制代码
docker start
docker restart
docker stop

3. 进入容器

复制代码
# 查看容器id
docker ps
复制代码
# 容器ID:057572c53888
docker attach containerID 
docker exec -it containerID /bin/bash

4. 容器查询

复制代码
docker inspect containerID
复制代码
docker top containerID
复制代码
docker diff containerID

5. docker 镜像的构建

方式一:docker 容器 commit

复制代码
docker commit

1、运行docker

复制代码
docker run --name webserver -d -p 80:80 nginx

2、进入容器的方法

复制代码
# 方式1
docker attach webserver

ctrl+p、q退出!!!!

复制代码
# 方式2
docker exec -it webserver bash

docker exec -it 容器id bash -c "echo 
'<h1>Hello,I am your container.</h1>' > /usr/share/nginx/html/index.html"

3、提交docker

复制代码
docker commit --author "szzz" --message "chang index" webserver nginx:v2

4、运行docker

复制代码
docker run --name webserver2 -d -p 81:80 nginx:v2

5、查看历史记录

复制代码
docker history nginx:v2

6、测试:http://192.168.9.10:81 (用该虚拟机的ip地址)

方式二:Dockerfile 定制镜像

确保 docker 主机可以上网

复制代码
docker build

Dockerfile 的编写方法

1、 网络确认

复制代码
ping www.baidu.com

2、准备文件夹和 Dockerfile 文件

复制代码
mkdir static_web # 创建文件夹

cd static_web # 进入文件夹

touch Dockerfile # 创建一个 Dockerfile 文件

3、编写 Dockerfile

vi Dockerfile 编辑文件,写入镜像代码

复制代码
# Version: 0.0.1
FROM ubuntu:14.04
MAINTAINER James Turnbull "james@example.com"
RUN apt-get update && apt-get install -y nginx
RUN echo 'Hi, I am in your containe' > /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

4、构建镜像

复制代码
docker build -t="jamur01/static_web" 

5、容器测试

复制代码
docker run -itd --name test -p 8080:80 jamur01/static_web

http://192.168.9.10:8080 (根据自己的虚拟机ip地址访问)

相关推荐
Wasim404几秒前
【Linux】网络命令
linux·网络安全·linux网络命令·linux网络安全入门
BUG_MeDe19 分钟前
从Json对象中提取某个对象的一点注意--libjson-c
linux·json
坚持就完事了41 分钟前
Linux的which命令
linux·运维·服务器
skywalk816342 分钟前
kitto_plus报错:AttributeError: module ‘kotti_plus‘ has no attribute ‘security‘
linux·开发语言·python
和小潘一起学AI42 分钟前
centOS安装neo4j
linux·运维·服务器
HealthScience1 小时前
H20服务器多卡运行有错误gpu_partition ,tmux错误
linux·运维·服务器
_Emma_1 小时前
【Raspberry PI】Raspberry Pi HEVC (H.265) 硬件解码器
linux·驱动开发·视频编解码
RisunJan1 小时前
Linux命令-netstat(查看Linux中网络系统状态信息)
linux·运维·服务器
Hello.Reader1 小时前
双卡 A100 + Ollama 生产部署从安装、踩坑、调优到最终可上线方案
linux·人工智能·算法
SPC的存折1 小时前
1、MySQL数据库基础
linux·运维·数据库·mysql