9 Jan, 2025 Β· 7 min read
"Learn Docker on AWS EC2 Instance - Ubuntu"
If you have ever used Oracle Virtual Box, you might know that we install operating systems in it, which will have its environment inside, and we can install many operating systems inside it
You can see the above image, in the virtual box we use a hypervisor, above which we install operating systems [Guest OS] for each VM, which will have its dependencies and libraries
As there are multiple OS installed it will cause a deficiency in infrastructure and speed
But in the case of containerization, we will have a single operating system [Host OS], on top of which we will use a container engine [similiar to a virtual box], in which we will run containers that are lightweight with minimal dependencies
Containerization results in efficiency and faster processing and booting
1# update ubuntu 2sudo apt-get update 3 4# install docker 5sudo apt-get install git docker.io -y 6 7# add current user to docker group 8sudo usermod -aG docker $USER 9 10# start docker service 11sudo systemctl start docker 12 13# check docker version 14docker --version
1# single command for all steps 2sudo apt-get update && \ 3sudo apt-get install git docker.io -y && \ 4sudo usermod -aG docker $USER && \ 5sudo systemctl start docker && \ 6docker --version
Similar to GitHub you can pull or push the images from or to Docker Hub
Each image has a unique ID and tag [kind of version]
docker pull <image>:[tag]
==> Pull images from the docker hub, a tag is optional, when a tag is not provided will pull the latest version
docker images
==> Display images available on local [initially it will show empty]
docker rmi <image-name/id>
==> Remove the image from local
docker image inspect <image-name/id>
==> Display details about the image
docker inspect <image-name/id>
==> Display details about the image
docker build -t <image-name>:<tag> <dockerfile>
==> Create your own image from docker file [will see in detail below]
docker save -o <file-name>.tar <image-name>:<tag>
==> Save an image as a tar file
docker load -i <file-name>.tar
==> Load image from the tar file
What is it?
Docker container is like a running copy of the program from the image
What is inside?
It's a place where a program runs its little world of files, like a mini-computer just for that program
How it works?
You can start, stop, or remove these containers easily. They're like portable units that always run the program the same way.
Why it's useful?
Containers make sure the program runs smoothly, and you can use them anywhere, like on your computer or in a big computer center.
In simple words, a container is the running state of an image
There are 5 container states
docker create <image-name/id:tag>
==> Create a container from the image, if the image is not found locally then will be pulled from the docker hub [tag is optional]
docker run <image-name/id>
==> Create a running container from an image
It is running a container, but we can't use our terminal because the terminal is occupied by a container, to run the container in the background we can use some flag
--network
==> Add container to network [will see network below]
-d
==> Run container in the background
--name
==> Give some name to container [docker will give some random name by default]
-it
==> Open container in interactive mode
-p
==> Port Mapping
-v <volume-name>:<path-in-container>
==> Attach volume while running a container
1# Preferred sequence for providing flags 2docker run --name <container-name> -p <local-port>:<docker-port> -v <volume-name>:<path-in-container> -d <image-name/id>
1# Run a container interactively 2docker run -it <image-name>:<tag> /bin/bash
docker ps
==> List all the running containers
-a
==> List all containers
-q
==> List idβs only
docker start <container-id/name>
==> Start the container
docker stop <container-id/name>
==> Stop the container
docker restart <container-id/name>
==> Restart the container
docker rm <container-id/name>
==> Delete the container
docker exec -it <container-id/name> <command>
==> Execute a command in a running container
docker inspect <container-id/name>
==> Details of container
docker logs <container-id/name>
==> View container logs
1# Copy files from/to a container 2 3docker cp <container_id>:<path_in_container> <local_path> 4docker cp <local_path> <container_id>:<path_in_container>
What is it?
Storage for Docker container kept outside of container which helps to keep your data safe.
What is inside?
Data which container needs or generates like database, logs, config files. Similar to a hard disk.
How it works?
They are created outside of a container, in your host machine. Which can be easily attached to multiple containers. You can read, write, and share data across containers.
Why it's useful?
It keeps your data intact even if the container crashes or is deleted. Perfect for storing important stuff like databases or logs that you donβt want to lose. π\n \n\n* You can see the location of volume in your host machine using inspect *[given below]_
docker volume ls
==> List all volumes
docker volume create <volume-name>
==> Create a volume
docker volume rm <volume-name>
==> Remove volume
docker volume inspect <volume-name>
==> Get details of volume like location, name, etc.
What is it?
Docker network is like a virtual LAN.
What is inside?
It has rules and settings that allow containers to communicate with each other.
How it works?
Docker connects all containers to a bridge network. [one of the types of docker network]
Why it's useful?
It allows you to manage how your containers communicate with each other and the outside world, whether on the same machine or spread across different hosts. π
docker network ls
==> List all networks
docker network create --driver <network-type> <network-name>
==> Create a network
docker network rm <network-name>
==> Remove the network
docker network connect <network-name> <container-name/id>
==> Remove container from network.
docker network connect <network-name> <container-name/id>
==> Add container to network.
Yo, peeps! Youβre all set to roll with Docker now! π. Wanna check out Docker files? Stay tuned for my Docker Part 2 blog... itβs droppinβ soon! π. Till thenβ¦