简体   繁体   English

如何将带有 docker-compose 的多个 docker 图像推送到 docker 集线器?

[英]How to push multiple docker images with docker-compose to docker hub?

I have several images and services in my docker-compose file, For creating a PHP, Apache, MySql, and PHPMyAdmin environment to run my website. I have several images and services in my docker-compose file, For creating a PHP, Apache, MySql, and PHPMyAdmin environment to run my website. Can anyone help me with how can I push my all images or created an environment with my website code on Docker Hub?谁能帮助我如何在 Docker Hub 上推送我的所有图像或使用我的网站代码创建环境?

Dockerfile Dockerfile

FROM php:7.4-apache
RUN docker-php-ext-install mysqli

docker-compose.yml docker-compose.yml

services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/html/

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

  phpmyadmin:
      image: phpmyadmin
      restart: always
      ports:
        - 8080:80
      environment:
         - PMA_ARBITRARY=1

index.php and all my website code in the src folder index.php 和我在src文件夹中的所有网站代码

It's useful to understand what you can and can't push to Docker Hub.了解可以推送到 Docker 集线器的内容非常有用。 Docker Hub will contain Docker images, but it can't host data content, the docker-compose.yml file itself, or anything else that's not a Docker image. Docker Hub will contain Docker images, but it can't host data content, the docker-compose.yml file itself, or anything else that's not a Docker image. The two support containers you use both use standard Docker Hub images.您使用的两个支持容器都使用标准 Docker Hub 映像。 So you need to make your php container self-contained, push that to Docker Hub, and manually copy the docker-compose.yml file to the place you'll run it.因此,您需要使您的php容器自包含,将其推送到 Docker 集线器,然后手动将docker-compose.yml文件复制到您将运行它的地方。

Remove dependencies on the local system.删除对本地系统的依赖。 You don't generally want to copy the application source code in addition to copying Docker containers around.除了复制 Docker 容器外,您通常不想复制应用程序源代码。 In the docker-compose.yml file, remove the volumes: that mount local-system source code into the container, but replace that with a Dockerfile line that copies it into the image:docker-compose.yml文件中,删除将本地系统源代码挂载到容器中的volumes:将其替换为将其复制到映像中的 Dockerfile 行:

COPY ./src/ /var/www/html/

This won't be a live development environment, and that's okay.这不会是一个实时的开发环境,没关系。

Name your Docker Hub images.命名您的 Docker 集线器图像。 You can have both a build: and an image: line in the docker-compose.yml file.您可以在docker-compose.yml文件中同时拥有build:image:行。 If you do, that specifies the name the image will be built with (rather than letting Compose pick it).如果你这样做,它指定了构建图像的名称(而不是让 Compose 选择它)。 This should leave you with:这应该给你留下:

services:
  php:
    build: .  # with default Dockerfile
    image: myname/myapp
    ports:
      - '80:80'
    # no volumes:
  db: # as above
  phpmyadmin: # as above

Build and test.构建和测试。 You should be able to take this setup;您应该可以进行此设置; docker-compose down -v to clean up all the local artifacts; docker-compose down -v清理所有本地工件; docker-compose build the image, and docker-compose up to start it all. docker-compose build镜像,然后docker-compose up启动它。 Do whatever manual testing you need, plus any automated end-to-end tests you've built.执行您需要的任何手动测试,以及您构建的任何自动化端到端测试。

Push the image.推送图片。

docker-compose push php

Run it somewhere else.在其他地方运行它。 You need the docker-compose.yml file, but not any of the other artifacts.您需要docker-compose.yml文件,但不需要任何其他工件。 When you run docker-compose up on the other machine, it will pull any images it doesn't already have locally.当您在另一台机器上运行docker-compose up ,它会拉取本地没有的任何图像。 (If it's a private image, you may need to docker login there first.) (如果是私有镜像,可能需要docker login 。)

scp docker-compose.yml there:
ssh root@there docker-compose up -d

Database content.数据库内容。 Your docker-compose.yml file doesn't declare volumes: to store database data;您的docker-compose.yml文件没有声明volumes:存储数据库数据; this could result in data loss across restarts.这可能会导致重新启动时数据丢失。 However, you also can't push these volumes to Docker Hub.但是,您也不能将这些卷推送到 Docker 集线器。 You can back up your database on the local system and restore it on the remote system;您可以在本地系统上备份您的数据库并在远程系统上恢复它; or you can run database migration and seeding scripts as part of your application's startup.或者,您可以在应用程序启动过程中运行数据库迁移和播种脚本。 The data needs to be dealt with separately from the code (images) and deployment mechanics (Compose).数据需要与代码(图像)和部署机制(Compose)分开处理。

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

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