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?
Start with Containerization
- 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
FROM php:7.0-cli RUN mkdir /app WORKDIR /app COPY . /app CMD ["php", "/app/run.php"]
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
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
docker run php-image
Where php-image is the name of your container image created above
Run the container on specific port (For web)
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
To delete an image
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
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
docker ps -a