安装Docker后,启动docker
[email protected]:/home/xxx/Documents# service docker start
停止docker命令
[email protected]:/home/xxx/Documents# service docker stop
查看运行的docker版本
[email protected]:/home/xxx/Documents# docker --version
Docker version 18.09.7, build 2d0083d
通过pull命令获取相关镜像
[email protected]:/home/xxx/Documents# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1ab2bdfe9778: Pull complete
a17e64cfe253: Pull complete
e1288088c7a8: Pull complete
Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Status: Downloaded newer image for nginx:latest
获取镜像后,通过docker run
使其运行起来
[email protected]:/home/xxx/Documents# docker run -d -p 8800:80 --name nginx_zhao nginx
780f3120141d2e4c861f3ab190f67d233130b87645db5e93eeef6ada108162fa
[email protected]:/home/xxx/Documents# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
780f3120141d nginx "nginx -g 'daemon of…" 14 seconds ago Up 12 seconds 0.0.0.0:8800->80/tcp nginx_zhao
sudo docker ps -a 列出所有容器, 不加 -a 仅列出正在运行的
sudo docker run -d -p 8800:80 --name nginx_zhao nginx
表示运行指定的镜像
dudo docker run -d --privileged=true -p 83:80 --name nginx83 nginx
表示提升权限
宿主主机端口:容器内部端口
-d 后台运行
-p 8800:80 是指定对外暴露的端口 容器内部用80 对应外部宿主主机的的8800 代理一样
--name指定容器的名字 最后的nginx 代表要运行的镜像名字 有tag的加上tag 如 nginx:xxx 默认为latest
然后访问宿主主机地址+8800端口 出现下图表明nginx已经成功运行
pull到的镜像有很多需要修改的地方,比如配置文件等,使用exec
命令进入到容器内部进行操作
[email protected]:/home/xxx/Documents# docker exec -it 780f3120141d /bin/bash
[email protected]:/# pwd
/
[email protected]:/# ls -l
total 64
drwxr-xr-x 2 root root 4096 Aug 12 00:00 bin
drwxr-xr-x 2 root root 4096 May 13 20:25 boot
drwxr-xr-x 5 root root 340 Sep 11 01:38 dev
drwxr-xr-x 1 root root 4096 Sep 11 01:38 etc
drwxr-xr-x 2 root root 4096 May 13 20:25 home
drwxr-xr-x 1 root root 4096 Aug 15 21:22 lib
drwxr-xr-x 2 root root 4096 Aug 12 00:00 lib64
drwxr-xr-x 2 root root 4096 Aug 12 00:00 media
drwxr-xr-x 2 root root 4096 Aug 12 00:00 mnt
drwxr-xr-x 2 root root 4096 Aug 12 00:00 opt
dr-xr-xr-x 290 root root 0 Sep 11 01:38 proc
drwx------ 2 root root 4096 Aug 12 00:00 root
drwxr-xr-x 1 root root 4096 Sep 11 01:38 run
drwxr-xr-x 2 root root 4096 Aug 12 00:00 sbin
drwxr-xr-x 2 root root 4096 Aug 12 00:00 srv
dr-xr-xr-x 13 root root 0 Sep 11 01:38 sys
drwxrwxrwt 1 root root 4096 Aug 15 21:22 tmp
drwxr-xr-x 1 root root 4096 Aug 12 00:00 usr
drwxr-xr-x 1 root root 4096 Aug 12 00:00 var
[email protected]:/# nginx -v
nginx version: nginx/1.17.3
[email protected]:/# exit
exit
通过inspect
获得镜像详细信息,通过grep
检索需要的项目
[email protected]:/home/xxx/Documents# docker inspect nginx_zhao |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
进入容器内部并修改后,怎么生成新的镜像供下次直接使用
[email protected]:/home/xxx/Documents# docker commit nginx_zhao zhao/nginx:v1.0
sha256:75fe4514801e00e544741efa375fe4eb764186f7a9a75d76c27ee488070c84ae
[email protected]:/home/xxx/Documents# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zhao/nginx v1.0 75fe4514801e 27 seconds ago 126MB
docker commit nginx_zhao zhao/nginx:v1.0
,其中nginx_zhao
表示刚修改的容器名字或者id,zhao/nginx:v1.0
表示保存的镜像名字 :后面为tag
刚刚commit
的镜像是保存在本地的,如果要提交到网络上供其他人pull 使用呢?
可以在https://cloud.docker.com/上注册一个账号,提交本地镜像到https://cloud.docker.com/上去。
参考:Docker技术入门与实战 Docker基本命令使用及发布镜像
原文地址:https://www.cnblogs.com/eugene0/p/11515580.html