[Linux] Test without damages with docker
Hello everybody,
today I’m goint to talkb about docker. Docker is a containerization engine, that let you to run a lightweight version of “virtual machine”. I don’t wanna talk a lot about docker, because it’s a very long and complex topic, here we’re going to use it to learn linux commands and/or create a safe test environment for your application.
Here you can find the environment
- Oracle Linux Server 7.8
- Kernel 4.14.35-1902.303.5.3.el7uek.x86_64
Installing docker
First of all, we need to install docker.
$ sudo yum install docker
$ sudo yum install docker.io
Then you need to start the docker daemon in order to use it.
$ sudo systemctl status docker
Then, if you want to use it without root permission, you need to create a docker group and add your user to that group.
$ sudo groupadd docker
$ usermod -aG docker $USER
Now, you need to logout and login and you can check if docker runs correctly.
$ docker version

Then, you can test a base image in order to check that all components works properly
$ docker run hello-world

You can see 3 different steps when you run the command:
- First of all, docker searches in the local repository the image that you need to run(red);
- Secondly, if it doesn’t find it, docker downloads it from Docker Hub (green);
- Lastly, it runs a container with that image (yellow).
Creating our test environment
Now, for example, you want to lear how Ubuntu works, how to install and remove packages, check logs, modify configuration, etc…
So, first of all we need to download the Ubuntu image. With the parameter after colon, we specify which image we prefer, checking all the version from docker hub. Now we’re going to install the latest image of Ubuntu image.
$ docker pull ubuntu:latest
Then you can run a container with that image in this way.
$ docker run -itd --name cont_ubuntu ubuntu:latest
$ docker ps
As you can see, now we have a running container named cont_ubuntu! You can connect to it with this command.
$ docker exec -it cont_ubuntu bash
As you can see, now we’re inside the container, and we can “play with”
Stop and restart
Suppose that now you need to restart your container because.
Firstly we need to stop the container, than we need to remove it, lastly we can restart it.
$ docker ps
$ docker kill [container_id]
$ docker rm [container_id]
$ docker run -itd --name cont_ubuntu ubuntu:latest
Final Thoughts
That’s it, now you can stop and start a container with latest ubuntu image, and this is could be helpful if you need to test some critical command and procedure, in this way you can test in a very safe and lightweight environment, that you can restart over and over again.
Pay attention, when you do some modifications on container, this change are NOT saved, so when you restart it, you start from the original image, without any modification.
Thats it, this is a simple way to test a lot of things, like script, command and even application, without any potential issue on the real machine
Regards
Vito