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.
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)
Find help for a specified command of container ( In below example I am searching the options available for docker container ls command)
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.
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.
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
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
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
In above example web1 container will be created with 8mb memory allocation
Find the statistics of container web1
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
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
- Using Docker Command
docker ps
- Using Docker Management Command
docker ps -a
- Using Docker Management Command
- Using Docker Management Command
docker container ls --all
Interact with Running Container (docker exec -it <Container id/name> bash)
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)
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
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).
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).
In above example container c1 will be un paused (provided it was paused before this).
Kill a Container
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)
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)
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)
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 copy test.html file to webserver.
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 copy 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.
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