简体   繁体   English

如何从 docker-compose 文件创建 DockerFile

[英]How can I create DockerFile from docker-compose file

I have an existing docker-compose file我有一个现有的 docker-compose 文件

version: '3.6'
services:

  verdaccio:
    restart: always
    image: verdaccio/verdaccio
    container_name: verdaccio
    ports:
      - 4873:4873
    volumes:
      - conf:/verdaccio/conf
      - storage:/verdaccio/storage
      - plugins:/verdaccio/plugins
    environment:
      - VERDACCIO_PROTOCOL=https

networks:
    default:
        external:
            name: registry

I would like to use an DockerFile instead of docker-compose as it will be more easy to deploy DockerFile on an Azure container registry.我想使用 DockerFile 而不是 docker-compose 因为在 Z3A580F142203677F5F0BC3089F8 容器上部署 DockerFile 会更容易。

I have tried many solution posted on blogs and others but nothing worked as I needed.我尝试了许多在博客和其他人上发布的解决方案,但没有任何我需要的解决方案。

How can I create simple DockerFile from the above docker-compose file?如何从上述 docker-compose 文件创建简单的 DockerFile 文件?

You can't.你不能。 Many of the Docker Compose options (and the equivalent docker run options) can only be set when you start a container.许多 Docker Compose 选项(和等效的docker run选项)只能在启动容器时设置。 In your example, the restart policy, published ports, mounted volumes, network configuration, and overriding the container name are all runtime-only options.在您的示例中,重新启动策略、发布的端口、已安装的卷、网络配置和覆盖容器名称都是仅运行时选项。

If you built a Docker image matching this, the most you could add in is setting that one ENV variable, and COPY ing in the configuration files and plugins rather than storing them in named volumes.如果您构建了与此匹配的 Docker 映像,则最多可以添加一个ENV变量,并在配置文件和插件中进行COPY ing,而不是将它们存储在命名卷中。 The majority of that docker-compose.yml would still be required. docker-compose.yml中的大部分仍然是必需的。

If you want to put conf , storage and plugins files/folders into image, you can just copy them:如果你想将confstorageplugins文件/文件夹放入镜像中,你可以复制它们:

FROM verdaccio/verdaccio
WORKDIR /verdaccio
COPY conf conf
COPY storage storage
COPY plugins plugins

but if you need to keep files/and folder changes, then you should keep them as it is now.但是如果您需要保留文件/和文件夹更改,那么您应该保持它们现在的样子。

docker-compose uses an existing image. docker-compose 使用现有图像。

If what you want is to create a custom image and use it with your docker-compose set up this is perfectly possible.如果您想要创建自定义图像并将其与 docker-compose 设置一起使用,这是完全可能的。

  1. create your Dockerfile - example here: https://docs.docker.com/get-started/part2/创建您的Dockerfile - 例如: https://docs.docker.com/get-started/part2/
  2. build an "image" from your Dockerfile: docker build -f /path/to/Dockerfile -t saurabh_rai/myapp:1.0 this returns an image ID something like 12abef12从您的 Dockerfile 构建一个“图像”: docker build -f /path/to/Dockerfile -t saurabh_rai/myapp:1.0这将返回一个类似于12abef12的图像 ID
  3. login to your dockerhub account ( saurabh_rai ) and create a repo for the image to be pushed to ( myapp )登录到您的 dockerhub 帐户 ( saurabh_rai ) 并为要推送到的图像创建一个存储库 ( myapp )
  4. docker push saurabh_rai/myapp:1.0 - will push your image to hub.docker.com repo for your user to the myapp repo. docker push saurabh_rai/myapp:1.0 - 将您的图像推送到集线器。docker.com 存储库供您的用户到myapp You may need to perform docker login for this to work and enter your username/password as usual at the command line.您可能需要执行docker login才能正常工作,并像往常一样在命令行输入您的用户名/密码。
  5. Update your docker-compose.yaml file to use your image saurabh_rai/myapp:1.0更新您的docker-compose.yaml文件以使用您的图像saurabh_rai/myapp:1.0

example docker-compose.yaml :例如docker-compose.yaml

version: '3.6'
services:

  verdaccio:
    restart: always
    container_name: verdaccio
    image: saurabh_rai/myapp:1.0
    ports:
      - 4873:4873
    volumes:
      - conf:/verdaccio/conf
      - storage:/verdaccio/storage
      - plugins:/verdaccio/plugins
    environment:
      - VERDACCIO_PROTOCOL=https
    network:
      - registry

I have solve this issue by using an existing verdaccio DockerFile given below.我已经通过使用下面给出的现有 verdaccio DockerFile 解决了这个问题。

FROM node:12.16.2-alpine as builder

ENV NODE_ENV=production \
    VERDACCIO_BUILD_REGISTRY=https://registry.verdaccio.org

RUN apk --no-cache add openssl ca-certificates wget && \
    apk --no-cache add g++ gcc libgcc libstdc++ linux-headers make python && \
    wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
    wget -q https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.25-r0/glibc-2.25-r0.apk && \
    apk add glibc-2.25-r0.apk

WORKDIR /opt/verdaccio-build
COPY . .

RUN yarn config set registry $VERDACCIO_BUILD_REGISTRY && \
    yarn install --production=false && \
    yarn lint && \
    yarn code:docker-build && \
    yarn cache clean && \
    yarn install --production=true



FROM node:12.16.2-alpine
LABEL maintainer="https://github.com/verdaccio/verdaccio"

ENV VERDACCIO_APPDIR=/opt/verdaccio \
    VERDACCIO_USER_NAME=verdaccio \
    VERDACCIO_USER_UID=10001 \
    VERDACCIO_PORT=4873 \
    VERDACCIO_PROTOCOL=http
ENV PATH=$VERDACCIO_APPDIR/docker-bin:$PATH \
    HOME=$VERDACCIO_APPDIR

WORKDIR $VERDACCIO_APPDIR

RUN apk --no-cache add openssl dumb-init

RUN mkdir -p /verdaccio/storage /verdaccio/plugins /verdaccio/conf

COPY --from=builder /opt/verdaccio-build .

ADD conf/docker.yaml /verdaccio/conf/config.yaml

RUN adduser -u $VERDACCIO_USER_UID -S -D -h $VERDACCIO_APPDIR -g "$VERDACCIO_USER_NAME user" -s /sbin/nologin $VERDACCIO_USER_NAME && \
    chmod -R +x $VERDACCIO_APPDIR/bin $VERDACCIO_APPDIR/docker-bin && \
    chown -R $VERDACCIO_USER_UID:root /verdaccio/storage && \
    chmod -R g=u /verdaccio/storage /etc/passwd

USER $VERDACCIO_USER_UID

EXPOSE $VERDACCIO_PORT

VOLUME /verdaccio/storage

ENTRYPOINT ["uid_entrypoint"]

CMD $VERDACCIO_APPDIR/bin/verdaccio --config /verdaccio/conf/config.yaml --listen $VERDACCIO_PROTOCOL://0.0.0.0:$VERDACCIO_PORT

By making few changes to the DockerFile I was able to build and push my docker image to azure container registry and deploy to an app service.通过对 DockerFile 进行少量更改,我能够构建 docker 映像并将其推送到 azure 容器注册表并部署到应用服务。

@Giga Kokaia, @Rob Evans, @Aman - Thank you for the suggestions it became more easy to think. @Giga Kokaia、@Rob Evans、@Aman - 感谢您提出的建议,让我们更容易思考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM