Understanding the basics of Docker
Understanding the basics of Docker
Collaboration between developers has become easier with Docker, it eliminates repetitive tasks while setting up and configuring development environments.
In this post I will be explaining the fundamentals of docker and basic usage.
Docker
Docker is a platform to build, ship and run any application as a light weight isolated container. The container contains everything required to run a piece of software. Collaborators on a software don’t need worry about the complexities faced when setting up.The complexity is pushed into containers that are easy to build, deploy and run.
Containers , Images and Dockerfile
It is quite often to mistake the definitions of images and containers when using docker.An image in docker is an executable package that contains everything needed to run a software. While a container is a runtime instance of an image. The image can contain codes, config and environment variables. Once an image is executed, a light weight isolated container is created.
Docker can build images automatically by reading instructions from a file which is a dockerfile. Think of a dockerfile as a list of instructions needed to create an image.You can read more about dockerfile here
Run your very first docker image
For this exercise we are going to be working with an official image before we go on to create ours. Docker has two types of images, official and contributed you can read more about docker images here.
To begin let us execute the command below to run the Nginx image:
$ docker run nginx:latest echo “Container Executed”
Unable to find image ‘nginx:latest’ locally
latest: Pulling from library/nginx
ff3d52d8f55f: Pulling fs layer
226f4ec56ba3: Pulling fs layer
ff3d52d8f55f: Downloading 245 kB/23.2ff3d52d8f55f: Downloading 490.7 kB/23.28 MB
226f4ec56ba3: Downloading 458 kB/21.5ff3d52d8f55f: Downloading 736.5 kB/23.2ff3d52d8f55f: Pull complete
226f4ec56ba3: Pull complete
53d7dd52b97d: Pull complete
Digest: sha256:41ad9967ea448d7c2b203c699b429abe1ed5af331cd92533900c6d77490e0268
Status: Downloaded newer image for nginx:latest
Container terminated
docker run
— command to run an image, docker looks for nginx:latest
image locally and if it does not exist it pulls the image. So next time if I try to run this image again it will only print out Container Executed
We can keeps containers alive without them terminated after an image has been downloaded by adding the -it flag. This automatically takes us to the root of the container
$ docker run -it nginx:latest bash
root@a6e49c782178:/# ls
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
root@a6e49c782178:/#
I Will be covering creating and publishing a docker image in the next tutorial.
References
https://medium.com/@claudiopro/first-steps-with-docker-95bd08090625
$9.99 Getting started with microservices [From zero to production] Flash Sale 🌟
https://www.udemy.com/getting-started-with-microservices-from-zero-to-production/?couponCode=NEW2018DISCOUNT
I love Docker, although my biggest struggle with it is the volumes and permission concept. For example, a common scenario:
A volume with a Nodejs app code with npm (or yarn) as package manager. The most natural way of installing the packages is by running npm install in the container. The problem is that if you do that all the new files will belong to root so you won’t be able to access them from the host machine.
I hope Docker team finds a nice way to solve this.