---
title: "docker初步使用"
author: "adashao"
date: "2021-09-20T09:22:59Z"
tags: []
categories: ["编程语言"]
slug: "docker"
draft: false
lastmod: "2021-09-20T09:52:49Z"
id: 2521040
url: "https://adashao.iteye.com/blog/2521040"
---

[root@iZ2zebdi0qh7gi3c1x5z5fZ docker]# cd apache/
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# ls
Dockerfile  index.html  run.sh

 
#创建Dockerfile
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# vim Dockerfile 
FROM centos#基于的基础镜像，在centos内核运行
MAINTAINER <549353504@qq.com> #维护者信息
RUN yum -y update  
RUN yum -y install httpd
EXPOSE 80  #开启80端口，给外部映射用
ADD index.html /var/www/html/index.html #将宿主机网站文件加入到镜像内
ADD run.sh /run.sh #将执行脚本复制到镜像内
RUN chmod 755 /run.sh #提权
CMD ["/run.sh"] #启动容器时执行脚本，开启Apache服务
 
#当前目录里创建index.html和run.sh
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# vim run.sh 
#!/bin/bash
rm -rf /run/httpd/* 
exec /usr/sbin/apachectl -D FOREGROUND #启动容器时启动服务
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# vim index.html 
hello world
 
#创建镜像，必须在当前有Dockerfile的目录下，注意空格加. 代表当前目录
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker build -t httpd:new .
#查看创建的镜像
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
httpd        new       b2c9b41d9b76   24 minutes ago   390MB
centos       latest    5d0da3dc9764   4 days ago       231MB

 
#运行镜像为容器
-p 为映射端口，将容器端口80映射为宿主主机端口1212（必须是未被占用的）
-P 不指定节点端口，随机指定端口，默认从32168端口开始分配
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker run --name t1 -d -p 80:80 httpd:new  
 
#查看容器状态
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker ps -a
CONTAINER ID   IMAGE       COMMAND     CREATED          STATUS          PORTS                NAMES
67e7579e1543   httpd:new   "/run.sh"   20 minutes ago   Up 19 minutes   0.0.0.0:80->80/tcp   t1
#在浏览器地址栏输入服务器IP,显示
hello world

#删除容器镜像，先停止容器，再删除容器，最后删除镜像
CONTAINER ID   IMAGE       COMMAND     CREATED         STATUS         PORTS                NAMES
7bafdf523cdc   httpd:new   "/run.sh"   2 minutes ago   Up 2 minutes   0.0.0.0:80->80/tcp   t1
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker stop 7bafdf523cdc
7bafdf523cdc
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        new       700531658701   3 minutes ago   390MB
centos       latest    5d0da3dc9764   4 days ago      231MB
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker rmi 700531658701
Error response from daemon: conflict: unable to delete 700531658701 (must be forced) - image is being used by stopped container 7bafdf523cdc
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker rm 7bafdf523cdc
7bafdf523cdc
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        new       700531658701   7 minutes ago   390MB
centos       latest    5d0da3dc9764   4 days ago      231MB
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker rmi 700531658701
Untagged: httpd:new
Deleted: sha256:700531658701e197cbef91b68c7702c35b23c96384cf9af05a34034a2d8b6524
Deleted: sha256:9b744bfaaafaa6e1c791d7be788981f9a965fb98f88c696ee6f9b958664f608e
Deleted: sha256:bfe8d0537d0a70e2231d3e91a4ffad96ebf2519b51f8aefdd7ecd1657ca250f9
Deleted: sha256:1502c7741d23bf723deccad927797864a47a486f0df4e7e0ec23f093848d0473
Deleted: sha256:a0ef0001274fa78cab871e7035e9ebd8e1eaa3763d3ab73410ea0f209925e574
Deleted: sha256:57702c12e3958f6cceb9a9b5297744ec85e516dda68b178e10895931e923ebd9
Deleted: sha256:15302f2b0a6fced9a5b50885990dcf4e95a85e3ec66ef8de874d5cc4b13c7bc9
Deleted: sha256:dfd0398fc04f7a822dbe9b74c51969b5e1f742ca8ea154dfc1e266d986048153
Deleted: sha256:6e26d122bc7c6023b7fd24098e5d2f10ce37e02594e67f422595489dac1d64ed
Deleted: sha256:cfed2a985f8a971d1279caa030cdbca26e379302dfa448127441ab3bceb26fbc
Deleted: sha256:d153fd42d8132cd79be6febb6df832fdfe442bb82c6569e6faad94c629b4aa9c
Deleted: sha256:4c0618686e676f5ce360fff41b03bd72f7a108b09b634083c76bf937ea718f52
Deleted: sha256:3137303e9f7567099169b06a966e713efd65e7ebe145ab46d82056334f201de6
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker rmi 5d0da3dc9764
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@iZ2zebdi0qh7gi3c1x5z5fZ apache]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
