Let's Discover Docker
In the previous post, we discovered how to use package managers to install applications.
Few years ago, this could have been a great option to prepare a development or a runtime environment in few seconds by scripting the commands to install all the required applications. But nowadays, we have a better solution: containers.
Containers are like a sandbox in which you can specify everything you need, by creating an image.
Each you time you will start your container, it will be reset and you will have a fresh brand new environment. This means you can do what you want in your container without the fear of breaking anything. And you know we can learn a lot by making errors, and errors again.
This is also particularly useful when you work in team: the same configuration can be used by all developers. Moreover, if you use containers in production, you will be sure that what you tested during the development phases will run in the same context in production.
In this post, we will discover how to use Docker containers, to test softwares in existing Docker images. Then, we will learn how to create our own image. So… Let’s discover Docker ! 🙂
You can either follow the installation steps from Docker web site or install it with the help your package manager.
For example, on windows, type choco install docker
.
Type docker -v
to confirm that everything is ready.
Lots of images are already available on Docker Hub. Just use the search engine to try to find if something fits to your needs. Images are now available for most of the softwares and you can easily start an environment to test them in few seconds.
For instance, I will show you how to get (pull) an image to run a container with Ubuntu.
In your terminal, just type docker pull ubuntu
to pull the image from Docker Hub.
From now, you can run a Docker container with Ubuntu just by typing the command docker run -it ubuntu bash
.
Et voilà, you are now root inside a Ubuntu Docker container. You can play without the fear of breaking anything. It’s pretty useful, for instance, when you want to learn UNIX commands or shell scripting.
However, you may want to personalize your container, installing additional softwares, apply custom configurations, … What you actually need is building your own Docker image.
As an example, we will try to create a Docker container with below criterias:
- Image based on Ubuntu
- Java must be installed
If we check the container we used previously, java was not installed. We need the same one, a little bit altered.
To build our image, we need to describe it in a file called Dockerfile. Create a folder, for example ubuntu-java-docker, then navigate inside this directory and create a file Dockerfile.
Edit this file and add the below content:
FROM ubuntu:latest
RUN apt-get update && \
apt-get install openjdk-11-jdk -y
The keyword FROM
is used to identify which image will be used as a base.
The keyword RUN
prefixes the commands you want to execute.
You can find additional keywords and information in Docker online documentation.
Now you can build your image by typing the command docker build -t ubuntu-java .
in the same directory as the Dockerfile file.
Once the image is built, you can start your container. Type docker run -it ubuntu-java bash
. As you can see, java is now available.
Thank you for reading this post.
Remember, its purpose is just to introduce Docker to someone who did not hear anything about it and not to teach you how to use it. You can do a lot more with Docker!
The reference to read is the Docker documentation that’s well explained and always up-to-date. Docker is a great tool more and more used and I think any developer should have at least tried it once.