├── README.MD ├── docker-architecture.MD ├── docker-architecture.png ├── docker-commands.MD └── docker-install.sh /README.MD: -------------------------------------------------------------------------------- 1 | ### Docker installation 2 | 3 | We are using Amazon Linux 2 OS for this. For reference 4 | 5 | ``` 6 | https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-container-image.html#create-container-image-install-docker 7 | ``` 8 | ### Connect EC2 instance in AWS 9 | 10 | * Create a security group that can allow all IP to all ports. Create inbound and outbound both. 11 | * Launch an EC2 instance with AWS Linux 2 and create key pair in ppk format 12 | * We are going to use Putty and Super putty to connect our instances. You can use any SSH client to connect. 13 | * to connect any instance you need to provide 14 | * IP 15 | * Username 16 | * Password/private key 17 | * Port (SSH by default takes port number 22) 18 | 19 | ### Install Docker in AWS Linux 2 20 | 21 | * Update OS packages 22 | ``` 23 | sudo yum update -y 24 | ``` 25 | 26 | * Install Docker 27 | ``` 28 | sudo amazon-linux-extras install docker 29 | ``` 30 | 31 | * Start Docker 32 | 33 | ``` 34 | sudo service docker start 35 | ``` 36 | 37 | * Enable it 38 | 39 | ``` 40 | sudo systemctl enable docker 41 | ``` 42 | 43 | * Add user to docker group 44 | 45 | ``` 46 | sudo usermod -a -G docker ec2-user 47 | ``` 48 | 49 | Just logout and login again to run docker commands with normal user. -------------------------------------------------------------------------------- /docker-architecture.MD: -------------------------------------------------------------------------------- 1 | ### Docker Architecture 2 | 3 | Docker architecture consists of 4 | 5 | * Docker client which is shell 6 | * Docker daemon 7 | * Docker Registry 8 | * Docker objects 9 | * Images 10 | * Containers 11 | * Docker networking 12 | * Docker storage 13 | 14 | ![alt text](docker-architecture.png) -------------------------------------------------------------------------------- /docker-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techworldwithsiva/docker-install-commands/7b5220df81787283eb7f0e7e202de7327a02bdc4/docker-architecture.png -------------------------------------------------------------------------------- /docker-commands.MD: -------------------------------------------------------------------------------- 1 | ### Docker Commands 2 | 3 | Container is the running version of image. You can consider container as mini server. It too have 0-65,535 ports. 4 | 5 | * To display the available images 6 | ``` 7 | docker images 8 | ``` 9 | * To pull the image from Docker Hub. If you dont mention version it will pull the latest version by default. 10 | ``` 11 | docker pull [image-name]:[version] 12 | ``` 13 | * To create container out of image 14 | ``` 15 | docker create [image-id] 16 | ``` 17 | * To display the running containers. 18 | ``` 19 | docker ps 20 | ``` 21 | * To display all the containers. 22 | ``` 23 | docker ps -a 24 | ``` 25 | * To start the container 26 | ``` 27 | docker start [container-ID] 28 | ``` 29 | * To pull, create and start the container with single command. 30 | ``` 31 | docker run [image-name]:[version] 32 | ``` 33 | * By default docker containers run in the foreground attached to the screen. If you want to detach use -d 34 | ``` 35 | docker run -d [image-name]:[version] 36 | ``` 37 | * To stop the running container 38 | ``` 39 | docker stop [container-ID] 40 | ``` 41 | * To remove the container. If it is running container use -f to force 42 | ``` 43 | docker rm [container-ID] 44 | ``` 45 | * To remove images. 46 | ``` 47 | docker rmi [image-ID] 48 | ``` 49 | * To assign a port to the container 50 | ``` 51 | docker run -d -p [host-port]:[container-port] [image-name]:[version] 52 | ``` 53 | * To name the container. 54 | ``` 55 | docker run -d --name [name-you-wish] [image-name]:[version] 56 | ``` 57 | * To login the container shell. To come out use CTRL+d 58 | ``` 59 | docker exec -it [container-id] bash 60 | ``` 61 | -------------------------------------------------------------------------------- /docker-install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | R="\e[31m" 4 | G="\e[32m" 5 | Y="\e[33m" 6 | N="\e[0m" 7 | 8 | LOG=docker-install.log 9 | USER_ID=$(id -u) 10 | if [ $USER_ID -ne 0 ]; then 11 | echo -e "$R You are not the root user, you dont have permissions to run this $N" 12 | exit 1 13 | fi 14 | 15 | VALIDATE(){ 16 | if [ $1 -ne 0 ]; then 17 | echo -e "$2 ... $R FAILED $N" 18 | exit 1 19 | else 20 | echo -e "$2 ... $G SUCCESS $N" 21 | fi 22 | 23 | } 24 | 25 | yum update -y &>>$LOG 26 | VALIDATE $? "Updating packages" 27 | 28 | amazon-linux-extras install docker -y &>>$LOG 29 | VALIDATE $? "Installing Docker" 30 | 31 | service docker start &>>$LOG 32 | VALIDATE $? "Starting Docker" 33 | 34 | systemctl enable docker &>>$LOG 35 | VALIDATE $? "Enabling Docker" 36 | 37 | usermod -a -G docker ec2-user &>>$LOG 38 | VALIDATE $? "Added ec2-user to docker group" 39 | 40 | yum install git -y &>>$LOG 41 | VALIDATE $? "Installing GIT" 42 | 43 | curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose &>>$LOG 44 | VALIDATE $? "Downloaded docker-compose" 45 | 46 | chmod +x /usr/local/bin/docker-compose 47 | VALIDATE $? "Moved docker-compose to local bin" 48 | 49 | echo -e "$R You need logout and login to the server $N" 50 | 51 | --------------------------------------------------------------------------------