My First Dockerfile
Everything we've done so far has been writing on the command line, which is fine for learning. However, especially nowadays, we can't live anymore without automating things -- if you, like us, love to automate everything that's possible, you'll really enjoy this topic.
The dockerfile is nothing more than a file where you determine all the details of your container, such as, for example, the image you're going to use, applications that need to be installed, commands to be executed, the volumes that will be mounted, etc., etc., etc.!
It's a makefile for creating containers, and in it you pass all the instructions for creating your container.
Let's see how this works in practice?
First thing: let's create a directory where we'll place our dockerfile, just to keep it organized. :D
Then simply create the dockerfile as shown in the following example:
# mkdir /root/first_dockerfile
# cd /root/first_dockerfile
# vim Dockerfile
Let's add the instructions we want for this container image that we're going to create:
FROM debian
RUN /bin/echo "HELLO DOCKER"
Just that for now. Save and exit vim.
Now let's run the "docker build" command to create this container image using the created dockerfile.
root@linuxtips:~/first_dockerfile# docker build -t tosko:1.0 .
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM debian
latest: Pulling from library/debian
fdd5d7827f33: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:e7d38b3517548a1c71e41bffe9c8ae6d6d29546ce46bf62159837aad072c90aa
Status: Downloaded newer image for debian:latest
---> f50f9524513f
Step 2/2 : RUN /bin/echo "HELLO DOCKER"
---> Running in df60a0644bed
HELLO DOCKER
---> fd3af97a8940
Removing intermediate container df60a0644bed
Successfully built fd3af97a8940
Successfully tagged tosko:1.0
root@linuxtips:~/first_dockerfile#
Note that we used the current directory, represented by the "." character, to indicate the path of my dockerfile file, but you don't necessarily need to be in the same directory, just pass the path of the directory where the file is located.
Just remember that it's the directory path and not the file path.