How To Create And Manage Docker Containers
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to install docker in AWS EC2 instance.
https://cloudaffaire.com/how-to-install-docker-in-aws-ec2-instance/
In this blog post, we will discuss the docker container. We will also create our 1st docker container. We will also explore all docker container management commands in this demo.
What is a Docker container?
A container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.
Container vs VM:
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.
Prerequisite for this demo:
- One AWS EC2 instance with Linux 2 AMI and internet access.
- Docker
How To Create And Manage Docker Containers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
##----------------------------- ## Docker: Container Management ##----------------------------- ## docker container [COMMAND] ## Create a new docker container ## docker container create --name docker container create --name myWEBSERVER --publish 80:80 httpd ## List all containers docker container ls -as ## Start a docker container ## docker container start docker container start myWEBSERVER ## Display detailed information on one or more containers ## docker container inspect docker container inspect myWEBSERVER ## Display container logs ## docker container logs docker container logs myWEBSERVER ## List port mappings or a specific mapping for the container ## docker container port docker container port myWEBSERVER ## Get inside a docker container ## docker container exec -it docker container exec -it myWEBSERVER bash exit ## Copy a file from localhost to docker container ## docker container cp ## docker container cp echo "hello world" > sample_data.txt docker container cp /home/ec2-user/sample_data.txt myWEBSERVER:/usr/local/apache2/htdocs #copy from host to container docker container cp myWEBSERVER:/usr/local/apache2/htdocs/index.html /home/ec2-user/ #copy from container to host ls ## Inspect changes to files or directories on a container's filesystem ## docker container diff docker container diff myWEBSERVER ## Export a container"s filesystem as a tar archive ## docker container export -o docker container export -o myWEBSERVER_backup.tar myWEBSERVER ## Display a live stream of container(s) resource usage statistics ## docker container stats -a ## docker container stats docker container stats myWEBSERVER ## Display the running processes of a container ## docker container top docker container top myWEBSERVER ## Pause all processes within one or more containers ## docker container pause docker container pause myWEBSERVER ## Unpause all processes within one or more containers ## docker container unpause docker container unpause myWEBSERVER ## Restart one or more containers ## docker container restart docker container restart myWEBSERVER ## Rename a container ## docker container rename docker container rename myWEBSERVER myWEBSERVERNEW ## Create a new image from a container"s changes ## docker container commit -a " docker container commit -a "Debjeet Bhowmik" myWEBSERVERNEW myhttpd ## Update configuration of one or more containers docker container update --kernel-memory 200M myWEBSERVERNEW ## Stop one or more running containers ## docker container stop docker container stop myWEBSERVERNEW ## Kill one or more running containers ## docker container kill docker container kill myWEBSERVERNEW ## Remove one or more containers ## docker container rm docker container rm myWEBSERVERNEW ## Remove all stopped containers ## docker container prune docker container prune ## Run a docker container docker container run --name myWEBSERVER --publish 80:80 --detach myhttpd ## Cleanup (If you want to continue with the next demo, do not remove) docker container rm -f myWEBSERVER |
Hope you have enjoyed this article. In the next blog post, we will discuss the docker system.
To get more details on docker, please refer below docker documentation