docker-compose inside Alpine container
Asked Answered
J

5

7

I'm attempting to bundle my app (code, Dockerfiles and a docker-compose script) together inside a single image for easy deployment (entrypoint will just docker-compose up). My Dockerfile for this top level app image looks like so

FROM alpine:latest
COPY . /app
RUN apk update && apk add --no-cache docker py-pip openrc && pip install docker-compose

When I run this image, it seems that dockerd hasn't started, being absent from top results; docker ps reports "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? ". If however I do service docker start, this returns " * WARNING: docker is already starting". Is there anything special I need to do to get Docker working side of an Alpine container?

Jorgan answered 15/2, 2019 at 14:34 Comment(1)
Did you install docker properly ?Confidential
B
2

Docker consists of docker daemon, who runs all services, and CLI, used to interact with daemon (ok, there are lots of other parts, but these are most important). Installing docker-compose does not install daemon, regardless of what message you are getting from service command.

To use docker inside container, you have to use docker-in-docker image. It is called docker:dind. Inherit from it (instead of alpine) and enjoy.

But before read this article, which explains why it is not a great idea and what are other ways to use docker inside container (tip: you can call host's docker daemon from inside container with just mounting /var/run/docker.sock:/var/run/docker.sock socket).

Birddog answered 15/2, 2019 at 14:49 Comment(3)
Look like it's doing the right thing but the inner containers are lacking external networking? Is there a way to pipe this down in a similar manner?Jorgan
Don't get you. Inner container's do not lack networking, they don't have daemon unless it is explicitly installed. Using /var/run/docker.sock eliminates this issue by granting them connectivity to host's container. But this is Unix socket, which is not pure networking.Birddog
Yep, it appears the problem is actually the logging for one of the internal containers/applications stopping (or being swallowed/hidden/etc.) despite the container/application continuing to run fineJorgan
C
4

If you can't or don't want to inherit from docker:dind as mentioned in grapes answer there is an option to use alpine as a base image and install Docker CLI and Docker Compose.

To keep image small some dependencies can be deleted after Docker Compose installation:

FROM alpine:3.11

RUN apk update && \
    apk add --no-cache docker-cli python3 && \
    apk add --no-cache --virtual .docker-compose-deps python3-dev libffi-dev openssl-dev gcc libc-dev make && \
    pip3 install docker-compose && \
    apk del .docker-compose-deps

This image doesn't contain Docker Engine and Docker-in-Dcoker approach has to be used by mounting /var/run/docker.sock from the host

docker run -v /var/run/docker.sock:/var/run/docker.sock <image name>
Creditable answered 8/4, 2020 at 8:52 Comment(0)
B
2

Docker consists of docker daemon, who runs all services, and CLI, used to interact with daemon (ok, there are lots of other parts, but these are most important). Installing docker-compose does not install daemon, regardless of what message you are getting from service command.

To use docker inside container, you have to use docker-in-docker image. It is called docker:dind. Inherit from it (instead of alpine) and enjoy.

But before read this article, which explains why it is not a great idea and what are other ways to use docker inside container (tip: you can call host's docker daemon from inside container with just mounting /var/run/docker.sock:/var/run/docker.sock socket).

Birddog answered 15/2, 2019 at 14:49 Comment(3)
Look like it's doing the right thing but the inner containers are lacking external networking? Is there a way to pipe this down in a similar manner?Jorgan
Don't get you. Inner container's do not lack networking, they don't have daemon unless it is explicitly installed. Using /var/run/docker.sock eliminates this issue by granting them connectivity to host's container. But this is Unix socket, which is not pure networking.Birddog
Yep, it appears the problem is actually the logging for one of the internal containers/applications stopping (or being swallowed/hidden/etc.) despite the container/application continuing to run fineJorgan
S
0

You can run this to get docker-compose

apk update && apk add --no-cache --virtual docker-cli python3 .docker-compose-deps python3-dev libffi-dev openssl-dev gcc libc-dev make python3 py3-pip py-pip curl libffi-dev openssl-dev gcc libc-dev rust cargo make

pip install docker-compose
Slunk answered 1/3, 2021 at 7:41 Comment(0)
E
0

Docker is a client-server application. It has docker client (docker binary) and docker daemon (dockerd)

You get this error because you installed only docker client, not docker daemon inside Dockerfile.

To resolve this you have several options:

  • use docker:dind image which has both docker daemon and client inside
  • map docker daemon socket from the docker host to the container with docker client
  • let docker client connect remotely to docker daemon (which may run anywhere, e.g. inside container, vm, bare-metal, etc...)

I wrote how to create docker in docker agent image and you can see the example there as well.

Exhilarant answered 8/12, 2022 at 10:59 Comment(0)
S
0

Simply download last version of docker-compose release form here then move it in /usr/bin and add it access to run, like below:

RUN apk add docker
RUN wget https://github.com/docker/compose/releases/download/v2.24.5/docker-compose-linux-x86_64
RUN mv docker-compose-linux-x86_64 /usr/bin/docker-compose
RUN chmod +x  /usr/bin/docker-compose

You need to run your container with below command:

docker run -v /var/run/docker.sock:/var/run/docker.sock {your-alpine-image}
Schoolmaster answered 2/2 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.