Docker is an operating-system-level virtualization makes it easier to create and deploy applications in an isolated environment. A Dockerfile is a script that contains collections of commands and instructions that will be automatically executed in sequence in the docker environment for building a new docker image.
All sysadmin or developer need a specific configuration for a proyect version, dependecies users , for old school the solution is virtual machine the principal difference whit Docker is more lightweight
I use Debian GNU Linux stretch for docker base image
Below are some dockerfile commands you must know:
FROM The base image for building a new image. This command must be on top of the dockerfile.
MAINTAINER Optional, it contains the name of the maintainer of the image.
RUN Used to execute a command during the build process of the docker image.
ADD Copy a file from the host machine to the new docker image. There is an option to use an URL for the file, docker will then download that file to the destination directory.
ENV Define an environment variable.
CMD Used for executing commands when we build a new container from the docker image.
ENTRYPOINT Define the default command that will be executed when the container is running.
WORKDIR This is directive for CMD command to be executed.
USER Set the user or UID for the container created with the image.
VOLUME Enable access/linked directory between the container and the host machine.
Now let’s stat to create our first dockerfile.
apt-get install docker
Create Dockerfile, default start.sh and supervisord.conf
Step 3 – Build New Docker Image and Create New Container Based on it
The Dockerfile and all required config files have been created, now we can build a new docker image based on Ubuntu 16.04 and our dockerfile with the docker command below:
docker build -t nginx_image .
When the command completed successfully, we can check the new image ‘nginx_image’ with the docker command below:
Then we can try to create a new container based on nginx_images. And before create new container, we can create new directory on the host machine for the webroot data.
mkdir -p /webroot
Now run the new container with command below:
docker run -d -v /webroot:/var/www/html -p 80:80 --name hakase nginx_image
Then we can check that the new container with name hakase based on ‘nginx_image’ is running:
- –name hakase nginx_image = We create a new container with the name ‘hakase’, based on docker image ‘nginx_images’.
- -p 80:80 = hakase container running on port 80 on the host machine.
- -v /webroot:/var/www/html = /webroot directory on the host machine rewrite the /var/www/html directory on the container.
The new container based on the nginx_image is running without error.
Step 4 – Testing Nginx and PHP-FPM in the Container
Try to create a new index.html file in the /webroot directory with echo:
echo '<h1>Nginx and PHP-FPM 7 inside Docker Container</h1>' > /webroot/index.html
Testing with curl command by accessing the host machine ip address.
curl 192.168.1.250 curl -I 192.168.1.250
We will see results below.
Next, test that PHP-FPM 7.0 is running by creating a new phpinfo file in the /webroot directory on the host machine.
echo '<?php phpinfo(); ?>' > /webroot/info.php
Open the web browser and type the host machine IP address:
Now you can see the output of the phpinfo file.
the new docker image ‘nginx_image’ has been successfully created, now we can create more containers based on that image.