Running Linux On Containers: Intro To Docker
What the heck are Containers?
Containers are fun. If you want to run different processes at the same time on a single machine on a single operating system, simply spin off containers and they will work for us.
Want to run two different versions of same application without caring about system resources and operating system restrictions? Then, containers are for you!
Virtual Machines Vs Containers
Not every time our tech lead wants us to set up full virtual machines from scratch just to test a simple application. It is time consuming as hell!
Hence, we moved to better and efficient solution i.e., running apps on containers.
No requirement of setting up operating system every time. You can directly run any application in an isolated environment.
Starting Docker on Linux
-
Step 1: Start the docker daemon
sudo systemctl start docker
-
Step 2: Pull the linux image(here Ubuntu image)
docker pull ubuntu
- Step 3: Running the container on the basis of the image you have pulled,
docker run
command is powerful, gives tons of options, one can customize the resources for their containers.
Command docker run ubuntu
runs ubuntu image on container.
When you run this nothing happens because docker is configured to run single process, it will wait for the process to end and the process will end then container will also end.
As soon as this command start running, the container will start running too and when this command ends the container will also stop running.
- Step 4:
docker run -it ubuntu
runs the ubuntu docker image in interactive environment. Flag -i and -t stands for interactive and terminal access respectively for the given container.
Now you are inside an Ubuntu environment.
ls
to list the current directories.
Your ubuntu image-
-
Step 5: Exit the container by typing
exit
command -
Step 6:
docker ps
lists all the active containers(running containers).
- Step 7:
docker ps -a
lists all the previous running containers with ports, names and their ending time.
- Step 8: Run ubuntu in running in the background
docker run ubuntu tail -f /dev/null
This process keeps on running so to close it we have to kill the process.
- Step 9: Kill the process: Running docker ps outputs the container’s id, move to new terminal window and paste the id in the following command
docker kill container_id