简体   繁体   中英

How can I create DockerFile from docker-compose file

I have an existing docker-compose file

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.

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?

You can't. Many of the Docker Compose options (and the equivalent docker run options) can only be set when you start a container. 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. The majority of that docker-compose.yml would still be required.

If you want to put conf , storage and plugins files/folders into image, you can just copy them:

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.

If what you want is to create a custom image and use it with your docker-compose set up this is perfectly possible.

  1. create your Dockerfile - example here: 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
  3. login to your dockerhub account ( saurabh_rai ) and create a repo for the image to be pushed to ( 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. You may need to perform docker login for this to work and enter your username/password as usual at the command line.
  5. Update your docker-compose.yaml file to use your image saurabh_rai/myapp:1.0

example 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.

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.

@Giga Kokaia, @Rob Evans, @Aman - Thank you for the suggestions it became more easy to think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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