Written by Shon Kaganovich [email protected]
A docker is a tool that allows developers to easily deploy apps without having to custom download all their dependencies for said project. It uses a sandbox (also called a container) that contains all the dependencies and compilers needed, but unlike virtual machines contains do not have high overhead and thus are efficient.
To understand how a container works let's first examine how a Virtual Machine works.
- A VM (virtual machine) runs applications inside of a guest OS which runs on virtual hardware powered by the server's host OS. This virtual overhead is huge. On the other hand containers skip creating a second OS.
source
(a) is a VM machine and (b) is a Container
-The VM takes our operating system and builds a hypervisor on top of that. The hypervisor creates virtual hardware and then connects it to our OS to process. then it builds a guest OS of our choice on top of that. The guest OS already has the correctly configured runtime system that has all the libraries and dependencies needed to compile our project
- The container skips creating the virtual hardware. Instead it builds a container manager. this container manager utilizes a linux kernel cgroup (that is in charge of resource usage) and an api to run your system environment build. In plain english just think of the container manager as the thing that connects your dependencies straight to the hardware resources.
-I will get into more detail later but a container is generally based off of a minimal linux environment that then has added dependencies needed for your project attached. The container manager then takes the basic linux requests and translates them to your operating system hardware requests
-
we will be using the Docker container system -- there are many more out there but this one is open source and easy to use. To download. Docker essentially provides a container platform. So first you need to download docker here. Just find your operating system and follow the steps.
-
now that you have docker downloaded you can pull and push images just as if you are using git. images hold the blueprints of our app which form our container.
-
docker hub holds many containers that could already work for you. Let's say you needed a python container you would just use the terminal and write
sudo docker search search
and it will show you all the possible containers with that dependency
sudo search <image-name>
Searches for a specific existing image on the docker hub
- now let's say you found one that works for you. In our case because we want to build a simple flask server we will just use
sudo docker pull python
sudo docker pull <image_name>
Pulls the latest version of that image. If you want a specific version you can do docker pull <Image_name>:tag
the tag you can find if you search up the image name on docker hub. when you do not add a tag it automatically adds that tag: latest
.
-
sometimes you cant find an image that works for you or you need to edit one, so it helps to know how to make one -the way to build a file is to make a file named Dockerfile. The docker will read and build a docker from this -you probably dont want to start from complete scratch so instead you will start from some kind of OS
-
we will start with the ubuntu: OS and then download python for it.
FROM ubuntu
RUN apt-get update && apt-get install -y python
- in the same manner you can install whatever you need
-now you want to check that this works. To do this you will run the build command that should look like this: DO NOT FORGET THE DOT AT THE END
sudo docker build -t <user_name>/<image_name> .
- if you do not have a username go to dockerhub and create an account -assuming everything builds correctly you can then push this onto your account to share to others.
- first run the command
sudo docker login
for your docker to know who you are and then run
sudo docker push <user_name>/><image_name>
-then all people will need to do to get that dockerfile and build the container is sudo docker pull <user_name>/<image_name>
(<user_name>/><image_name> is the name of the image technically)
-so now that you have a container you probably want to develop something inside and then run it -the main point about a container is that you write code outside of the container, so write you code using your OS's text editors -to run your docker just do
sudo docker run <user_name>/><image_name>
-you will find that this does nothing.. in this case we want to at least run an interactive bash shell to do this we would run
sudo docker run -it <user_name>/><image_name> bash
-if you try going through the directories you will find that you cant find your files to fix this we will need to mount a volume denoted -v <directory>
. We will use the the current working directory so run this command
sudo docker run -v $PWD/:/home -it <user_name>/><image_name>
-
what this does is it mounts the current working directory onto the container shell
-
now most of the time you also need to export a certain port like XXXX for servers. do that you should add the option
-p 127.0.0.0:<INTERNAL_PORT>:<EXTERNAL_PORT>
- writing this all out every time can be a pain so just create a docker-run.sh file. In your text editor you should add this:
#!/bin/bash
sudo docker run \
-v $PWD/:/home/ \
-w /home/<REST_OFDIRECTORY_YOU_WANT_THE_SHELL_TO_START_INT> \
-p 127.0.0.0:<INTERNAL_PORT>:<EXTERNAL_PORT>
-it <user_name>/<image_name>
bash
-we added the -w to change the working directory
- then to make the docker-file an executable just run
chmod +x ./docker-run.sh
-good job now the docker should be running and now inside the shell execute the commands to run your server/application
- as you use more and more images you probably want to delete and add different images
-to see all the images you have downloaded just use:
sudo docker images
- when you are done developing with a certain image you can remove it with
sudo docker rmi <image_ID>
- for more information search the docker documentation.
Main info
https://docker-curriculum.com/#docker-network
The difference between a container and vm also the image
http://www.electronicdesign.com/dev-tools/what-s-difference-between-containers-and-virtual-machines