Wednesday, February 23, 2022

EXAMPLES: Docker Container




DOCKER CONTAINERS

Applications like Microservices, WebApp, DbApp, etc are deployed on containers.

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. 


DOCKER CONTAINER COMMANDS

Find help for all the commands related to container
        
        docker container --help

Find help for a specified command of container ( In below example I am searching the options available for docker container ls command)

        docker container ls --help


Run a Container in attached mode. (by default, Docker runs a container on the foreground. This means we can't return to our shell prompt until the process finishes).

        docker container run -it ubuntu

In above example docker container runs in attached mode. -it option is used to run the container is interactive terminal.

Run a Container in detached mode (-d).
This command starts the container, prints its id, and then returns to the shell prompt.

        docker container run -it -d ubuntu

Run a Container with a name (If you do not create the name of the container then docker assign some random name to the container). We can use container name alternatively with Container id

        docker container run -it --name c1 -d ubuntu

In above example c1 is the name of the container

Run a Container with limited memory (by default its 2gb)

        docker run -m 8m -dit --name web1 nginx

In above example web1 container will be created with 8mb memory allocation

Find the statistics of container web1

        docker stats web1

Run Container with limited CPU Allocation

        docker run -c 614 -dit --name db nginx 
        docker run -c 410 -dit --name web nginx

This will give 60% to the db container (614 is 60% of 1024) and 40% to the web container

Inspect a Container

        docker inspect web

Find logs of a container

        docker logs web

List running the containers
    - Using Docker Command

        docker ps

    Using Docker Management Command
    
    
        docker container ls

List all the containers
    - Using Docker Command
        
        docker ps -a

    Using Docker Management Command

        docker container ls --all

Interact with Running Container (
docker exec -it <Container id/name> bash)

        docker exec -it c1 bash

In above example, we are interacting with container c1 (means we are inside container c1, exit command is used to exit from container)

Stop a Container

        docker stop c1

In above example container c1 will be in Exited mode - ExitCode (0)

Start a Container

        docker start c1

Restart a Container

        docker restart c1

In above example container c1 will be restarted(started container will be stopped and started again while stopped container will be started).

Pause a Container

        docker pause c1

Undo unpause a Container

        docker unpause c1

In above example container c1 will be un paused (provided it was paused before this).

Kill a Container

        docker kill c1

In above example container c1 will be stopped forcefully - ExitCode (137).

remove a Stopped Container

        docker rm c1

remove a Container forcefully (this way, even a running container will be removed)

        docker rm -f c1

remove all the containers

        docker rm -f $(docker ps -a -q)

-q means quietly, even if there are errors, it will ignore and not show


Port forwarding

Run the following command to do the port forwarding with Host Machine

        docker container run -it --name webserver -p 80:80 -d ubuntu

first 80 is port number of host machine, while second 80 is port of the container. 80:80 means port of container is mapped to port of host machine.
IF you cannot run above commands, it is because your web servers on your host machine is using the port, thus you need to purge any web servers (e.g apache, nginx, etc)

To run the application on my container and access it through web browser

        docker exec -it webserver bash #go inside the container

Run the following commands in the container

        apt update #inside the container it will update the apt repo 
        apt install apache2 -y #it will install the apache in the container (it will ask geographic - 6, 71)
        service apache2 start #it will start apache service in container

Go to your browser and use public IP address and you will be able to see Apache default page.

        exit #to exit from the container

on your local machine create a file called test.html and write some html code then c
opy test.html file to webserver.

        echo "<h1> this is super cool page for apache </h1>" >> test.html
        docker cp test.html webserver:/var/www/html/   #copy the test.html file to the container

go to browser and PublicIP/test.html, you will be able to see the test.html output on the browser.
on browser, type:

        localhost/test.html


No comments:

Post a Comment

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...