0

I am running a Dockerfile that (is supposed to) run a docker-compose.yml file for a different package, but currently returns an error.

The command

docker-compose -f /home/kasm-default-profile/Project-main/docker-compose.yml up -d --build 

fails because the Docker client cannot connect to the Docker daemon.

This is my 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

And this is the full error it returns:

> [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

The error is traced back to a python source code file, I need to use python3.8 for my project, which I have specified, but it sometimes mentions python3.10 which confuses me.

Any help would be greatly appreciated, thank you in advance.

1
  • 1
    Is docker actually running on your machine? Commented Jul 12, 2024 at 16:23

1 Answer 1

0

You cannot run this Dockerfile as you've envisioned it.

The most significant problem here is that an image doesn't persist running programs at all. When you try to RUN service docker start, that may or may not succeed – service often doesn't work in Docker at all, and the Docker daemon generally requires elevated privileges beyond what's allowed in a container – but the Docker daemon is shut down at the end of that RUN command. When you get up to the RUN docker-compose ... command, the Docker daemon isn't running any more, which is the specific error you get.

By the same token, even if you could RUN docker-compose up, this launches containers which embed processes which will again exit as soon as the RUN command completes.

This isn't a "tweak this permission" problem: this Dockerfile cannot run. You'd need to get a running Docker daemon, and then separately run the docker compose up command. Normally if you need some sort of server you'd run it in a separate container, but now you're back at a point of needing Docker and Compose in order to launch containers to run Docker and Compose.


I probably wouldn't bother with any of this machinery. You intrinsically need a Docker daemon or similar (a Kubernetes runtime, Podman, ...) to run this container, so you're not avoiding a host dependency by running a second Docker daemon in a container. Compose is widespread enough that I'd consider it a basic dependency like Docker. I'd just unpack and run this directly.

# on the host system, not in a container
unzip Project-main.zip
cd Project-main
docker compose up -d --build

If you're planning to run this elsewhere, you need to copy the docker-compose.yml file, but you should not normally need to copy the rest of the application source.


If for some reason you really want an image that's capable of launching the entire application, the Docker Hub docker image turns out to contain Compose. In principle you could build an image that started FROM docker and embedded the Compose file; for example

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"]

To run this, you'll need to bind-mount the host's Docker socket into the container. But in principle you don't need the docker-compose.yml file, since it's build into the image.

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.