Continuing the series of basic docker exercises in this article, we will learn the docker file, practice building docker image manually and build image with docker file.
The work we do will be as follows
- Create the image from the centos base image
- Image with built-in httpd server
- htop
- vim
- Copy the data file index.html to the var / ww / html directory on the background execution container / usr / sbin / httpd When the container from this image runs, it will run the httpd web server
Create data
First of all we need to create data. Create a mycode folder containing the myimage directory, inside the myimage folder create a test.html file with basic html content
1 2 3 4 5 6 7 | pc:~/Documents$ mkdir mycode pc:~/Documents$ cd mycode/ pc:~/Documents/mycode$ mkdir myimage pc:~/Documents/mycode$ cd myimage/ pc:~/Documents/mycode/myimage$ touch test.html pc:~/Documents/mycode/myimage$ |
Edit html file
Practice building manually
Create cent containers from latest version centos images using command
docker run -it --name cent centos:latest
Next, run the command yum update
to update the centos of the container
Next, install httpd and httpd-tools using the command
yum install httpd httpd-tools -y
After the installation is complete, we can test httpd with the command httpd -v
Next we install vim yum install vim -y
To install the htop we need to install the extended distribution repository
1 2 3 4 | yum install epel-release -y yum update yum install htop |
We have finished installing httpd, htop, vim. Next, we proceed to copy the data that is created test.html file to the working directory / var / www / html on the container.
First of all, you need to get out of the running container by pressing ctrl + p, ctrl + q
- Run the
docker ps
command to check that docker is running - Copy data from host machine into the container with the command
docker cp /home/nguyen.van.thinhb/Documents/mycode/myimage/test.html cent:/var/www/html
- Go to the container to check if the data has been copied or not
We see that the test.html file is already in the container
Next we pack this container into images
- First of all, you need to exit and close the container with the
exit
command - Pack the container named cent just made up images named imagecent
So that the image has been created, now we try to create the container and run the httpd server from the image we just created with the command
docker run --rm -p 6789:80 imagecent:latest httpd -D FOREGROUND
The -rm
parameter so that when the container is off, it will also automatically delete this container
The -p 6789:80
parameter to map port 6789 on the host machine to port 80 on the container
The parameter -D FOREGROUND
to run in the background
Then access port 6789 in the browser on the host and you will see that the container’s httpd server is running
We have built our images manually, and we will do this with the docker file
Practice building with dockerfile
Create docker file
We create docker file in the directory myimage, cd into the myimage directory and create with the command touch Dockerfile
Open Dockerfile and edit the contents as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | FROM centos:latest RUN yum update -y RUN yum install httpd httpd-tools -y RUN yum install vim -y RUN yum install epel-release -y RUN yum update -y RUN yum install htop -y WORKDIR /var/www/html EXPOSE 80 ADD ./test.html /var/www/html/ ENTRYPOINT [ "httpd" ] CMD [ "-D", "FOREGROUND"] |
Explain this section:
FROM centos:latest
specifies the original image as images centos: latest
RUN commands to execute commands like update centos ..
WORKDIR installs the default working directory
EXPOSE sets up the listening port on the container
ADD to copy data from host machine into container
ENTRYPOINT is the process that runs by default when creating containers
CMD [“-D”, “FOREGROUND”] is the parameter for ENTRYPOINT
Run the docker file to generate the image
In the directory yet, run the command Dockerfile
docker build --rm -t dockerfileimage -f Dockerfile .
After running the build images we see a terminal running 12 processes corresponding to the commands we create in Dockerfile, when finished, then we check with the command images docker images
will show images dockerfileimage has been successfully created.
Create the container from the newly created image and run the httpd web server
We have created the dockerfileimage image now we create the container and run the httpd web server from this image
Run the command docker run --rm -p 6789:80 dockerfileimage
The container has been created and is running a web server, now we access the web server of the container with a browser with port 6789 and see the web server working.
End
Above is the basic content of docker file, let’s wait for the next part, thank you everyone.