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
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.ymlfile 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 yourphpcontainer self-contained, push that to Docker Hub, and manually copy thedocker-compose.ymlfile 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.ymlfile, remove thevolumes:that mount local-system source code into the container, but replace that with a Dockerfile line that copies it into the image:This won't be a live development environment, and that's okay.
Name your Docker Hub images. You can have both a
build:and animage:line in thedocker-compose.ymlfile. If you do, that specifies the name the image will be built with (rather than letting Compose pick it). This should leave you with:Build and test. You should be able to take this setup;
docker-compose down -vto clean up all the local artifacts;docker-compose buildthe image, anddocker-compose upto start it all. Do whatever manual testing you need, plus any automated end-to-end tests you've built.Push the image.
Run it somewhere else. You need the
docker-compose.ymlfile, but not any of the other artifacts. When you rundocker-compose upon the other machine, it will pull any images it doesn't already have locally. (If it's a private image, you may need todocker loginthere first.)Database content. Your
docker-compose.ymlfile doesn't declarevolumes: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).