Docker学习笔记1

时间:2023-03-09 07:06:54
Docker学习笔记1

来源:第一本Docker书

基础命令

安装:

apt-get install docker

启动:

service  docker start

创建并运行第一个容器:

docker run -i -t ubuntu /bin/bash

-i -t :提供交互的shell

查看启动的容器、所有容器:

docker ps
docker ps -a

重命名:

ps -a 可以看到我们的容器有个随机的名字cranky_lalande

efd4d3fff333        ubuntu:latest                                                       "/bin/bash"            15 minutes ago      Up 7 seconds                                      cranky_lalande          
我们可以指定自己的名字:

docker run --name ubuntu_docker -i -t ubuntu /bin/bash

启动指定容器:

docker start ubuntu_docker

也可以根据id启动:

docker start efd4d3fff333

给后台容器添加会话(回车进入):

docker attach ubuntu_docker

守护式容器:

docker run --name daemon_docker -d ubuntu /bin/sh -c "while true;do echo hello docker;sleep 1;done;"

-d:后台运行

查看后台容器在干嘛:

docker logs ubuntu_docker

查看容器的进程:

docker top daemon_docker

停止守护式容器:

docker stop daemon_docker

自动重启容器:

 docker run --restart=always --name daemon_docker_restart -d ubuntu /bin/sh -c "while true;do echo hello docker;sleep 1;done;"

无论容器的退出代码是什么都会自动重启,还可以指定:--restart=on-failure:5,当退出代码非0时重启,最多5次。

删除容器:

docker rm daemon_docker_restart

删除所有(我的报错,不能删除多个):

docker rm 'docker ps -a -q'

-a:列出所有

-q:只显示id

使用镜像

列出镜像:

docker images

注册docker hub (lanqiexh/k***1)

用户仓库:jone/ubunntu

顶层仓库:ubuntu

拉取镜像:

docker  pull xx

查找镜像:

docker search kalilinux

构建镜像:

docker commit eaa1a6e0b320 lanqiexh/u_apache2

dockerfile

mkdir DockerLearn
vim Dockerfile #Version 0.0.1
From lanqiexh/u_apache2
MAINTAINER James Turn
RUN apt-get update
RUN apt-get install nginx -y
RUN echo 'Hi, I am in your container' > /usr/share/nginx/html/index.html
EXPOSE 80

最后要(空格和点)

docker build -t aaa/u_apache2 .

-t: 仓库/名称

还可以加标签:

aaa/u_apache2:v1

从新镜像启动容器:

docker run -d -p 80 --name web aaa/u_apache2 nginx -g "daemon off;"

-p: 指定80端口映射到宿主主机,

查看启动的容器:

docker ps -l

341fc8d3ca4c        aaa/u_apache2:latest   "nginx -g 'daemon of   6 minutes ago       Up 5 minutes        0.0.0.0:32769->80/tcp   web_u

宿主主机访问:http://0.0.0.0:32769/

可以访问到容器的服务

当然也可以指定宿主主机的端口:-p 8080:80

dockerfile指令