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

Related posts:

The following two tabs change content below.

Chandra Shekhar

Consultant at Infogain
Chandra Shekhar is a LAMP Developer,Thinker,Blogger and currently working as Magento(Ecommerce) specialist.. I am always open to learn new things in LAMP as well as other newer technology. In the journey of my last 9 years of web and product development, I have decided to share my knowledge and experience to Open Source Community in 2011. In this regard I have started CuteHits.com. Since then I am actively sharing my knowledge on various LAMP technology through CuteHits.com’s technical ... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)