问题描述
我正在尝试在docker-in-docker中运行。 我在容器中运行docker。重新启动容器后,我得到了错误
docker Failed to start. In /var/log/docker.log I see the following line - Error starting daemon: pid file found,ensure docker is not running or delete /var/run/docker.pid.
Suggestion - modify run-docker.sh to delete this file before starting the docker
我认为,泊坞窗已经在运行,但是它试图再次启动它。从而导致此错误。
我的Dockerfile具有ubuntu:18.04作为基本映像。我已经在其中安装了docker。入口点是dockerd&和服务器启动命令。
CMD [ dockerd && "python3","manage.py","runserver","0.0.0.0:3608"]
请帮助
解决方法
据我所知,OP是您的应用程序,它是某种服务器,它利用另一项服务,并且您想对整个事情进行docker化。我将在下面列出可能的解决方案:
- 您可以使用docker-compose启动外部服务,构建包含服务器的映像并将两者链接在一起。
示例:
version:'3.3'
services:
your-server:
build: .
ports:
- "3608:3608"
depends_on:
- utility
utility:
image: "external_utility_image"
以上内容假设您的Dockerfile
和docker-compose.yml
在同一目录中,并且您的Dockerfile
描述了仅包含服务器的映像-不需要docker-in-docker。您的服务器将使用主机名utility
(与docker-compose.yml
中指定的服务名称相同)连接到外部实用程序。
- 您可以使Python脚本使用安装在主机和Docker SDK上的Docker。
使用nginx作为外部实用程序的示例:
Dockerfile
:
FROM python:3
WORKDIR /app
COPY ./requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY ./run.py run.py
CMD [ "/app/run.py" ]
requirements.txt
:
docker==4.2.2
run.py
:
#!/usr/local/bin/python
import docker
import urllib.request
import time
client = docker.from_env()
client.containers.run('nginx:stable-alpine',detach=True,name='service')
service = client.containers.get('service')
ip = service.attrs['NetworkSettings']['IPAddress']
print(f"Service IP: {ip}")
time.sleep(5) #Wait for service to start
with urllib.request.urlopen(f"http://{ip}") as r:
contents = r.read()
print(contents)
service.stop()
service.remove(v=True)
使用方法:
docker build --tag docker-py-test:latest .
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock docker-py-test:latest
- Docker-in-Docker-您当前的方法-不推荐
Docker-in-Docker on Ubuntu 14.04 - deprecated Official repository for Docker and Docker-in-Docker images
- 在同一容器中安装两个服务-不推荐
在这种情况下,您将服务器和外部实用程序安装在同一映像中,而不是使用包含该实用程序的预制映像。您还需要一些监督程序之王。
Official Docker documentation for running multiple processes in a single container