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
Definitions
ImageAn 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.
Commands
Note: These commands are in order of setting up and taking down a docker environment
docker pull
Gets the base image from the docker repository.
Example: docker pull ubuntu
docker build
Builds a docker image from a Dockerfile
.
Example: docker build -t ubuntu_image .
docker run
Run a command in a new container.
-d
will run the container in the background.
--privileged
gives extended privileges to the container.
Example: docker run -dit --name ubuntu_container ubuntu_image
docker ps
List all the containers.
By default, it just shows the running containers.
Example: docker ps -a
Name | Description |
--all , -a |
Show all containers (default shows just running) |
--filter , -f |
Filter output based on conditions provided |
--format |
Pretty-print containers using a Go template |
--last , -n |
Show n last created containers (includes all states) |
--latest , -l |
Show the latest created container (includes all states) |
--no-trunc |
Don’t truncate output |
--quiet , -q |
Only display numeric IDs |
--size , -s |
Display total file sizes |
docker run -i -t ubuntu:xenial /bin/bash
Takes you to the bash of the ubuntu:xenial
image.
docker exec
Execute a command on a running docker container.
Example: docker exec -it ubuntu_container /bin/bash
docker stop
Stops one or more containers.
Example: docker stop ubuntu_container
Example (stops all running containers): docker stop $(docker ps -a -q)
Note: docker kill ubuntu_container
will shut down the container non-gracefully.
Example (kills all running containers): docker kill $(docker ps -q)
docker rm
Removes one or more containers.
Example: docker rm ubuntu_container
Example (removes all stopped containers): docker rm $(docker ps -a -q)
Example (removes all containers and volumes): docker rm -vf $(docker ps -aq)
docker rmi
Removes one or more images.
Example: docker rmi ubuntu_image
Example (removes all images): docker rmi -f $(docker images -q)