Docker: A primer for PHP Developers

               A Docker image is a read-only template that contains instructions for creating a Docker container.

               By analogy, a Docker image is what a class is in PHP.

               Similarly, a Docker container is what an instance of a class is in PHP.

               1. Building an image using a Dockerfile:

#builds an image using the Dockerfile in the same directory where the command was executed (with "latest" tag and "v2" tag)

$ docker build -t myimage:latest
$ docker build -t myimage:v2

#builds an image in a remote repository using a Dockerfile located in a specific directory

$ docker build -t [username/]< image-name >[:tag] < dockerfile-path >
$ docker build -t kbarut/myimage:v1 -f docker/app/Dockerfile

               2. Checking the history of an image:

#checks the history of an image in a remote repository with no image tags specified

$ docker history kbarut/myimage

#checks the history of an image in a remote repository for a specific image tag

$ docker history [username/]< image-name >[:tag]
$ docker history kbarut/myimage:v2

               3. Listing images:

#lists images in local repository

$ docker images

#lists all images in remote repository

$ docker image ls myrepository

#lists all images in remote repository for a specific image tag

$ docker image ls myrepository:v1

               4. Removing images:

#removes an image from the local registry using the image name

$ docker rmi myimage

#removes an image from the local registry using the image name and image tag

$ docker rmi myimage:v1

#removes an image from the local registry using the image id

$ docker rmi 789539dd8bd

#removes all untagged Docker images

$ docker rmi $(docker images -f "dangling=true" -q)

#removes an image from remote repository with image tag

$ docker rmi [username/]< image-name >[:tag]
$ docker rmi kbarut/myimage:v1

#removes all images

$ docker rmi $(docker images -a -q)

               5. Tagging an image:

#creates an image called "new_tagged_image" with tag "v1" for the image kbarut/old_tagged_image with latest tag

$ docker tag [username/]/< image-name >[:tag]
$ docker tag kbarut/old_tagged_image new_tagged_image:v1

#creates a new image with the latest tag

$ docker tag < image-name > < new-image-name >
$ docker tag latest_tag_myimage myimage

#creates a new image specifying the "new tag" from an existing image and existing tag

$ docker tag [username/]< image-name >[:tag] < new-image-name >[:new-tag]
$ docker tag kbarut/myimage:v1 newimage:v2

               6. Pushing an image to a registry:

$ docker push [source_image] [registry]/[repository]:[tag]
$ docker push my_image docker.io/my_repository:v1.0

Kâmi Barut-Wanayo © 2024 - 2025