我正在运行一个 Dockerfile,它(应该)为不同的包运行 docker-compose.yml 文件,但当前返回错误。
命令
docker-compose -f /home/kasm-default-profile/Project-main/docker-compose.yml up -d --build
失败,因为 Docker 客户端无法连接到 Docker 守护进程。
这是我的 Dockerfile:
FROM kasmweb/core-ubuntu-jammy:1.15.0
USER root
ENV HOME /home/kasm-default-profile
ENV STARTUPDIR /dockerstartup
ENV INST_SCRIPTS $STARTUPDIR/install
WORKDIR $HOME
######### Customize Container Here ###########
#get updates, install python 3.8, pip, unzip and docker's prerequisites && systemd installed#
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install software-properties-common
RUN apt-add-repository ppa:deadsnakes/ppa -y
RUN apt-get update
RUN apt-get install -y python3.10 python3-pip ca-certificates curl unzip systemd
RUN install -m 0755 -d /etc/apt/keyrings
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
RUN chmod a+r /etc/apt/keyrings/docker.asc
#run the command to add the repo to the Apt sources#
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
RUN sudo apt-get update
#installs docker#
RUN sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
RUN sudo apt-get update
RUN sudo service docker start
RUN sudo /etc/init.d/docker start
#takes the Project zip, unzips it in container and runs docker compose#
COPY ./Project-main.zip /home/kasm-default-profile/
RUN unzip /home/kasm-default-profile/Project-main.zip
RUN rm /home/kasm-default-profile/Project-main.zip
RUN sudo apt-get install docker-compose -y
RUN docker-compose -f /home/kasm-default-profile/Project-main/docker-compose.yml up -d --build
######### End Customizations ###########
RUN chown 1000:0 $HOME
RUN $STARTUPDIR/set_user_permission.sh $HOME
ENV HOME /home/kasm-user
WORKDIR $HOME
RUN mkdir -p $HOME && chown -R 1000:0 $HOME
USER 1000
这是它返回的完整错误:
> [22/29] RUN docker-compose -f /home/kasm-default-profile/Project-main/docker-compose.yml up -d --build:
0.525 Traceback (most recent call last):
0.525 File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 700, in urlopen
0.525 httplib_response = self._make_request(
0.525 File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 395, in _make_request
0.525 conn.request(method, url, **httplib_request_kw)
0.525 File "/usr/lib/python3.10/http/client.py", line 1283, in request
0.525 self._send_request(method, url, body, headers, encode_chunked)
0.525 File "/usr/lib/python3.10/http/client.py", line 1329, in _send_request
0.525 self.endheaders(body, encode_chunked=encode_chunked)
0.525 File "/usr/lib/python3.10/http/client.py", line 1278, in endheaders
0.525 self._send_output(message_body, encode_chunked=encode_chunked)
0.525 File "/usr/lib/python3.10/http/client.py", line 1038, in _send_output
0.525 self.send(msg)
0.525 File "/usr/lib/python3.10/http/client.py", line 976, in send
0.526 self.connect()
0.526 File "/usr/lib/python3/dist-packages/docker/transport/unixconn.py", line 30, in connect
0.526 sock.connect(self.unix_socket)
0.526 FileNotFoundError: [Errno 2] No such file or directory
0.526
0.526 During handling of the above exception, another exception occurred:
0.526
0.526 Traceback (most recent call last):
0.526 File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
0.526 resp = conn.urlopen(
0.526 File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 756, in urlopen
0.526 retries = retries.increment(
0.526 File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 532, in increment
0.526 raise six.reraise(type(error), error, _stacktrace)
0.526 File "/usr/lib/python3/dist-packages/six.py", line 718, in reraise
0.526 raise value.with_traceback(tb)
0.526 File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 700, in urlopen
0.526 httplib_response = self._make_request(
0.526 File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 395, in _make_request
0.527 conn.request(method, url, **httplib_request_kw)
0.527 File "/usr/lib/python3.10/http/client.py", line 1283, in request
0.527 self._send_request(method, url, body, headers, encode_chunked)
0.527 File "/usr/lib/python3.10/http/client.py", line 1329, in _send_request
0.527 self.endheaders(body, encode_chunked=encode_chunked)
0.527 File "/usr/lib/python3.10/http/client.py", line 1278, in endheaders
0.527 self._send_output(message_body, encode_chunked=encode_chunked)
0.527 File "/usr/lib/python3.10/http/client.py", line 1038, in _send_output
0.528 self.send(msg)
0.528 File "/usr/lib/python3.10/http/client.py", line 976, in send
0.528 self.connect()
0.528 File "/usr/lib/python3/dist-packages/docker/transport/unixconn.py", line 30, in connect
0.528 sock.connect(self.unix_socket)
0.528 urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
0.528
0.528 During handling of the above exception, another exception occurred:
0.528
0.528 Traceback (most recent call last):
0.528 File "/usr/lib/python3/dist-packages/docker/api/client.py", line 214, in _retrieve_server_version
0.528 return self.version(api_version=False)["ApiVersion"]
0.528 File "/usr/lib/python3/dist-packages/docker/api/daemon.py", line 181, in version
0.528 return self._result(self._get(url), json=True)
0.528 File "/usr/lib/python3/dist-packages/docker/utils/decorators.py", line 46, in inner
0.528 return f(self, *args, **kwargs)
0.528 File "/usr/lib/python3/dist-packages/docker/api/client.py", line 237, in _get
0.528 return self.get(url, **self._set_request_timeout(kwargs))
0.528 File "/usr/lib/python3/dist-packages/requests/sessions.py", line 557, in get
0.528 return self.request('GET', url, **kwargs)
0.528 File "/usr/lib/python3/dist-packages/requests/sessions.py", line 544, in request
0.529 resp = self.send(prep, **send_kwargs)
0.529 File "/usr/lib/python3/dist-packages/requests/sessions.py", line 657, in send
0.529 r = adapter.send(request, **kwargs)
0.529 File "/usr/lib/python3/dist-packages/requests/adapters.py", line 498, in send
0.529 raise ConnectionError(err, request=request)
0.529 requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
0.529
0.529 During handling of the above exception, another exception occurred:
0.529
0.529 Traceback (most recent call last):
0.529 File "/usr/bin/docker-compose", line 33, in <module>
0.529 sys.exit(load_entry_point('docker-compose==1.29.2', 'console_scripts', 'docker-compose')())
0.529 File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 81, in main
0.529 command_func()
0.529 File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 200, in perform_command
0.529 project = project_from_options('.', options)
0.529 File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 60, in project_from_options
0.529 return get_project(
0.529 File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 152, in get_project
0.529 client = get_client(
0.529 File "/usr/lib/python3/dist-packages/compose/cli/docker_client.py", line 41, in get_client
0.529 client = docker_client(
0.529 File "/usr/lib/python3/dist-packages/compose/cli/docker_client.py", line 170, in docker_client
0.530 client = APIClient(use_ssh_client=not use_paramiko_ssh, **kwargs)
0.530 File "/usr/lib/python3/dist-packages/docker/api/client.py", line 197, in __init__
0.530 self._version = self._retrieve_server_version()
0.530 File "/usr/lib/python3/dist-packages/docker/api/client.py", line 221, in _retrieve_server_version
0.530 raise DockerException(
0.530 docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
------
Dockerfile:44
--------------------
42 |
43 | RUN sudo apt-get install docker-compose -y
44 | >>> RUN docker-compose -f /home/kasm-default-profile/Project-main/docker-compose.yml up -d --build
--------------------
ERROR: failed to solve: process "/bin/sh -c docker-compose -f /home/kasm-default-profile/Project-main/docker-compose.yml up -d --build" did not complete successfully: exit code: 1
错误追溯到python源代码文件,我的项目需要使用python3.8,我已经指定了,但它有时会提到python3.10,这让我很困惑。
任何帮助将不胜感激,提前谢谢您。
您无法按照您的设想运行此 Dockerfile。
这里最重要的问题是图像根本无法持续运行程序。 当您尝试
RUN service docker start
时,这可能会成功,也可能不会成功 – service
通常在 Docker 中根本不起作用,并且 Docker 守护进程通常需要超出容器中允许的权限提升权限 – 但 Docker 守护进程已关闭在 RUN
命令的末尾。 当您执行 RUN docker-compose ...
命令时,Docker 守护进程不再运行,这是您收到的具体错误。
同样,即使您可以
RUN docker-compose up
,这也会启动嵌入进程的容器,一旦 RUN
命令完成,这些进程将再次退出。
这不是“调整此权限”问题:此 Dockerfile 无法运行。 您需要获得一个正在运行的 Docker 守护进程,然后单独运行
docker compose up
命令。 通常,如果您需要某种服务器,您会在单独的容器中运行它,但现在您又回到了需要 Docker 和 Compose 的境地,以便启动容器来运行 Docker 和 Compose。
我可能不会打扰任何这些机器。 您本质上需要一个 Docker 守护进程或类似的守护进程(Kubernetes 运行时、Podman 等)来运行此容器,因此您无法通过在容器中运行第二个 Docker 守护进程来避免主机依赖性。 Compose 已经足够广泛,我认为它是像 Docker 一样的基本依赖项。 我只是解压并直接运行它。
# on the host system, not in a container
unzip Project-main.zip
cd Project-main
docker compose up -d --build
如果您计划在其他地方运行此程序,则需要复制
docker-compose.yml
文件,但通常不需要复制应用程序源的其余部分。
如果出于某种原因您确实想要一个能够启动整个应用程序的镜像,那么Docker Hub
docker
镜像会包含 Compose。 原则上,您可以构建一个以 FROM docker
开头并嵌入 Compose 文件的映像;例如
FROM ubuntu AS unpack
WORKDIR /app
COPY Project-main.zip ./
RUN unzip Project-main.zip
FROM docker
COPY --from=unpack /app/Project-main /app
WORKDIR /app
ENTRYPOINT ["docker", "compose"]
CMD ["up", "-d"]
要运行它,您需要将主机的 Docker 套接字绑定挂载到容器中。 但原则上您不需要
docker-compose.yml
文件,因为它已构建到图像中。
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
project-compose
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
project-compose down