A Dockerfile is a special text file, with it we can build images automatically. Inside a Dockerfile we can use a set of commands to shape the image as we need.
On our last section, we learned how to execute some important commands to manage images and containers with Docker client. On this section, we will see how to automate some things.
Before we dive in a Dockerfile, let's see some steps to create our own version of the static website. Later on, we will automate the image creation in a Dockerfile. Shall we?
To build an image we are going to execute some of the commands we already know and a few new. Like I said before, don't worry too much about how difficult this can be because we are going to automate everything later on.
To create an image, first we need to choose a base, for this sample the base image will be nginx:alpine. I think I said before, but Alpine is a super light Linux distro.
Let's execute the base image on detached mode and set the container name as my_static_website
, with the command below:
docker run --name my_static_website -d nginx:alpine
Now that we have the base image running, let's change the file index.html:
docker exec -it my_static_website sh
vi /usr/share/nginx/html/index.html
Change the contents of the file to something like this:
<!DOCTYPE html>
<html>
<head><title>Welcome to my static website!<title>
<stype>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to my static website!</h1>
<p><em>Thank you for visiting my website.</em></p>
</body>
</html>
Hit i to start to change the file then ESC (maybe a few times) and :x when you're done. Press CTRL+D to exit the console and get back to our VM.
We are almost done. On the next step, we will need to commit our changes and we can do this by typing:
docker commit -m "<message>" -a "<firstname> <lastname>" my_static_website <DOCKER_HUB_USER>/my_static_website:latest
The final step is to push the modification to you Docker hub repository:
docker login --username=<DOCKER_HUB_USER>
docker push <DOCKER_HUB_USER>/my_static_website
To check your image, execute the following commands:
docker kill $(docker ps -qa)
docker rm $(docker ps -qa)
docker rmi $(docker images -qa)
docker run -p 4000:80 -d <DOCKER_HUB_USER>/my_static_website
Done. That's all we need to create an image. It was easy for you? I hope not.
On this section we learned how to create an image executing some Docker commands on the console. The steps that we followed are:
- Execute in background the base image
- Change it to your needs
- Commit the changes
- Push to Docker hub
- Which is the option to rename a container before running it?
- [A] -n
- [B] --container
- [C] -r
- [D] --name
- Which is the command used to execute a binary inside a container?
- [A] run
- [B] exec
- [C] command
- [D] ps
On the previous section we learned how to create an image by ourselves typing some commands on the terminal. Now, we are diving in the Dockerfile to automate the creation of the same image.
First, we will need to setup a few things. They are as follows:
- A directory to store the Dockerfile
- A site directory to store the index.html
- The index.html
- A Dockerfile
The format of the Dockerfile basically follow the rule below:
# comment
INSTRUCTION arguments
This is the first instruction on a Dockerfile. All Dockerfiles must start with a FROM
instruction. It will say which is the base image from which we are building.
Used to add metadata information to an image. Basically, you will need a key and a value to set as a metadata.
This is the easiest one. It will copy the contents of a folder to another one.
This instruction will tell Docker that the container listens on a port at runtime. Although, it will not make the port of the container available to access on the host, to do this we still need the -p option.
Sets the working directory for any of the instructions RUN
, CMD
, ENTRYPOINT
, COPY
and ADD
. If the argument does not exist, it will be created.
Creates a volume with a mount point with the same name.
There is a lot more instructions available on the Docker website, but for our needs this is enough. I recommend you to check the other instructions when you have a chance.
Our Dockerfile
will be similar to this one:
FROM nginx:alpine
LABEL maintainer="<firstname> <lastname> <<email>>"
WORKDIR /usr/share/nginx/html
COPY site .
EXPOSE 80
As you can see, we are saying to Docker:
- The base image with the instruction
FROM
- The maintainer information with
LABEL
- The work directory inside the image using
WORKDIR
- To copy the contents of site to work directory with
COPY
- The container listens on a port at runtime using
EXPOSE
To finally build the image, execute on the commands on the same directory as the Dockerfile
:
docker build -t my_new_static_website:latest .
To run it:
docker run -p 4000:80 -d my_new_static_website
To push to the Docker hub, now you can do like this:
docker tag my_new_static_website <DOCKER_HUB_USER>/my_new_static_website:latest
docker push <DOCKER_HUB_USER>/my_new_static_website
On this section we learned how to create an image using a Dockerfile and it's instructions.
- What is the name of the instruction to copy directory contents to an image?
- [A] CP
- [B] MOVE
- [C] COPY
- [D] LINK
- What is the name of the instruction to set the working directory inside an image?
- [A] WORKDIR
- [B] DIR
- [C] SET
- [D] WORK