dockerdocker-composedocker-registrydocker-push

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. Can anyone help me with how can I push my all images or created an environment with my website code on Docker Hub?

Dockerfile

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

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


Solution

  • It's useful to understand what you can and can't push to Docker Hub. 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. 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.

    Remove dependencies on the local system. You don't generally want to copy the application source code in addition to copying Docker containers around. 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:

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

    This won't be a live development environment, and that's okay.

    Name your Docker Hub images. You can have both a build: and an image: line in the docker-compose.yml file. If you do, that specifies the name the image will be built with (rather than letting Compose pick it). 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 build the image, and docker-compose up to start it all. 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. When you run docker-compose up on the other machine, it will pull any images it doesn't already have locally. (If it's a private image, you may need to docker login there first.)

    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; this could result in data loss across restarts. However, you also can't push these volumes to Docker Hub. 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).