Table of contents
- 1. Container Commands
- 1. Create a new container using the below command
- 2. Inspect the new container
- 3. SSH setup for containers
- 4. Shutdown a container
- 5. Log in to a stopped container
- 6. List all containers(stopped and running)
- 7. List given no. of containers
- 8. List running containers only
- 9. List Stopped containers only
- 10. Show the last container which you have created (stopped/running)
- 11. Naming the container
- 12. Rename a container
- 13. Deleting a container by giving its name or ID
- 14. Delete all (running/stopped) containers at once
- 15. Delete running containers only
- 16. List stopped containers only
- 17. Starting a stopped container
- 18. Attaching to a running container
- 19. Run a Linux command remotely in a container Or Get an independent terminal from a container remotely (from the Host)
- 20. Stopping a container from the 'host machine'
- 21. Inspecting the container's processes from the host machine
- 22. Show the last 4 containers (stopped/running)
- 23. Create a container in a background mode ( without terminal access )
- 24. Find More About The Container
- 25. List all container names
- 2. STATS:
- 1. Display usage statistics of a container
- 2. Allocating memory for a container (below command allocates 1 GB RAM)
- 3. Updating memory of an existing container
- 4. CPU Allocation
- Community and Social Footprints :
1. Container Commands
1. Create a new container using the below command
sudo docker run -it ubuntu /bin/bash
The "docker run" command provides all launching capabilities for docker to create a container. We use docker run to create new containers.
-i: open STDIN from the container
-t: tells docker to assign a pseudo-tty to the container
-it : provides an interactive shell
ubuntu: ubuntu is an image and also called as "stock image" or "base image". This image will be downloaded from Docker hub when we run 'docker run' command first time
/bin/bash: 'shell program' that will be installed in the terminal
2. Inspect the new container
Let's believe that it's a separate machine by using the below commands
hostname
cat /etc/hosts
hostname -i
ps -ef
3. SSH setup for containers
Setup SSH in the containers so that they can communicate with each other
Create two containers having IP addresses - 172.17.0.2, 172.17.0.3
Try to connect to 172.17.0.3 from 172.17.0.2 using the below command. You will get an error.
$ ssh demo@172.17.0.3
(you won't be able to connect by default)Install ssh server
$ apt-get update
$ apt-get install openssh-server
Start the server
$ service ssh start
(status/stop/restart)Create a user and set up a password
$ useradd -m -d /home/demo -s /bin/bash demo
$ passwd demo
Connect to the container using ssh from 172.17.0.2 or any other machine.
$ ssh demo@172.17.0.3
Enable root user over ssh
Add the below line under "# Authentication:" in "/etc/ssh/sshd_config"
PermitRootLogin yes
4. Shutdown a container
"exit" to stop the container
5. Log in to a stopped container
$docker start
$docker attach
6. List all containers(stopped and running)
$ docker container ls -a
$ docker ps -a
7. List given no. of containers
$ docker ps -a -n1
8. List running containers only
$ docker container ls
$ docker ps
9. List Stopped containers only
$ docker container ls -f status=exited
(Where Status can be exited/running)
The "docker container ls" command output shows:
- Image name from which container is created
- ID - the container can be identified using short UUID, longer UUID Or name.
- Status of the container (Up / Exited)
- Name of the container
10. Show the last container which you have created (stopped/running)
$ docker container ls -l
11. Naming the container
$ docker run --name demo -it ubuntu /bin/bash
Note: Two containers can't have the same name.
12. Rename a container
$ docker rename container_name_1 container_name_2
13. Deleting a container by giving its name or ID
$ docker rm ID/name
14. Delete all (running/stopped) containers at once
$ docker rm -f $(docker container ls -a -q)
$ docker rm -f $(docker ps -a -q)
15. Delete running containers only
$ docker rm -f $(docker container ls -q)
$ docker rm -f $(docker ps -q)
16. List stopped containers only
$ docker container ls -a -f status=exited
17. Starting a stopped container
$ docker start container_name
18. Attaching to a running container
$ docker attach container_name (OR)
$ docker attach b1b1c8dc1939
19. Run a Linux command remotely in a container Or Get an independent terminal from a container remotely (from the Host)
$ docker exec -it tomcat-server ps -ef
20. Stopping a container from the 'host machine'
$ docker stop container_name
(Gracefully stop the container)
$ docker kill container_name
(Forcibly stop the container)
21. Inspecting the container's processes from the host machine
$ docker top container_name
22. Show the last 4 containers (stopped/running)
$ docker ps -n4
23. Create a container in a background mode ( without terminal access )
$ docker run -it -d ubuntu /bin/bash
24. Find More About The Container
The 'docker inspect' command interrogates the container and returns complete information about it.
Ex: image name, IP, Memory details, hostname ..etc
Examples:
$ docker inspect container_name
$ docker inspect -f '{{.Config.Hostname}}' container_name
$ docker inspect -f '{{.NetworkSettings.Networks.bridge.IPAddress}}' container_name
Note: use --format (OR) -f
25. List all container names
$ docker inspect --format "{{.Name}}" $(docker ps -a -q) | tr -d '/'
2. STATS:
1. Display usage statistics of a container
$ docker stats --no-stream container_name
$ docker stats --no-stream --all
$ docker stats --no-stream --format {{.MemUsage}} container_name
$ docker stats --no-stream --format {{.CPUPerc}} container_name
2. Allocating memory for a container (below command allocates 1 GB RAM)
$ docker run -it --name container_name -m 1g ubuntu /bin/bash
$ docker run -it --name container_name -m 1024m ubuntu /bin/bash
3. Updating memory of an existing container
$ docker update -m 2024m container_name
4. CPU Allocation
$ docker run -it --cpus="2" --name container_name ubuntu /bin/bash
$ docker update --cpus="2" conatiner_name