1. Home
  2. Cloud and Servers
  3. Containers
  4. Docker
  5. Docker – 2. Dockerfiles and images

Docker – 2. Dockerfiles and images

In the previous part of this series we have introduced a concept and tool of project Docker and we have also installed the Docker. Remember that the basic is formed by containers. Now we will focus on configuration file Dockerfile and how it influences the image.

Hint: Docker is running perfectly on Active24 VPS. Try out the premium virtual server today!

What is Dockerfile and how to use it

Dockerfile is a configuration text file, to which we write command for automatization to build or run the image. We can for example let it install a necessary software or other necessities, to secure the stable development environment. If we create the Dockerfile, it is a good advice to place it to the directory with all the files that we want to use to build and run image from. For example with the directory called docker-test in the home directory of the logged in user (swung dash):

mkdir ~/docker-test && cd ~/docker-test
touch Dockerfile

We have created the directory called docker-test in the home directory of the logged in user (swung dash), moved in it (first line) and we created a text file called Dockerfile in the empty directory (second line).

In the operating system Windows the file name has to be without extension, which is usually added in the Windows at the end of the file after a dot (e.g. extension txt).

We create very simple file calledindex.html, that we will soon load via web server Nginx in a container:

echo "Hello from the Docker container!" > index.html

Currently in the docker-test directory there are 2 files: Dockerfile and index.html. To the Dockerfile we will now write that we want to install the web server Nginx (first line). With a command on the second line we copy the fileindex.html to the image that we will soon display via Nginx. Open the Dockerfile in a text editor nano:

nano Dockerfile

and write a simple script with two lines:

FROM nginx
COPY index.html /usr/share/nginx/html

Save the file with a shortcut Ctrl + X and then confirm by pressing y and Enter.

Command FROM and COPY are only two of the other supported. In the other parts of this series we will learn more commands.

Building and running images

Configuration file Dockerfile is ready so we can now build and run an image. To check the correctness of the script in the file Dockerfile, we will let it build an image called nginx-test (do not forget to copy the dot at the end):

docker build -t nginx-test .

Then find out if the image is built in the list of images:

docker image ls

Since we have not yet built any other images, only two images will appear. One is called nginx and second is nginx-test. The first one is the basic image that the second was built from. To the second one we have copied the index.html, so we will run this image:

docker run -d nginx-test

To see if the image called nginx-test is running and created the container with the web server Nginx, we will check with a command:

docker ps

A list of running containers is displayed. To get into the container and check if everything is working alright, we need the CONTAINER ID (12 characters from the first column – here be08a567a805, your will differ). This is how we get into the container:

docker exec -it be08a567a805 bash

We have gained access to the command line (bash) inside of the container be08a567a805 now all that’s left is to try if the Nginx server is working and if it will display the content of the file index.html:

curl localhost

After the command is executed we will see the content of the file index.html (Hello from the Docker container!), which means that our web server Nginx is working and displays the content correctly.

Running container is terminated (first line) and deleted (second line):

docker stop be08a567a805
docker rm be08a567a805

Now we can change the content of file index.html in the directory docker-test and again build and run the image called nginx-test, as described above.

So we wouldn’t need to terminate, delete and run again the program each time we edit the content of index.html we will work with the content (volume).

Summary

We have shown you how to build and run an image with a specific settings that we wrote into the text configuration file Dockerfile. We have gotten inside the container of the running image (that is to the container), to check, if all is working well.

Updated on December 18, 2024

Was this article helpful?

Related Articles