码迷,mamicode.com
首页 > 其他好文 > 详细

docker常用操作

时间:2019-09-16 23:37:08      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:efault   isp   toc   添加   war   php   fpm   常用   files   

TOC

镜像仓库

登录到一个镜像仓库

[root@docker-test volumes]# docker login -u liuxz -p "密码"
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

查找相关镜像

[root@docker-test volumes]# docker search nginx 
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                             Official build of Nginx.                        11953               [OK]                
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   1654                                    [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   740                                     [OK]
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   77                                      
bitnami/nginx                     Bitnami nginx Docker Image                      70                                      [OK]
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   53                                      [OK]

拉取镜像

[root@docker-test ~]# docker pull ubuntu:16.04

推送镜像

推送镜像需要给镜像名称改变。名称命名:/左边的是镜像仓库的地址。/ 右边冒号左边为镜像仓库中的目录,冒号右边为镜像tag。不设置tag,默认推送的tag为laste

[root@docker-test dockerimages]# docker push liuxz/liuxz:test-nginxv3

本地镜像管理

查看本地所有镜像

[root@docker-test volumes]# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
nginx                  v4                  b85eacba49dd        24 hours ago        126MB
mysql                  5.7                 383867b75fd2        4 days ago          373MB
127.0.0.1:5000/nginx   v3                  06eee3f347f4        5 days ago          126MB
nginx                  v3                  06eee3f347f4        5 days ago          126MB
liuxz/liuxz            test-nginxv3        06eee3f347f4        5 days ago          126MB
nginx                  v2                  4b9bb110e222        6 days ago          126MB
busybox                latest              19485c79a9bb        11 days ago         1.22MB
nginx                  latest              5a3221f0137b        4 weeks ago         126MB
registry               2.6.2               d5ef411ad932        6 months ago        28.5MB

删除镜像

如果该镜像运行着容器,需要停止并删除容器后才能删除该镜像

[root@docker-test volumes]# docker rmi nginx:v3
Untagged: nginx:v3

打包镜像文件到一个文件

[root@docker-test dockerimages]# docker save -o nginxv3.tar.gz nginx:v3

载入打包的镜像文件

[Loaded image: nginx:v3root@docker-test dockerimages]# docker load -i nginxv3.tar.gz 
a522e7d83654: Loading layer [==================================================>]  4.096kB/4.096kB

制作镜像文件

需要创建Dockerfile 文件

root@docker-test dockerimages]# docker build -t nginx:v3 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : from nginx
 ---> 5a3221f0137b
Step 2/2 : run echo ‘hello,world‘>/usr/share/nginx/html/index.html
 ---> Running in f916f74712fa
Removing intermediate container f916f74712fa
 ---> 06eee3f347f4
Successfully built 06eee3f347f4
Successfully tagged nginx:v3

或者
-f 指定Dockerfile 文件位置

root@docker-test dockerimages]docker build -t  nginx:v4 -f ./Dockerfile .
Sending build context to Docker daemon  2.048kB
Step 1/2 : from nginx
 ---> 5a3221f0137b
Step 2/2 : run echo ‘hello,world‘>/usr/share/nginx/html/index.html
 ---> Running in f916f74712fa
Removing intermediate container f916f74712fa
 ---> 06eee3f347f4
Successfully built 06eee3f347f4
Successfully tagged nginx:v4

本地镜像标记为某一仓库的镜像

[root@docker-test dockerimages]# docker image tag nginx:v3 liuxz/liuxz:test-nginxv3

查看镜像的历史记录

[root@docker-test volumes]# docker history nginx:v4 
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
b85eacba49dd        24 hours ago        /bin/sh -c #(nop) COPY file:e65ef6e80221ae85…   20B                 
796ce4b40ff2        24 hours ago        /bin/sh -c #(nop) WORKDIR /usr/share/nginx/h…   0B                  
897d26a4cb39        24 hours ago        /bin/sh -c echo ‘hello,world‘>/usr/share/ngi…   12B                 
5a3221f0137b        4 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           4 weeks ago         /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           4 weeks ago         /bin/sh -c set -x     && addgroup --system -…   56.8MB              
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.3.5        0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.17.3     0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop) ADD file:330bfb91168adb4a9…   69.2MB  

容器操作

创建一个容器并运行它

后台运行

[root@docker-test volumes]# docker run -d --name mysql5 -p 3310:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=true  -v data:/var/lib/mysql mysql:5.7

列出所有容器(包含运行和未运行)

[root@docker-test volumes]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
e1c3a0a8f065        mysql:5.7           "docker-entrypoint.s…"   4 hours ago         Up 4 hours          33060/tcp, 0.0.0.0:3310->3306/tcp   mysql5
482757e6349e        mysql:5.7           "docker-entrypoint.s…"   5 hours ago         Up 5 hours          33060/tcp, 0.0.0.0:3301->3306/tcp   mysql4
3dea2d1fa813        mysql:5.7           "docker-entrypoint.s…"   6 hours ago         Up 6 hours          33060/tcp, 0.0.0.0:3309->3306/tcp   mysql3
cc877d94adf8        mysql:5.7           "docker-entrypoint.s…"   6 hours ago         Up 6 hours          33060/tcp, 0.0.0.0:3308->3306/tcp   mysql2
2df642bdef21        nginx:v4            "nginx -g ‘daemon of…"   24 hours ago        Up 24 hours         0.0.0.0:8084->80/tcp                mynginx
b3f9f5a5e17b        mysql:5.7           "docker-entrypoint.s…"   25 hours ago        Up 25 hours         0.0.0.0:3306->3306/tcp, 33060/tcp   mysql1

列出运行的容器

[root@docker-test volumes]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
e1c3a0a8f065        mysql:5.7           "docker-entrypoint.s…"   4 hours ago         Up 4 hours          33060/tcp, 0.0.0.0:3310->3306/tcp   mysql5
482757e6349e        mysql:5.7           "docker-entrypoint.s…"   5 hours ago         Up 5 hours          33060/tcp, 0.0.0.0:3301->3306/tcp   mysql4
3dea2d1fa813        mysql:5.7           "docker-entrypoint.s…"   6 hours ago         Up 6 hours          33060/tcp, 0.0.0.0:3309->3306/tcp   mysql3
cc877d94adf8        mysql:5.7           "docker-entrypoint.s…"   6 hours ago         Up 6 hours          33060/tcp, 0.0.0.0:3308->3306/tcp   mysql2
2df642bdef21        nginx:v4            "nginx -g ‘daemon of…"   24 hours ago        Up 24 hours         0.0.0.0:8084->80/tcp                mynginx
b3f9f5a5e17b        mysql:5.7           "docker-entrypoint.s…"   25 hours ago        Up 25 hours         0.0.0.0:3306->3306/tcp, 33060/tcp   mysql1
931e0bc90580        registry:2.6.2      "/entrypoint.sh /etc…"   5 days ago          Up 27 hours         0.0.0.0:5000->5000/tcp              myregistry

或者

[root@docker-test volumes]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
e1c3a0a8f065        mysql:5.7           "docker-entrypoint.s…"   4 hours ago         Up 4 hours          33060/tcp, 0.0.0.0:3310->3306/tcp   mysql5
482757e6349e        mysql:5.7           "docker-entrypoint.s…"   5 hours ago         Up 5 hours          33060/tcp, 0.0.0.0:3301->3306/tcp   mysql4
3dea2d1fa813        mysql:5.7           "docker-entrypoint.s…"   6 hours ago         Up 6 hours          33060/tcp, 0.0.0.0:3309->3306/tcp   mysql3
cc877d94adf8        mysql:5.7           "docker-entrypoint.s…"   6 hours ago         Up 6 hours          33060/tcp, 0.0.0.0:3308->3306/tcp   mysql2
2df642bdef21        nginx:v4            "nginx -g ‘daemon of…"   24 hours ago        Up 24 hours         0.0.0.0:8084->80/tcp                mynginx
b3f9f5a5e17b        mysql:5.7           "docker-entrypoint.s…"   25 hours ago        Up 25 hours         0.0.0.0:3306->3306/tcp, 33060/tcp   mysql1

删除容器

没有停止的容器停止容器后删除。不想停止容器 rm 后添加 -f 参数

[root@docker-test volumes]# docker rm mysql5
mysql5

停止一个容器运行

[root@docker-test volumes]# docker stop mysql4
mysql4

启动一个停止的容器

[root@docker-test volumes]# docker start mysql4
mysql4

重启一个容器

[root@docker-test volumes]# docker restart mysql4
mysql4

在运行的容器中打开一个交互式终端

[root@docker-test volumes]# docker exec -it mysql4 /bin/bash
root@482757e6349e:/# ls
bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

查看容器的日志

说明

OPTIONS说明:

*   **-f : **跟踪日志输出

*   **--since :**显示某个开始时间的所有日志

*   **-t : **显示时间戳

*   **--tail :**仅列出最新N条容器日志

简单使用

[root@docker-test volumes]# docker logs mysql4
Initializing database
2019-09-16T08:56:56.527598Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-09-16T08:56:56.713272Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-09-16T08:56:56.744925Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-09-16T08:56:56.805532Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: efcae397-d85f-11e9-82de-0242ac110007.
2019-09-16T08:56:56.808888Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2019-09-16T08:56:56.809223Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
Database initialized
Initializing certificates
Generating a RSA private key
...................................................................................................................................................+++++
.......................................................+++++
unable to write ‘random state‘
writing new private key to ‘ca-key.pem‘
-----

技术图片





docker常用操作

标签:efault   isp   toc   添加   war   php   fpm   常用   files   

原文地址:https://www.cnblogs.com/Liuxz/p/11530777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!