Docker is a tool for building container-based applications and services that run on a single OS kernel. First developed in 2013, it has grown in adoption due to its standardized method to run and ship applications. The primary way to use Docker is through its command line interface. Though support of each command may vary between Linux and Windows, the commands are still the same.
Also, you may have to prefix each docker command with "sudo".
Installing Docker in Amazon EC2
As of 2018, Docker containers still run on top of EC2 instances in Amazon. Therefore, for experimenting with containers, it is best to try them out first in your own EC2 instance before deploying them to Amazon's Elastic Container Service or Amazon Fargate.
# ssh into your ec2 instance # my_ec2_instance could be something like "ec2-51-89-70-101.compute-1" ssh -i "my_cert.pem" ec2-user@my_ec2_instance.amazonaws.com # update yum sudo yum update -y # install docker sudo yum install -y docker # start docker sudo service docker start sudo usermod -a -G docker ec2-user
An image is an executable package that contains all the dependencies and code needed to run.Container
A container is a running instance of an image.
docker ps will show you a list of running containers.
CommandsNote: These commands are in order of setting up and taking down a docker environment
Gets the base image from the docker repository.
docker pull ubuntu
Builds a docker image from a
docker build -t ubuntu_image .
Run a command in a new container.
-d will run the container in the background.
--privileged gives extended privileges to the container.
docker run -dit --name ubuntu_container ubuntu_image
List all the containers.
By default, it just shows the running containers.
docker ps -a
||Show all containers (default shows just running)|
||Filter output based on conditions provided|
||Pretty-print containers using a Go template|
||Show n last created containers (includes all states)|
||Show the latest created container (includes all states)|
||Don’t truncate output|
||Only display numeric IDs|
||Display total file sizes|
Execute a command on a running docker container.
docker exec -it ubuntu_container /bin/bash
Stops one or more containers.
docker stop ubuntu_container
Example (stops all running containers):
docker stop $(docker ps -a -q)
docker kill ubuntu_container will shut down the container non-gracefully.
Example (kills all running containers):
docker kill $(docker ps -q)
Removes one or more containers.
docker rm ubuntu_container
Example (removes all stopped containers):
docker rm $(docker ps -a -q)
Removes one or more images.
docker rmi ubuntu_image
Example (removes all images):
docker rmi $(docker images -q)