Docker

Docker container Vs Virtual Machine



Docker Architecture:



Workflow:




Basic Concepts

- Registry

- Repository

- Tag 

- Image

- Containers.


Registry/ Repository: https://hub.docker.com is a public registry and it contains many repositories.

For Eg: https://hub.docker.com/r/in28min/todo-rest-api-h2 (Highlighted one is the repository).


Tags:

In the repository, there will a different versions of an image. 

    For Eg:     docker run in28min/todo-rest-api-h2:1.0.0.RELEASE


Image: A static template - A set of bytes

Container: Running version of your image

==========================================================================

Install dockerhttps://docs.docker.com/desktop/ 

==========================================================================


Docker commands

Run a docker image :

    docker run -p 5000:5000 in28min/todo-rest-api-h2:1.0.0.RELEASE

        -p 5000:5000 => {host port} : {container port}

if the image is not available in local, it will pull from repository

Detached mode (-d): 

To run the container in the background. The below command will return the container id.

    docker run -p 5000:5000 -d in28min/todo-rest-api-h2:1.0.0.RELEASE

To check the logs of the container running in the background (-f is to tail the log), 

    docker logs <container-id>

    docker logs -f <container-id>     

list the running Containers:

    docker container ls

list all the containers: (Both running & Exited)

    docker container ls -a

    

    Image related commands

List docker images: 

    docker images

Stop the container:

    docker container stop <Container id>

Adding Tag:

     docker tag <image>:tag1  <image>:tag2

    Eg: docker tag in28min/todo-rest-api-h2:1.0.0.RELEASE  in28min/todo-rest-api-h2:latest

Pull image:

Just to download the image from the registry. it won't run. 

    docker pull <image>:tag name

   Eg: docker pull mysql

- If the tag name is not provided, it will pull the latest.

Search images from repo:

    docker search mysql

- Make sure you are downloading official images

Image History:

This will show all the steps involved in creating docker image

    docker image history <image Id>

inspect image:

    it will show the tags, env variable, container config, entry point and etc

    docker image inspect <image id>

Remove image:

    docker image remove <image id>

- removes the image from local, not from the repository.

Container related commands

Run a Container:

    docker container run -p 5000:5000 -d  <image>: <tag>

Pause/Un pause a container:

    docker container pause <Container id>

    docker container unpause <Container id>

Inspect container:

    docker container inspect <container id>

remove stopped container:

    docker container prune 

Stop container: - graceful shuddown

    docker container stop <container id>

Restart Container:

    docker restart <container id>

kill a container: - Immediately terminates the process

    docker container kill <container id>

Restart Policy:

    docker run -p 5000:5000 -d --restart=always in28min/todo-rest-api-h2:1.0.0.RELEASE

Events:

    docker events

Top Process:

    docker top <container id>

Stats: 

- to see CPU/MEM usage and etc

    docker stats

Docker run with CPU and MEM value:

    docker run -p 5000:5000 -d  -m 512m --cpu-quota 50000 in28min/todo-rest-api-h2:1.0.0.RELEASE

system df:

- to view what are the resources managed by the docker deamon.

    docker system df


To go into the container:

docker exec -it <containerid> bash


 Creating a Docker File

Step 1 − Create a file called Docker File and edit it using vi editors. Please note that the name of the file has to be "Dockerfile" with "D" as capital.

Step 2 − Build your Docker File using the following instructions.

#This is a sample Image 
FROM ubuntu 
MAINTAINER test@gmail.com 

RUN apt-get update 
RUN apt-get install –y nginx 
CMD [“echo”,”Image created”] 

- The first line "#This is a sample Image" is a comment

The next line has to start with the FROM keyword. It tells docker, from which base image you want to base your image from. In our example, we are creating an image from the ubuntu image.

MAINTAINER keyword and just mention the email ID

The RUN command is used to run instructions against the image. In our case, we first update our Ubuntu system and then install the nginx server on our ubuntu image.

- The last command is used to display a message to the user.


Build the Docker File:

docker build 
docker build  -t ImageName:TagName dir
  • -t − is to mention a tag to the image

  • ImageName − This is the name you want to give to your image.

  • TagName − This is the tag you want to give to your image.

  • Dir − The directory where the Docker File is present.


Creating Docker-Compose File


Docker Compose  :

Docker compose is used to run multiple containers as a single service. For example, suppose you had an application which required NGNIX and MySQL, you could create one file which would start both the containers as a service without the need to start each one separately.

sudo vim docker-compose.yml 


  • The database and web keyword are used to define two separate services. One will be running our mysql database and the other will be our nginx web server.

  • The image keyword is used to specify the image from dockerhub for our mysql and nginx containers

  • For the database, we are using the ports keyword to mention the ports that need to be exposed for mysql.

  • And then, we also specify the environment variables for mysql which are required to run mysql.


Run a docker compose file:
sudo ./docker-compose up 
This command will take the docker-compose.yml file in your local directory and start building the containers.

Once executed, all the images will start downloading and the containers will start automatically.


Other Links: 













Comments

Popular posts from this blog

Java Design Patterns