If you missed my previous article, I suggest you read it before you start this tutorial docker 2 where we will learn to master our Dockerfile unless you already have a good foundation in Docker.
Article : Docker Tutorial: Getting Started with Docker
This tutorial will start at the end of the first tutorial where we have builder our following Docker image under the very simple name of “monapache”:
FROM ubuntu:14.04 MAINTAINER Judicael paquet RUN apt-get update && \ apt-get install -y apache2
Indeed, the goal of this first tutorial was to learn the first basic commands and make a very simple first image from a Dockerfile.
Learn to master his Dockerfile
Copy files from its environment
For the craziest of us, we could write all our files live with Linux command lines with a simple RUN in our Dockerfile.
However, there is a much simpler technique and obviously we will favor it: we will copy files (for those who know Vagrant, they see perfectly what I want to talk about).
In our folder where we have our Dockerfile, we will create a docker/ folder and we will write a host-apache2.conf file like this:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/test </VirtualHost>
And we will make a small index.html file that contains only the text below:
Hello World
Now that the file is in place, we will put the copy of our two files in our Dockerfile because it is the goal of our operation:
FROM ubuntu:14.04 MAINTAINER Judicael paquet RUN apt-get update && \ apt-get install -y apache2 ADD docker/host-apache2.conf /etc/apache2/sites-enabled/000-default.conf ADD docker/index.html /var/www/test/index.html
By building your image and launching it on port 8001, you will have a Hello World that will appear in your browser by typing http: // localhost: 8001:
docker build -t monapache . docker run -d -p 8001:80 monapache /usr/sbin/apache2ctl -D FOREGROUND
You can see that the http://localhost:8000 of the first tutorial still works. You have two containers running at the same time.
A frequently asked question: what is the difference between ADD and COPY (which also exists)? In fact the function ADD is the same as COPY with the possibility in addition to being able to indicate a URL.
Launch a script while launching the container
The Dockerfile allows you to launch scripts while launching of the container as you do in your RUN to launch Apache.
So we will add this in our Dockerfile
FROM ubuntu:16.04 MAINTAINER Judicael paquet RUN apt-get update && \ apt-get install -y apache2 ADD docker/host-apache2.conf /etc/apache2/sites-enabled/000-default.conf ADD docker/index.html /var/www/test/index.html ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
By building your image and launching it on port 8002, you will always have the Hello World. The difference is that the apache launch is done directly in the Dockerfile.
docker build -t monapache . docker run -d -p 8002:80 monapache
Other useful functions
The Dockerfile offers other functions that will be useful as these:
- WORKDIR [path] which moves the cursor in a folder
- ENV [name] [value] which allows you to create environment variables that you can retrieve afterwards by making $ {name}
- VOLUME [path] creates a common mount point between the master host and the containers.
- USER [name] is used to specify the user to use
Do not hesitate to look at the official documentation which offer other less used functions but which could be useful to you later.
Let’s stop the docker run
While you have just learned how to use the docker run, I will now advise you not to use it anymore. In fact the docker run is a shortcut to make a docker create + a docker start.
Here’s how we would do to launch a new container:
docker create --name="super_apache" -p 8003:80 mon_apache docker start super_apache
If you stop your container, you can restart it with the docker start to retrieve your container in the state it was when docker stop.
Conclusion Dockerfile
I hope you will now become a real Dockerfile expert with this little tutorial on the subject. We will see in a future tutorial to use another very useful tool called the docker-compose.
3 Trackbacks / Pingbacks