Docker
Docker is a tool that makes it easy to run applications in containers.
Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Dockerfile.
A Dockerfile is like a set of instructions for making a container.
It tells Docker what base image to use, what commands to run, and what files to include.
For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.
Docker Files Contents:-
FROM
:- For the base image. This command must be on top of the docker file.
RUN
:-To execute commands, it will create a layer in the image.
MAINTAINER
:- author/Owner/Description
COPY
:-Copy files from the local system (dockerVM) we need to provide a source, and destination. (We can’t download the file from the internet and any remote repo)
ADD
:-Similar to copy, it provides a feature to download files from the internet, also we extract the file from the docker image side.
EXPOSE
:- To expose ports such as port 8080 for tomcat, port 80 for Nginx, etc…
WORKDIR
:- To set a working directory for a container.
CMD
:- Execute commands but during container creation.
ENTRYPOINT
:- Similar to CMD, but has higher priority over CMD, the first commands will be executed by ENTRYPOINT only.
Docker is made up of several components that work together to enable the creation, deployment, and management of containerized applications.
Here are the main components of Docker:
Docker daemon: The Docker daemon is the core component of Docker that runs on the host operating system and manages Docker images and containers. It listens for Docker API requests and handles the creation, deletion, and management of Docker objects such as images, containers, networks, and volumes.
Docker client: The Docker client is a command-line tool that allows users to interact with the Docker daemon. Users can use the Docker client to build and manage Docker images, create and run Docker containers, and manage Docker networks and volumes.
Docker images: Docker images are read-only templates that contain everything needed to run an application, including the application code, dependencies, libraries, and system tools. Docker images are built from Dockerfiles, which are plain text files that specify the instructions needed to build the image.
Docker containers: Docker containers are running instances of Docker images. Each container runs in its own isolated environment, with its own file system and network interfaces. Containers can be created, started, stopped, and deleted using Docker commands.
Docker registry: A Docker registry is a central repository for Docker images. Docker users can push and pull Docker images from a registry to share them with others or use them across different environments. Docker Hub is a popular public registry for Docker images, but users can also set up their own private registries.
Docker network: Docker networks provide communication between Docker containers, as well as between Docker containers and the host system or other external systems. Docker supports several types of networks, including bridge networks, overlay networks, and host networks.
Docker volumes: Docker volumes provide persistent storage for Docker containers, allowing data to persist even if the container is stopped or deleted. Docker volumes can be created and managed using Docker commands and can be used to share data between Docker containers or between a container and the host system.
Install Docker Engine:
- Login to your EC2 Instance:
Verify if the docker is installed or not
rpm -qa | grep -i docker
Search for the docker RPM
yum search docker
Update the System Packages
yum update -y
Install Docker
yum install docker -y
Verify the Docker service is in running and active status.
systemctl status docker
Start/Restart if required (If not running)
systemctl restart docker
You can view the running containers using
docker ps
docker ps -a
If you are getting a "permission denied" error for the user then add the user to the docker group
whoami
grep docker /etc/group
sudo usermod -aG docker ec2-user
grep docker /etc/group
docker ps
sudo su
docker ps
Download Mysql Image and Verify that the Image exists
docker pull mysql
docker images
Run the Container
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=em7admin mysql:latest
docker ps
To go into the Docker Container
docker ps
docker exec -it <Container-ID> bash
Kill Docker Container
docker kill <Container-ID>
docker ps
Running the Docker Container in the background using -d [deteched] option
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=em7admin -d mysql:latest
Or
docker run -p 80:80 nginix:latest
Docker CMDS:
You can find the Images on DockerHub here:-
https://hub.docker.com/search?q=&type=image
Check the version of the Docker Server Engine running on the Host
docker version
Start a container from an Image [run]
docker run <Image-Name>
docker run <
IMAGE:TAG
>
i.e
docker run nginx
docker run alpine:3.17
Note: It will download the image "nginx" from DockerHub if unable to find it in the local repository and run it.
When you are using your own Images, in that case, you need to use your username and the repository name i.e
docker run <username>/<Repository-name>
Run a container with the nginx:1.14-alpine image and name it webapp
docker run -d --name webapp nginx:1.14-alpine
List containers [ps]
docker ps
To show all the containers including the existing ones
docker ps -a
Stop a container[stop]
docker stop <Container ID / Container Name>
Remove a container [rm]
docker rm <Container ID / Container Name>
You can remove multiple Containers same time.List images[images]
docker images
Note: Delete all dependent containers to remove the imageRemove images [rmi]
docker rmi <
IMAGE:TAG
>
Delete all images on the host
First:- Stop and delete all the containers being used by images.
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Then run the command to delete all the available images:
docker rmi $(docker images -aq)
Download an image [pull] [Downloading the image without installing it]
docker pull <
IMAGE
>
Execute a command on a running container [exec]
docker exec <Contianer-ID> <CMD-To-Run-On-Container>
i.e
docker exec ngnix cat /etc/hosts
Attach and Detach [Run]
docker run kodekloud/simple-webapp
-d: is used to run the container in the background
docker run –d kodekloud/simple-webapp
To attach the container again
docker attach <container-id>
Append a command
docker run centos
docker run centos sleep 50
docker run -d centos sleep 50
docker run ubuntu cat /etc/*release * docker run ubuntu:12.10 cat /etc/*release*
When running a BASE image it will exit automatically as nothing is running on it, so you can use this to get that working.docker run -it centos bash
// To login into the containerdocker run -d centos sleep 20
RUN - STDIN
-i : Interactive Mode
-t : sudo terminaldocker run –i kodekloud/simple-prompt-docker docker run –it kodekloud/simple-prompt-docker
Delete all containers [Both Running and Not Running ones] from the Docker Host.
Note: You may have to stop containers before deleting them.Stop all the containers
docker stop $(docker ps -a -q)
Remove all the containers
docker rm $(docker ps -a -q)
Or
docker ps -a | cut -d " " -f1| xargs -I {} docker stop {}
docker ps -a | cut -d " " -f1| xargs -I {} docker rm {}
Note:- The container is only live as long as the process inside it is Alive, if the Webservice container is stopped/crashed, the container exists.
Run – PORT mapping
docker run –p 80:5000 kodekloud/simple-webapp
docker run –p 8000:5000 kodekloud/simple-webapp
docker run –p 8001:5000 kodekloud/simple-webapp
docker run –p 3306:3306 mysql
docker run –p 8306:3306 mysql
- RUN – Volume mapping
To persist data, you need to map the directory outside of the container on the Docker host to a directory inside the container...docker run –v /opt/datadir:/var/lib/mysql mysql
Containers are assigned with private IPs, which are not accessible on the Internet, you can use docker inspect <container-id>
cmd to know the container IP.
2 Ways to access the application on Web UI :-
1. Internal IP [To access it via Internal IP, you need to be inside the DockerHost ]
2. Mapping a port to the Docker Host and accessing it using the External IPdocker run -p 8080:8080 jenkins
To persist the data/configuration across the docker container or during the restarts, you need to map a volume.
docker run -p 8080:8080 -v /root/<local-directory>:/var/jenkins_home -u root jenkins
Inspect Container
docker inspect <container-ID/Name>
Container Logs
docker logs <container-ID/Name>
Docker History
docker history <IMAGE>
Run an instance of kodekloud/simple-webapp with a tag blue and map port 8080 on the container to 38282 on the host.
Run the command:docker run -p <Contianer-Port>:<Host-Port> <
IMAGE:TAG
>
i.edocker run -p 38282:8080 kodekloud/simple-webapp:blue
You can run this container in the background after adding the -d flag.
Here:
Container Port: 8080
Host Port: 38282
Docker Images
-- Build an Image:docker build -t . <
Image
Name>
docker build -t <
Image-Name:TAG
> .
NOTE: At the end of the command, we used the "." (dot) symbol which indicates for the current directory, so you need to run this command from within the directory that has the Dockerfile.
- Run an instance of the image webapp-color and publish port 8080 on the container to 8282 on the host.
Container with image 'webapp-color'
Container Port: 8080
Host Port: 8282
Sol: docker run -p 8282:8080 webapp-color
How to check what is the base Operating System used by the python:3.6 image?
docker run python:3.6 cat /etc/*release*
-- Environment Variables
You can use docker inspect cmd to identify the value set to the APP_COLOR variable.docker inspect <contianer-id> | grep APP_COLOR
export APP_COLOR=blue; python
app.py
docker run -e APP_COLOR=blue simple-webapp-color
docker run -e APP_COLOR=pink simple-webapp-color
docker run -p 38282:8080 --name blue-app -e APP_COLOR=blue -d kodekloud/simple-webapp
To know the env field from within a webapp container, rundocker exec -it <contianer-id> env
i.e
docker exec -it webapp env
- Deploy a
MySQL
database using the mysql image and name it mysql-db. Set the database password to use db_pass123. Look up the mysql image on Docker Hub and identify the correct environment variable to use for setting the root password.
Name: mysql-db
Image: mysql
Env: MYSQL_ROOT_PASSWORD=db_pass123
Sol:docker run -d -e MYSQL_ROOT_PASSWORD=db_pass123 --name mysql-db mysql
To know the env field from within a mysql-db container, rundocker exec -it mysql-db env
docker stop: Stop a running container (send SIGTERM, and then SIGKILL after grace period) [...] The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL.
docker kill: Kill a running container (send SIGKILL, or specified signal) [...] The main process inside the container will be sent SIGKILL, or any signal specified with option --signal.
Both the docker kill and docker stop commands stop a container. One more way to stop a container is just to remove it. We can use the docker rm command to remove a container. This will remove the container from local storage immediately.