Dockde容器操作
我们在启动容器的时候。会发现明明已经启动容器了,为什么还是会查看不到容器进程呢?
Docker 容器启动时,默认会把容器内部第一个进程,也就是pid=1的程序,作为docker容器是否正在运行的依据,如果docker 容器pid=1的进程挂了,那么docker容器便会直接退出
Docker容器命令
参数选项:
docker run选项 | 作用 |
---|---|
-d | 放在后台运行 |
-name | 指定容器名 |
-i | 容器交互模式input |
-t | 给容器提供一个终端tty |
--rm | 容器退出自动删除 |
--restart always | 容器开机自启 |
-p(小) | 端口映射 |
-P(大) | 随机端口映射 |
-v | 映射数据卷 |
# 查看正在运行的容器
[root@docker01 ~]# docker ps
[root@docker01 ~]# docker ps -a -q
c494097ad9d6
b5c120b9c97d
6115b211d38f
## 选项:
-a:查看所有容器(正在运行的和退出的)
-q:只显示容器的ID号
# 创建容器
[root@docker01 ~]# docker create nginx:alpine
# 启动容器
1)前提:容器pid为1的进程必须在前台运行,否则无法启动
[root@docker01 ~]# docker start 0cac7a6a774a
# 停止容器
[root@docker01 ~]# docker stop 0cac7a6a774a
# 重启容器
[root@docker01 ~]# docker restart 0cac7a6a774a
# 容器无法stop时,可以强制kill
[root@docker01 ~]# docker kill 0cac7a6a774a
## 重点:运行容器
1)在终端前台运行容器
[root@docker01 ~]# docker run nginx:alpine
2)后台运行
[root@docker01 ~]# docker run -d nginx:alpine
3)指定容器名称
[root@docker01 ~]# docker run --name web01 -d nginx:alpine
4)启动容器并连接容器
[root@docker01 ~]# docker run --name web03 -it nginx:alpine /bin/sh
[root@docker01 ~]# docker run --name c7_v1 -it centos:7 /bin/bash
5)容器退出自动删除
[root@docker01 ~]# docker run --rm --name c7_v2 -it centos:7 /bin/bash
6)docker服务重启后,容器开机自启
[root@docker01 ~]# docker run --restart always --name web10 -it -d nginx:alpine
7)取消容器开机自启
[root@docker01 ~]# docker update --restart=no web10
8)连接容器
[root@docker01 ~]# docker exec -it web10 /bin/sh
9)拷贝宿主机的文件到容器中
[root@docker01 ~]# docker cp /root/main.sh web15:/
[root@docker01 ~]# docker cp web15:/main.sh /opt/
10)删除容器 //删除所有停止和已死掉的容器,慎重使用
[root@docker01 ~]# docker rm $(docker ps -a -q)
11)删除单个容器
[root@docker01 ~]# docker stop 容器id
[root@docker01 ~]# docker rm 容器id
容器端口映射
## 先查看声明了哪些端口
[root@docker01 ~]# docker inspect nginx:alpine
[
{
"Id": "sha256:cc44224bfe208a46fbc45471e8f9416f66b75d6307573e29634e7f42e27a9268",
"RepoTags": [
"nginx:alpine"
],
"RepoDigests": [
"nginx@sha256:eb05700fe7baa6890b74278e39b66b2ed1326831f9ec3ed4bdc6361a4ac2f333"
],
"Parent": "",
"Comment": "",
"Created": "2021-12-29T19:29:13.874392808Z",
"Container": "7575fb322d4b00ae10e2adb9fa8ef3d9c855b8530ff2d4d0187a2734302b3469",
"ContainerConfig": {
"Hostname": "7575fb322d4b",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {} #//容器端口
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.5",
"NJS_VERSION=0.7.1",
"PKG_RELEASE=1"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:6818f894a012ecd044735f31c67f74ab749d254ca8385096062680514d3c3c5e",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.5",
"NJS_VERSION=0.7.1",
"PKG_RELEASE=1"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:6818f894a012ecd044735f31c67f74ab749d254ca8385096062680514d3c3c5e",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 23456409,
"VirtualSize": 23456409,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/0c0c0cb2a53e190ecba292c2398fd650ed6621c1ee50003771879e411cc2a6ee/diff:/var/lib/docker/overlay2/f9a4c042c2a01b760ba432599c33b6ff06b621e0afd6a7b7436adfc3b7f44a2f/diff:/var/lib/docker/overlay2/3958afcc640bc5b7899257f8811fe709909039734e2574ede9a975679a9e2032/diff:/var/lib/docker/overlay2/3badf218f71ef57ab4555a7e57aa1f6cd2a38076f7b956cee256adf5f3c57411/diff:/var/lib/docker/overlay2/d3a6e9022fdba37266677efcb9f67d30b224388ebdb74399293a3e7e379c700e/diff",
"MergedDir": "/var/lib/docker/overlay2/4a01fa2d606f00545e58bbc4375cc286cbfe1e6c427886d5171633dbe5817c01/merged",
"UpperDir": "/var/lib/docker/overlay2/4a01fa2d606f00545e58bbc4375cc286cbfe1e6c427886d5171633dbe5817c01/diff",
"WorkDir": "/var/lib/docker/overlay2/4a01fa2d606f00545e58bbc4375cc286cbfe1e6c427886d5171633dbe5817c01/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:8d3ac3489996423f53d6087c81180006263b79f206d3fdec9e66f0e27ceb8759",
"sha256:1c9c1e42aafaa6067e6591a2b3214b4d6c6c357b597377bd395486d9999209cd",
"sha256:3f87f0a06073927060d269480f2c55cd4ac978732291be1e900a0e834dbc0db0",
"sha256:5ee3266a70bd586778d7d3dc36fd1dbc2ab407d559615a1c3508697eac86142f",
"sha256:0e835d02c1b5a93d74af44edda6dc7b36938429847b13d0553a5ba4adab1e686",
"sha256:419df8b600324530e8b64cabca7b0e6b959efc8d41fb2b8253014a4099747501"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
# 指定端口映射 //本机的8088指向容器的80端口
[root@docker01 ~]# docker run --name web11 -p 8088:80 -d nginx:alpine
-p 宿主机端口:容器端口
# 指定IP和端口映射
[root@docker01 ~]# docker run --name web13 -p 172.16.1.101:80:80 -d nginx:alpine
[root@docker01 ~]# docker run --name web13 -p 10.0.0.101:80:80 -d nginx:alpine
# 随机端口映射
[root@docker01 ~]# docker run --name web14 -P -d nginx:alpine
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
e6b69f7b89f3 nginx:alpine "/docker-entrypoint.…" 12 seconds ago Up 12
seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp web14
[root@docker01 ~]# docker run --name web15 -p 10.0.0.101::80 -d nginx:alpine
-p 宿主机IP::容器端口
-p 宿主机IP::容器端口/udp
-p 1-65535:1-65535
使用docker运行nginx做小游戏
手动解压
#启动容器
[root@docker01 ~]# docker run --name web01 -p80:80 -d nginx:alpine
#复制游戏代码到容器立马
[root@docker01 ~]# docker cp h5_games.zip web01:/
#连接容器
[root@docker01 ~]# docker exec -it web01 /bin/sh
/ # vi /etc/nginx/conf.d/1_h5.conf
/ # unzip h5_games.zip -d /usr/share/nginx/html
/ # mv /usr/share/nginx/html/h5_games/* .
数据卷映射
# -v 宿主机目录:容器目录
[root@docker01 h5_games]# docker run -p 80:80 -v /opt/h5_games:/usr/share/nginx/html
-d nginx:alpine
# -v 容器目录(宿主机上随机目录)
[root@docker01 h5_games]# docker run -v /usr/share/nginx/html -d nginx:alpine
[root@docker01 _data]# docker inspect 9f117a84b020bcadaee3
手动制作镜像
# 1.首先要启动一个基础镜像
[root@docker01 _data]# docker run -it -d centos:centos7 /bin/bash
# 2.进入到容器立马换源 //更换华为源
[root@44286c1a2d30 /]# bash <(curl -sSL https://linuxmirrors.cn/main.sh)
# 3.安装nginx
[root@448571c63be8 /]# yum install -y nginx unzip
# 4.写配置文件
[root@448571c63be8 /]# vi /etc/nginx/conf.d/h5.conf
server{
listen 80;
server_name _;
root /code;
index index.html;
}
# 5.创建code目录
[root@448571c63be8 /]# mkdir /code
# 6.拷贝代码 //切记,不要直接直接退出容器,不然前面所有操作全部作废;在开一个终端
[root@docker01 ~]# docker cp /root/h5_games.zip 44286c1a2d30:/code
# 7.部署代码 //回到容器站点目录
[root@448571c63be8 code]# unzip h5_games.zip
[root@448571c63be8 code]# mv h5_games/* .
# 8.将容器打成镜像 //回到新开的终端把容器打成镜像
[root@docker01 ~]# docker commit 44286c1a2d30 nginx_h5:v1
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_h5 v1 1eb5f8b128bd 6 seconds ago 912MB #//生成的新镜像
nginx alpine cc44224bfe20 20 months ago 23.5MB
alpine latest c059bfaa849c 21 months ago 5.59MB
centos centos7 eeb6ee3f44bd 24 months ago 204MB
# 9.导出镜像
[root@docker01 _data]# docker save nginx_h5:v1 > /tmp/h5.tgz
# 10.拷贝镜像 //发送给docker02
[root@docker01 _data]# scp /tmp/h5.tgz 172.16.1.102:/tmp
# 11.导入镜像
[root@docker02 ~]# ll /tmp
total 900980
-rw-r--r-- 1 root root 922600960 Sep 8 15:31 h5.tgz
drwx------ 2 root root 6 Sep 7 14:27 vmware-root_6145-1983849898
drwx------ 2 root root 6 Sep 8 07:53 vmware-root_6147-1992172584
[root@docker02 ~]# docker load < /tmp/h5.tgz
[root@docker02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_h5 v1 1eb5f8b128bd 5 minutes ago 912MB
alpine latest c059bfaa849c 21 months ago 5.59MB
centos v7.9 eeb6ee3f44bd 24 months ago 204MB
# 12.启动
[root@docker02 ~]# docker run -p 80:80 -d nginx_h5:v1 /sbin/nginx -g 'daemon off;'
# 13.浏览器访问