Quickstart containers

本文介绍如何验证Docker是否安装成功,并演示如何下载预构建的Ubuntu镜像,运行交互式shell,绑定Docker到其他主机或端口等基本操作。此外还介绍了如何控制容器的启动、停止、重启及日志收集等。

This quickstart assumes you have a working installation of Docker. To verify Docker is installed, use the following command:

# Check that you have a working install
$ docker info

If you get docker: command not found or something like/var/lib/docker/repositories: permission denied you may have anincomplete Docker installation or insufficient privileges to accessDocker on your machine. With the default installation of Docker dockercommands need to be run by a user that is in the docker group or by theroot user.

Depending on your Docker system configuration, you may be requiredto preface each docker command with sudo. One way to avoid having to usesudo with the docker commands is to create a Unix group called docker andadd users that will be entering docker commands to the ‘docker’ group.

For more information about installing Docker or sudo configuration, refer tothe installation instructions for your operating system.

Download a pre-built image

# Download an ubuntu image
$ docker pull ubuntu

This will find the ubuntu image by name onDocker Huband download it from Docker Hub to a localimage cache.

Note:When the image is successfully downloaded, you see a 12 characterhash 539c0211cd76: Download complete which is theshort form of the image ID. These short image IDs are the first 12characters of the full image ID - which can be found usingdocker inspect or docker images --no-trunc=true.

Running an interactive shell

To run an interactive shell in the Ubuntu image:

$ docker run -i -t ubuntu /bin/bash       

The -i flag starts an interactive container. The -t flag creates apseudo-TTY that attaches stdin and stdout.

To detach the tty without exiting the shell, use the escape sequenceCtrl-p + Ctrl-q. The container will continue to exist in a stopped stateonce exited. To list all containers, stopped and running, use the docker ps -acommand.

Bind Docker to another host/port or a Unix socket

Warning:Changing the default docker daemon binding to aTCP port or Unix docker user group will increase your security risksby allowing non-root users to gain root access on the host. Make sureyou control access to docker. If you are bindingto a TCP port, anyone with access to that port has full Docker access;so it is not advisable on an open network.

With -H it is possible to make the Docker daemon to listen on aspecific IP and port. By default, it will listen onunix:///var/run/docker.sock to allow only local connections by theroot user. You could set it to 0.0.0.0:2375 or a specific host IPto give access to everybody, but that is not recommended becausethen it is trivial for someone to gain root access to the host where thedaemon is running.

Similarly, the Docker client can use -H to connect to a custom port.The Docker client will default to connecting to unix:///var/run/docker.sockon Linux, and tcp://127.0.0.1:2376 on Windows.

-H accepts host and port assignment in the following format:

tcp://[host]:[port][path] or unix://path

For example:

  • tcp:// -> TCP connection to 127.0.0.1 on either port 2376 when TLS encryptionis on, or port 2375 when communication is in plain text.
  • tcp://host:2375 -> TCP connection onhost:2375
  • tcp://host:2375/path -> TCP connection onhost:2375 and prepend path to all requests
  • unix://path/to/socket -> Unix socket locatedat path/to/socket

-H, when empty, will default to the same value aswhen no -H was passed in.

-H also accepts short form for TCP bindings:

`host:` or `host:port` or `:port`

Run Docker in daemon mode:

$ sudo <path to>/docker daemon -H 0.0.0.0:5555 &

Download an ubuntu image:

$ docker -H :5555 pull ubuntu

You can use multiple -H, for example, if you want to listen on bothTCP and a Unix socket

# Run docker in daemon mode
$ sudo <path to>/docker daemon -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock &
# Download an ubuntu image, use default Unix socket
$ docker pull ubuntu
# OR use the TCP port
$ docker -H tcp://127.0.0.1:2375 pull ubuntu

Starting a long-running worker process

# Start a very useful long-running process
$ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")

# Collect the output of the job so far
$ docker logs $JOB

# Kill the job
$ docker kill $JOB

Listing containers

$ docker ps # Lists only running containers
$ docker ps -a # Lists all containers

Controlling containers

# Start a new container
$ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")

# Stop the container
$ docker stop $JOB

# Start the container
$ docker start $JOB

# Restart the container
$ docker restart $JOB

# SIGKILL a container
$ docker kill $JOB

# Remove a container
$ docker stop $JOB # Container must be stopped to remove it
$ docker rm $JOB

Bind a service on a TCP port

# Bind port 4444 of this container, and tell netcat to listen on it
$ JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)

# Which public port is NATed to my container?
$ PORT=$(docker port $JOB 4444 | awk -F: '{ print $2 }')

# Connect to the public port
$ echo hello world | nc 127.0.0.1 $PORT

# Verify that the network connection worked
$ echo "Daemon received: $(docker logs $JOB)"

Committing (saving) a container state

Save your containers state to an image, so the state can bere-used.

When you commit your container, Docker only stores the diff (difference) betweenthe source image and the current state of the container’s image. To list imagesyou already have, use the docker images command.

# Commit your container to a new named image
$ docker commit <container> <some_name>

# List your images
$ docker images

You now have an image state from which you can create new instances.

Where to go next

<think>我们正在解决DockerCompose启动时出现的连接错误:`errorduringconnect:Posthttp://.%2Fpipe%2FdockerDesktopLinuxEngine:EOF`根据引用[1][3]中提到的使用`docker-machine`和`pipe`的相关内容,以及引用[2]中提到的DockerToolbox方案,我们可以推断这个问题通常发生在使用DockerforWindows时,特别是当Docker服务没有正确运行或者环境变量未正确设置时。解决方案步骤:1.**确保Docker服务正在运行**:在Windows上,DockerDesktop需要在后台运行。检查任务栏右下角是否有Docker图标,并确认它是运行的(显示为鲸鱼图标且没有警告)。如果没有运行,手动启动它。2.**尝试重启Docker服务**:右键点击任务栏的Docker图标,选择“Restart”来重启Docker。3.**使用DockerToolbox的解决方案(如果使用的是旧版)**:如果您的环境不支持DockerDesktop(比如家庭版Windows),您可能需要使用DockerToolbox(如引用[2]所述)。但注意,DockerToolbox使用VirtualBox创建虚拟机,其环境管理通过`docker-machine`命令进行。如果已经安装了DockerToolbox,请尝试重启default虚拟机(引用[3]):```docker-machine.exerestartdefault```然后设置环境变量:```docker-machine.exeenvdefault|Invoke-Expression```4.**设置环境变量(针对DockerDesktop)**:有时,环境变量未设置正确也会导致问题。在PowerShell中尝试设置环境变量:```powershell$Env:DOCKER_HOST="npipe:////./pipe/docker_engine"```5.**检查DockerCompose文件**:确保您的`docker-compose.yml`文件没有语法错误。可以运行`docker-composeconfig`来验证。6.**使用管理员权限运行命令行**:确保您以管理员身份运行命令提示符或PowerShell。7.**重置DockerDesktop**:如果上述步骤无效,尝试重置DockerDesktop:右键点击系统托盘图标->Settings->Reset->Resettofactorydefaults。注意:这将删除所有容器和镜像,请谨慎操作。8.**其他可能的解决方案**:-确保Windows的命名管道服务(NamedPipe)已启用(通常Docker会自动处理)。-检查防火墙设置,确保没有阻止Docker的通信。根据引用[1],如果使用Portainer,我们也可以通过命名管道连接,但这里我们主要解决compose的连接问题。如果问题仍然存在,请考虑更新DockerDesktop到最新版本。由于错误信息中提到了`http://.%2Fpipe%2FdockerDesktopLinuxEngine`,这通常是指向Docker引擎的管道地址。在DockerDesktop中,引擎通过一个命名管道(在Windows上是`npipe`)进行通信。因此,确保环境变量`DOCKER_HOST`指向正确的管道地址非常重要。下面给出一个在PowerShell中设置环境变量的示例:```powershell#设置DOCKER_HOST为Windows的命名管道$Env:DOCKER_HOST="npipe:////./pipe/docker_engine"#然后再次尝试运行docker-composeup-ddocker-composeup-d```如果上述方法都不奏效,请尝试卸载并重新安装DockerDesktop。注意:在DockerToolbox环境下,Docker引擎运行在名为default的Linux虚拟机中,此时DOCKER_HOST通常是TCP地址(如`tcp://192.168.99.100:2376`),而不是命名管道。因此,请根据您使用的Docker版本(DockerDesktop或DockerToolbox)来选择相应的解决方案。最后,生成相关问题供用户进一步参考。</think> ### 解决 Docker Compose 连接错误 `error during connect Post http://.%2Fpipe%2FdockerDesktopLinuxEngine EOF` 这个错误通常发生在 Docker 引擎没有正确响应请求时。以下是系统化的解决方案: #### 1. 重启 Docker 服务 ```powershell # 重启 Docker 守护进程 docker-machine.exe restart default ``` - 等待 30 秒确保服务完全启动 - 检查服务状态:`docker info`[^3] #### 2. 修复环境变量配置 ```powershell # 重新加载 Docker 环境配置 docker-machine.exe env default | Invoke-Expression # 验证管道连接 Test-NetConnection -ComputerName . -PipeName docker_engine ``` #### 3. 配置命名管道共享 1. 打开 Docker Desktop 设置 2. 进入 **Resources > File Sharing** 3. 添加管道路径:`\\.\pipe\docker_engine` 4. 点击 **Apply & Restart**[^1] #### 4. 重置 Docker 服务 ```powershell # 完全重置 Docker 环境 docker-machine.exe rm default docker-machine.exe create default --driver=hyperv ``` #### 5. 安装 Portainer 进行调试(可选) ```bash docker run -d --name portainer \ --restart always \ -p 9000:9000 \ -v \\.\pipe\docker_engine:\\.\pipe\docker_engine \ portainer/portainer ``` 访问 `http://localhost:9000` 查看容器状态[^1] #### 6. 降级使用 Docker Toolbox 如果仍无法解决: 1. [下载 Docker Toolbox](https://github.com/docker/toolbox/releases) 2. 运行安装程序 3. 启动 Docker Quickstart Terminal[^2] > **重要提示**:操作前备份所有容器:`docker save -o backup.tar <image_name>`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值