Docker Containers
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.
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 in 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 donot 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
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
Will give 60% to the db container (614 is 60% of 1024) and 40% to the web container
Inspect a Container
docker inspect web
With above command we can inspect/debug web container
Find logs of a container
docker logs web
Above command displays the logs of web container
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
Intreact with Running Container
Syntax
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
Start a Container
docker start c1
In above example container c1 will be started.
Restart a Container
docker restart c1
In above example container c1 will be restarted(started container will be stopped and started and Stopped container will be Started).
Pause a Container
docker pause c1
In above example container c1 will be paused.
UnPause a Container
docker pause c1
In above example container c1 will be unpaused.
kill a Container
docker kill c1
In above example container c1 will be stopped forcefully-ExitCode 137.
remove a Stopped Container
docker rm c1
In above example container c1 will be deleted.
remove a Container forcefully
docker rm -f c1
In above example container c1 will be removed forcefully.
remove all the containers
docker rm -f $(docker ps -a -q)
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
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
service apache2 start # it will start apache service in container
goto your browser and use public ip address and you will be able to see Apache defualt page
exit # to exit from the container
on your local machine create a file called test.html and write some html code
Copy test.html file to webserver
docker cp test.html webserver:/var/www/html/ # copy the test.html file to the container
got to browser and PublicIP/test.html, you will be able to see the test.html output on the browser
Assignment
Comments
Post a Comment