Docker Containers Crash Course

Docker Crash Course

As Hypervisor is slow to boot and use a lot of resources and needs full installation

the Container Technology not that old we used to use LXC – openVZ extra

but what a cool about Docker is it really lightweight with awesome images build  and we can ship many services in one machine

it comes into two parts [DockerClient,  DockerServer]

and today i will write the best quick intro i could tell

1 – Introduction [how it will work]

docker run the process inside the container and when it has done it EXIT the container ( by EXIT I mean it STOP the container )

docker has official images [distros] called  saved in ( registry ) there is public registry also you can have a private registry, example hub.docker.com

every container you run get id example 915c72318028

2- installation

you can get docker to install from https://get.docker.com

we did download the sh install filer remember to user CAPITAL -O

we run the docker setup via root account

to verify Docker Installation use

as you can there is 2 versions for the client and server

3- Basic Usage

to be able to run container First you have to decide what image you want to use

so lets pickup ubuntu for example

so we can search the public hub to find ubuntu by

 

there is a list of Ubuntu images but the most important is the OFFICIAL images ( the one we can trust )

FYI: you and other people can make and upload images easily

so let’s RUN our first ubuntu container via docker

for the first time docker will pull images from the hub and save it locally in your machine for faster boot 😀

to list the current images

$ docker images

 

and here we go.

yes as you see EXACTLY the command passed successfully,

so where is the container!

you can list running containers via docker ps

as you can see it not listed because ps is for running containers only!

the answer for the next question, simple yes your container finished work and stopped as we mentioned in the introduction

Docker will keep your container running as long as you keep it busy ( this is the cool thing about Docker and Resource Save )

so let’s list all containers even the stopped one

 

the status of the container is Exited (0)

so next time we need run container with a command to see it working

and this means we run 2 containers and the status of them stopped

as you can see the container started in less than a second

jump in container shell

but this time we will add 2 parameters to run -i -t
-i, –interactive               Keep STDIN open even if not attached

-t, –tty                       Allocate a pseudo-TTY

this both to keep us connected to a container

 

docker in detached mode

no we have the container up and running

 

using port forward with docker in detached mode

docker come with awesome port mapping, for example, we want to run nginx container and by

image default it listens to 80,443

there is 2 port forwards here

0.0.0.0:32775->80/tcp, 0.0.0.0:32774->443/tcp

localhost:32775 to nginx container:80

localhost:32774 to nginx container:443/tcp

4- Building Docker Images

building a docker image is like create an ( OS Template ) for reuse, you don’t have to setup all your dependencies each time you decide to run container, you build image once and you use it as mass production.

Docker Images

first, we need to understand how is images looks like

images

images come with repository and tag

each repository can have many apps

for example:

privterepo/webserver

privterepo/db

privterepo/logs

and each app could have a tag name example

privterepo/webserver:2.5.1

privterepo/webserver:2.6.1

Docker Commit ( save container changes as a template ) 

so we need to install some packages inside one of our containers let’s say we want to install python Django

and all our app dependencies  once you did finish with installing your packages

let’s save the status of this container as image

first, we need  3 things

1 – Container ID

2- Registry Name  ( you can write anything you want depends if you will upload it or not )

3- modification name

4- Tag name ( Version )

 

let’s assume we build a container for development and we installed our needed  libs

we will use the container id to commit it docker commit 505815e27433 private/app:1.0

as you can see in our images there is a new one with id: 3c7881fe969f

also, you can create an image without specifying a version

and an image will be versioned as the latest 

so we can use our new image to build as many as containers on it 

keep in mind you can make multi commits for the same modification you made in a container

 

running Docker container from a self-made image

or use the latest version ( the default tag name )

 

Destroy  all containers

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.