Docker in practice for beginners

As a beginner of docker you might want to see, docker in action. This article will help you on this direction. Throughout this tutorial we will focus on “How to dockrize a PHP script”. I hope you have taken a look of What is Docker and its advantages. Throughout this tutorial, we will see the practical approach of docker. But before start with this article we will start with a common question a beginner might have

What is the difference between Container and Image?

Containers are the one under which images runs and Images contain the files and parameters that will be used by the containers.I mean docker image contain all the files and softwares which might required for your application and all these are put together in a package which are known as container. Which we can further use any other place as it is without any new headache.

Start with Containerization

The objective of this article is to “How to dockrize a PHP script”. In order to achieve this goal we need to follow 3 step process.

  • Create a Dockerfile : Which contain step by step guide, which needs to perform in order to setup an application
  • Build an image: Run all steps of Dockerfile and associate it with a particular (build) name
  • Run an image : Run the build image name

Dockerfile

FROM php:7.0-cli
RUN mkdir /app
WORKDIR /app
COPY . /app
CMD ["php", "/app/run.php"]

Code Analysis:
FROM keyword means download the image from docker hub.
RUN means run a command
WORKDIR means its working directory, any command will run on this directory only
COPY this will copy all the files from current place to working directory
Finally CMD means run this command php and second argument is file name

Command to build a container

Go to CMD and run the below command

docker build -t php-image .

Where php-image is the name of your container image and and . means current directory
We can push this image to dockerhub as well so that next time we can use the same, whenever required.

Command to run a container on CMD

Go to CMD and run the below command

docker run php-image

Where php-image is the name of your container image created above

Run the container on specific port (For web)

Go to CMD and run the below command

docker run --name php-image -p 85:80 -d php-image

Where 85 is the customized port and 80 is the default port.
First “php-image” is the container name of image which you are creating
The last “php-image” is the image name which you have just build above
Now go to ipconfig and see your IP use http://–YOUR-IP–:85 and your application is running there.

To see all the images

Go to CMD and run the below command

docker images

To delete an image

Go to CMD and run the below command

docker rmi –f imageid

You will get your image id by running docker images command. And find the image id of your image like php-image

To delete a container

Go to CMD and run the below command

docker container rm –containerid-

get all container id by using docker container ls–aq command. And find the container id of your image like php-image

Get all container id

Go to CMD and run the below command

docker ps -a
The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...