Docker入门命令

Posted 无名の辈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker入门命令相关的知识,希望对你有一定的参考价值。

Edit

Docker入门命令

# 安装镜像
sudo docker pull ubuntu:12.04

# 镜像列表
sudo docker images
# 运行镜像
sudo docker run -t -i ubuntu:12.04 /bin/bash
# 运行镜像,如果这个镜像不存在,则直接从仓库中下载下来 然后退出用exit
sudo docker run -t -i training/sinatra /bin/bash

利用Dockerfile创建镜像

# 创建文件
mkdir sinatra
cd sinatra
touch Dockerfile

编辑 Dockerfile 文件


运行文件

sudo docker build -t="ouruser/sinatra:v2" .

ADD:复制本地文件到镜像
EXPOSE:像外部开放端口
CMD:描述容器启动后运行的程序 例如

1.# put my local web site in myApp folder to /var/www
2.ADD myApp /var/www
3.# expose httpd port
4.EXPOSE 80
5.# the command to run
6.CMD ["/usr/sbin/apachectl","-D","FOREGROUND"]

从本地文件系统导入

sudo cat ubuntu-14.04 |docker import -ubuntu:14.04

docker 相关命令

# 删除
sudo docker rmi training/sinatra
# 从导出的本地文件再导入到本地镜像库
sudo docker load --input ubuntu_14.04.tar

实战案例

FROM ubuntu:13.04
MAINTAINER [email protected]
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y
# 安装 ssh、apache 和 supervisor


RUN apt-get install -y openssh-server apache2 supervisor
RUN mkdir -p /var/run/sshd
RUN mkdir -p /var/log/supervisor
# 创建ssh和supervisor服务这个农场运行所需要的目录
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# 添加supervisord配置文件,并复制配置文件到对应目录

EXPOSE 22 80
CMD ["/usr/bin/supervisord"]

# supervisor 配置文件的内容
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"
# 启动supervisor容器
sudo docker run -p 22 -p 80 -t -i test/supervisords
%23%20Docker%u5165%u95E8%u547D%u4EE4%0A%0A%60%60%60%20bash%0A%23%20%u5B89%u88C5%u955C%u50CF%0Asudo%20docker%20pull%20ubuntu%3A12.04%0A%0A%23%20%u955C%u50CF%u5217%u8868%0Asudo%20docker%20images%0A%23%20%u8FD0%u884C%u955C%u50CF%0Asudo%20docker%20run%20-t%20-i%20ubuntu%3A12.04%20/bin/bash%0A%23%20%u8FD0%u884C%u955C%u50CF%uFF0C%u5982%u679C%u8FD9%u4E2A%u955C%u50CF%u4E0D%u5B58%u5728%uFF0C%u5219%u76F4%u63A5%u4ECE%u4ED3%u5E93%u4E2D%u4E0B%u8F7D%u4E0B%u6765%20%u7136%u540E%u9000%u51FA%u7528exit%0Asudo%20docker%20run%20-t%20-i%20training/sinatra%20/bin/bash%0A%20%20%20%20%0A%60%60%60%0A%0A%23%23%20%u5229%u7528Dockerfile%u521B%u5EFA%u955C%u50CF%0A%0A%0A%60%60%60%20bash%0A%23%20%u521B%u5EFA%u6587%u4EF6%0Amkdir%20sinatra%0Acd%20sinatra%0Atouch%20Dockerfile%0A%60%60%60%0A%23%23%23%23%u7F16%u8F91%20Dockerfile%20%u6587%u4EF6%0A%0A%60%60%60%20bash%0A%0A%60%60%60%0A%23%23%23%23%u8FD0%u884C%u6587%u4EF6%0A%0A%60%60%60%20bash%0Asudo%20docker%20build%20-t%3D%22ouruser/sinatra%3Av2%22%20.%0A%60%60%60%0A%0AADD%3A%u590D%u5236%u672C%u5730%u6587%u4EF6%u5230%u955C%u50CF%0AEXPOSE%uFF1A%u50CF%u5916%u90E8%u5F00%u653E%u7AEF%u53E3%0ACMD%uFF1A%u63CF%u8FF0%u5BB9%u5668%u542F%u52A8%u540E%u8FD0%u884C%u7684%u7A0B%u5E8F%20%u4F8B%u5982%0A%0A%60%60%60%20base%0A%23%20put%20my%20local%20web%20site%20in%20myApp%20folder%20to%20/var/www%0AADD%20myApp%20/var/www%0A%23%20expose%20httpd%20port%0AEXPOSE%2080%0A%23%20the%20command%20to%20run%20%0ACMD%20%5B%22/usr/sbin/apachectl%22%2C%22-D%22%2C%22FOREGROUND%22%5D%0A%60%60%60%0A%0A%0A%23%23%23%u4ECE%u672C%u5730%u6587%u4EF6%u7CFB%u7EDF%u5BFC%u5165%0A%0A%60%60%60%20bash%0Asudo%20cat%20ubuntu-14.04%20%7Cdocker%20import%20-ubuntu%3A14.04%0A%60%60%60%0A%0A%23%23%23docker%20%u76F8%u5173%u547D%u4EE4%0A%0A%60%60%60%20bash%0A%23%20%u5220%u9664%0Asudo%20docker%20rmi%20training/sinatra%0A%23%20%u4ECE%u5BFC%u51FA%u7684%u672C%u5730%u6587%u4EF6%u518D%u5BFC%u5165%u5230%u672C%u5730%u955C%u50CF%u5E93%0Asudo%20docker%20load%20--input%20ubuntu_14.04.tar%0A%60%60%60%0A%0A%23%23%23%23%u5B9E%u6218%u6848%u4F8B%0A%0A%60%60%60%20bash%0AFROM%20ubuntu%3A13.04%0AMAINTAINER%[email protected]%0ARUN%20echo%20%22deb%20http%3A//archive.ubuntu.com/ubuntu%20precise%20main%20universe%22%20%3E%20/etc/apt/sources.list%0ARUN%20apt-get%20update%0ARUN%20apt-get%20upgrade%20-y%0A%23%20%u5B89%u88C5%20ssh%u3001apache%20%u548C%20supervisor%0A%0A%0ARUN%20apt-get%20install%20-y%20openssh-server%20apache2%20supervisor%0ARUN%20mkdir%20-p%20/var/run/sshd%0ARUN%20mkdir%20-p%20/var/log/supervisor%0A%23%20%u521B%u5EFAssh%u548Csupervisor%u670D%u52A1%u8FD9%u4E2A%u519C%u573A%u8FD0%u884C%u6240%u9700%u8981%u7684%u76EE%u5F55%0ACOPY%20supervisord.conf%20/etc/supervisor/conf.d/supervisord.conf%0A%0A%23%20%u6DFB%u52A0supervisord%u914D%u7F6E%u6587%u4EF6%uFF0C%u5E76%u590D%u5236%u914D%u7F6E%u6587%u4EF6%u5230%u5BF9%u5E94%u76EE%u5F55%0A%0AEXPOSE%2022%2080%0ACMD%20%5B%22/usr/bin/supervisord%22%5D%0A%0A%23%20supervisor%20%u914D%u7F6E%u6587%u4EF6%u7684%u5185%u5BB9%0A%5Bsupervisord%5D%0Anodaemon%3Dtrue%0A%5Bprogram%3Asshd%5D%0Acommand%3D/usr/sbin/sshd%20-D%0A%5Bprogram%3Aapache2%5D%0Acommand%3D/bin/bash%20-c%20%22source%20/etc/apache2/envvars%20%26%26%20exec%20/usr/sbin/apache2%20-DFOREGROUND%22%0A%60%60%60%0A%0A%60%60%60%20bash%0A%23%20%u542F%u52A8supervisor%u5BB9%u5668%0Asudo%20docker%20run%20-p%2022%20-p%2080%20-t%20-i%20test/supervisords%0A%60%60%60%0A%0A





以上是关于Docker入门命令的主要内容,如果未能解决你的问题,请参考以下文章

docker之入门——安装及基本命令

Docker删除报错:Error response from daemon: conflict: unable to delete 08b152afcfae (must be forced)(代码片段

Docker 基础知识-入门篇

Docker入门教程命令

Docker快速入门——Docker常用命令

Docker入门系列02