Try running a container: Copy and paste this command into your terminal and then come back
docker run -dp 80:80 docker/getting-started
使用手册:https://docs.docker.com/
Settings:
Configuration: https://docs.docker.com/engine/reference/commandline/dockerd/
{ "debug": true, "experimental": false }
Docker服务器启动后:running,在终端输入docker info
MacBook-Pro ~ % docker info Client: Debug Mode: false Plugins: app: Docker Application (Docker Inc., v0.8.0) buildx: Build with BuildKit (Docker Inc., v0.3.1-tp-docker) mutagen: Synchronize files with Docker Desktop (Docker Inc., testing) Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 19.03.8 Storage Driver: overlay2 Backing Filesystem: <unknown> Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 4.19.76-linuxkit Operating System: Docker Desktop OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 1.944GiB Name: docker-desktop ID: LQJA:QBZG:XG2B:AXRT:OHJ2:NPOU:S7WD:KBAJ:PFDM:IT7J:RHZ4:LXB2 Docker Root Dir: /var/lib/docker Debug Mode: true File Descriptors: 40 Goroutines: 53 System Time: 2020-06-30T15:12:29.992202656Z EventsListeners: 4 HTTP Proxy: gateway.docker.internal:3128 HTTPS Proxy: gateway.docker.internal:3129 Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false Product License: Community Engine
Docker使用:
新建并启动容器【持久交互方式启动容器】:终端允许用户交互
Docker启动容器成功后,会自动为容器创建一个ID,这个ID就是它的主机名,可以使用这个ID对容器进行启动,重启,关闭,删除等操作
docker run -i -t centos /bin/bash
docker run -it centos /bin/bash
-t 让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,
-i 让容器的标准输入保持打开
-i -t 命令保证持久的标准输入交互式shell
在交互模式下,用户可以通过所创建的终端来输入命令,能够与容器进行交互,而不是一个运行后台服务的容器。
docker run -it centos /bin/bash Unable to find image 'centos:latest' locally latest: Pulling from library/centos 6910e5a164f7: Pulling fs layer报错:docker: error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/83/831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f/data?verify=1593533419-gH2YSE7Jbo9pn/9/BB8zuRoSmE4=: Service Unavailable. See 'docker run --help'.
使用 docker image build . 查看错误原因,国内网不支持该镜像源,手动切换为国内的镜像源,然后重启docker unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /Users/xinyuqing/Dockerfile: no such file or directory
镜像源:
https://docker.mirrors.ustc.edu.cn
修改~/.docker/daemon.json文件,加入:
{ "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"] }
重新创建docker容器,可以下载镜像文件:
MacBook-Pro ~ % docker run -it centos /bin/bash [root@97a75fe5c895 /]# 进入了容器内
MacBook-Pro ~ % docker run -it centos /bin/bash Unable to find image 'centos:latest' locally latest: Pulling from library/centos 6910e5a164f7: Downloading latest: Pulling from library/centos 6910e5a164f7: Pull complete Digest: sha256:4062bbdd1bb0801b0aa38e0f83dece70fb7a5e9bce223423a68de2d8b784b43b Status: Downloaded newer image for centos:latest [root@97a75fe5c895 /]# 进入了容器内
查看目前已经启动的docker容器:docker ps 或 docker container ls
查看所有容器:docker ps -a 或 docker container ls -a
以下没有容器被启动 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
退出容器:exit
修改默认分配的容器名称,使用--name:
docker run -it --name testenv01 centos /bin/bash
docker run -it --name testenv01 centos /bin/bash MacBook-Pro ~ % docker ps -a 可以看到Names一列新建docker的名称 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f65206cbf203 centos "/bin/bash" 23 seconds ago Up 22 seconds testenv01 97a75fe5c895 centos "/bin/bash" 4 hours ago Exited (127) 47 seconds ago eloquent_grothendieck
创建守护式容器(后台运行),如:Web服务,使用-d参数,
docker run -d centos /bin/bash -c 'while true; do echo hello world; sleep 1; done'
容器会把输出的结果STDOUT打印到宿主机器上
MacBook-Pro ~ % docker run -d centos /bin/bash -c 'while true; do echo hello world; sleep 1; done' 9ce025d85749ce9f6f78a0a9c14db5ffa3afebf7100f65d6d67fa2d450686bd5
docker logs 容器名称, 查看后台docker容器
终止容器:exit 或 ctrl+d 或 docker stop 容器名,根据状态status来判断是否启动 Up/Exited
启动/重启容器:docker start 容器名
MacBook-Pro ~ % docker start testenv01 testenv01 MacBook-Pro ~ % docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9ce025d85749 centos "/bin/bash -c 'while…" About a minute ago Exited (1) About a minute ago bold_ganguly f65206cbf203 centos "/bin/bash" 6 minutes ago Up 4 seconds testenv01 97a75fe5c895 centos "/bin/bash" 4 hours ago Exited (127) 7 minutes ago eloquent_grothendieck附着在容器上:两种方式 attach, exec(常规使用)
在使用 -d 参数时,容器启动会进入后台,某些时候需要对容器进行操作,包括使用 docker attach 或 docker exec命令
attach命令:如果从这个stdin中exit,会导致容器停止
docker run -dit testevn02docker containner lsdocker attach 容器ID , 访问容器testevn02里面的内容
exec命令:如果从这个stdin中exit,不会导致容器停止,所以推荐使用
docker exec 后面可以跟多个参数,常用参数为-i, -t
只有-i参数时,由于没有分配伪终端,界面没有linux命令提示符,但命令执行结果仍然可以返回
当 -it时,可以看到linux命令操作符
docker exec -i testenv01 /bin/bash
docker exec -it testenv01 /bin/bash
容器删除,docker rm命令
docker ps -a
docker rm testenv01
导出容器,docker export命令,导出为一个tar包,导出容器快照到本地文件
docker container ls -a
docker export testenv01 > test.tar
导入容器,docker import,再次导入到镜像中
cat test.tar | docker import -test/centos:v1.0
docker image ls
也可以通过指定URL或者某个目录来导入
docker import http://example.com/expameimage.tgz expamle/imagerepo
一般不使用docker export /import,使用docker build
端口映射,使用 -p:
docker run -p ip:hostPort:containerPort redis
有三种方式:
映射指定地址的指定端口到虚拟机的指定端口
Docker简单使用, docker exec -it e_mysql bash 进入docker
docker build -t image_db:1.0 . # 启动 docker, run the docker,命令如下:-p 映射端口号到主机的3306, 运行实例:e_mysql , -d为image的名字 image_db docker run -p 3306:3306 --name e_mysql -d image_db:1.0 # 进入docker mysql中 docker exec -it e_mysql bash mysql -h127.0.0.1 -uroot -p