运维之我的docker-运行你第一个镜像实例-docker容器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了运维之我的docker-运行你第一个镜像实例-docker容器相关的知识,希望对你有一定的参考价值。

介绍

     docker里面镜像在运行文件系统和参数是,它永远不会发生变更。而容器就是镜像运行时的一个实例。

当你在docker上运行一个命令时他会运行以下几个操作:

1.检查镜像是否存在

2.如果没有就下载

3.加载镜像运行你要的操作

 

查看本地容器

[[email protected] ~]# docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
centos7-ssh                                            latest              14a2c2ad7cb1        29 hours ago        320.5 MB
registry.cn-hangzhou.aliyuncs.com/forker/centos7-ssh   last                14a2c2ad7cb1        29 hours ago        320.5 MB
centos-ssh                                             latest              b21aa4e2d825        29 hours ago        320.5 MB
centos7sshv1                                           latest              c1a9c3f2f823        30 hours ago        320.5 MB
docker.com/nginxs/centos7                              last                00f6c5817350        33 hours ago        191.8 MB


查找服务端的容器

docker search [image name]

[[email protected] ~]# docker search  nginx
NAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                     Official build of Nginx.                        5079      [OK]       
jwilder/nginx-proxy       Automated Nginx reverse proxy for docker c...   915                  [OK]
richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable ...   329                  [OK]
million12/nginx-php       Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...   75                   [OK]
webdevops/php-nginx       Nginx with PHP-FPM                              65                   [OK]
maxexcloo/nginx-php       Framework container with nginx and PHP-FPM...   58                   [OK]
h3nrik/nginx-ldap         NGINX web server with LDAP/AD, SSL and pro...   33                   [OK]
bitnami/nginx             Bitnami nginx Docker Image                      22                   [OK]
evild/alpine-nginx        Minimalistic Docker image with Nginx            11                   [OK]
gists/nginx               Nginx on Alpine                                 8                    [OK]
webdevops/nginx           Nginx container                                 7                    [OK]
maxexcloo/nginx           Framework container with nginx installed.       7                    [OK]
1science/nginx            Nginx Docker images that include Consul Te...   4                    [OK]
blacklabelops/nginx       Dockerized Nginx Reverse Proxy Server.          4                    [OK]
ixbox/nginx               Nginx on Alpine Linux.                          3                    [OK]
dock0/nginx               Arch container running nginx                    2                    [OK]
servivum/nginx            Nginx Docker Image with Useful Tools            2                    [OK]
frekele/nginx             docker run --rm --name nginx -p 80:80 -p 4...   2                    [OK]
xataz/nginx               Light nginx image                               2                    [OK]
drupaldocker/nginx        NGINX for Drupal                                2                    [OK]
tozd/nginx                Dockerized nginx.                               1                    [OK]
unblibraries/nginx        Baseline non-PHP nginx container                0                    [OK]
watsco/nginx              nginx:1.11                                      0                    [OK]
c4tech/nginx              Several nginx images for web applications.      0                    [OK]
funkygibbon/nginx         nginx + openssl automated build, customisa...   0                    [OK]


 

拉取容器到本地

[[email protected] ~]#
docker pull nginx
latest: Pulling from
nginx
2c49f83e0b13: Pull
complete
4a5e6db8c069: Pull
complete
08ecf065655b: Pull
complete
ff0618bc0767: Pull
complete
12a77b8bf89a: Pull
complete
5dde53921c3f: Pull
complete
a53219dc4d2f: Pull
complete
8c7e9b6e3131: Pull
complete
f9bff7d0d06e: Pull
complete
3ac9cfbdf572: Pull
complete
491aec45eaf8: Pull
complete
cd3cf76a61ee: Pull
complete
Digest:
sha256:e2dbdc9824482b79050a67c1e6143365d0eeefcc77bf0e22cc2715d91b8d1ad4
Status: Downloaded
newer image for nginx:latest

 

 

运行容器

把容器里面的80端口映射到本机的"0.0.0.0:80"

-p         把容器内端口映射到本地 -p [本地端口]:[容器内端口]
[[email protected]
~]# docker run -p 0.0.0.0:80:80
-d nginx
7153768b14e2ab37856d63ccf2fd15c87f1c57f1f2a456b05131fe923930fce7

 

查看启动容器列表

[[email protected] ~]#
docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                   NAMES
7153768b14e2        nginx               "nginx -g ‘daemon of   7 minutes ago       Up 7 minutes        443/tcp, 0.0.0.0:81->80/tcp             modest_poincare    
b9cb57eb22f9        nginx               "nginx -g ‘daemon of   9 minutes ago       Up 9 minutes        80/tcp, 443/tcp,
0.0.0.0:80->8080/tcp  
furious_mccarthy

 

操作容器启动,关闭,重启,kill

docker {start|stop|restart|kill}  [CONTAINER ID]

[[email protected] ~]#
docker kill b9cb57eb22f9

 

 

启动一个容器并多个端口

docker run -d --name
"web-node1" -p  8022:22 -p
80:80 nginx /usr/sbin/sshd -D

 

本地数据数据映射到容器内

-d       守护进程方式运行
-v       映射数据卷  -v [本地目录]:[容器内目录]
docker run -d -it -v /root/www/:/mnt/ --name
"web-node2" -p  8024:22
-p 80:80 nginx  /usr/sbin/sshd
-D



本文出自 “nginxs小白” 博客,转载请与作者联系!

以上是关于运维之我的docker-运行你第一个镜像实例-docker容器的主要内容,如果未能解决你的问题,请参考以下文章

运维之我的docker-python sdk

运维之我的docker-compose快速创建zookeeper集群

运维之我的docker-单机多容器的编排-compose

运维之我的docker-使用ELK处理docker日志

运维之我的docker-集群的管理-swarm

运维之我的docker-docker命令参数