# Docker

What is docker?

Docker is a container platform that allows you to build,test and deploy applications quickly.A developer defines all the applications and it's dependencies in a docker file which is then used to build docker images that defines a docker container.

**Why do you need docker?**

Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

There are two types of editions in docker:-

1. Community Edition:- community edition is a set of free Docker products. this is available on Linux, mac, Windows or on cloud platforms like AWS or Azure
    
2. enterprise edition:-Enterprise Edition is a certified and supported container platform that comes with enterprise add-ons like the image management image Security and universal control plan for managing and orchestrating container run times
    

### **Basic docker commands:-**

1. Docker run "image\_name":-This command runs the file that you want to
    
2. docker ps:- this command lists all running containers and some basic information about them.
    
3. docker ps -a:-This command shows all running as well as previously stopped or exited containers.
    
4. docker stop "container name or id":-By this command stops the container that you want.
    
5. docker rm "container name or id":-docker rm command to remove stopped or exited container permanently
    
6. docker images:-docker images command to see a list of available images and their sizes on our host.
    
7. docker rmi "image name ":-This command deletes the image that you want.
    
8. docker pull "container name":-this command is for downloading an image.
    
9. docker exec "file name"-the docker exec command to execute a command on my docker container, to see the contents of a file inside this particular container.
    
10. docker run -d "repo name":-This will run the docker container in the background mode.
    
11. cat /etc/\*release\*:-to just to check the operating system that we are on.
    
12. docker run -d "file name":-the docker container in the detached mode by providing the -d option. This will run the docker container in the background mode and you will be back to your prompt immediately.
    
13. docker attach "name or id of container":-This is for attach the container.
    
14. docker stop $(docker ps -aq):-to stop all the running and non running container .
    
15. docker rm $(docker ps -aq):-To remove all the stopped containers at once, run the command
    
16. docker run -d --name webapp nginx:1.14-alpine:-Run a container with the `nginx:1.14-alpine` image and name it `webapp`
    

### **Docker run**

"docker run image\_name":-To run a specified docker image.

"docker run image\_name: version":-To run a specified docker image with a specified version.

**Docker run STDIN(Standard Input):-**

"docker run image\_name":-This will directly show the output of the container. But let us assume that our application requires an input to display the output statement.

"docker run -i image\_name":-This will take us into interactive mode and it will start taking in inputs and also display it with the output of the container. But there is still one problem. Let us now assume that our program first asks us to enter my name, this command will not display that, it will directly take inputs.

"docker run -it image\_name":-This will take us into interactive mode and also attach a terminal with it. So this will first show us enter your name message and also take in input, then it will also show the output attached with our input.

example:-

. Let us run an application that is asked for my name and on entering my name print a welcome message

Welcome! Please enter your name: Yash(my input)

output:- Welcome Yash!

. Now, we run this on the docker:-

docker run "image\_name"

output:- Welcome Yash!

This will directly show the output of the container not taking any input

. for taking input i parameter is for interactive mode and when I input my name it prints the expected output

docker run -i "image\_name"

Welcome! Please enter your name: Yash(my input)

output:- Welcome Yash!

But there is still one problem. Let us now assume that our program first asks us to enter my name, this command will not display that, it will directly take inputs.to solve this problem we take -t , the -t stands for pseudo-terminal. So with the combination of -i and t, we're now attached to the terminal

docker run -it "image\_name"

Welcome! Please enter your name: Yash(my input)

output:-Welcome Yash!

### **Docker Images**

let's suppose we are creating a simple web application that I have built using the python flask framework.

There are steps to creating an image

1. OS ubuntu:-We start with an operating system like ubuntu.
    
2. update apt repo:-Then update the source repositories using the apt command.
    
3. Install dependencies using apt:-Then install dependencies using the apt command then.
    
4. Install Python dependencies using pip:-install Python dependencies using the PIP command.
    
5. Copy source code to/opt folder:-copy over the source code of my application to a location like /opt
    
6. Run the web server using the "flask" command:-Then finally run the web server using the flask command.
    

## **Creating your own image:-**

First, we need to understand what we are containerizing, what application we are creating an image for, and how the application is built.

Step 1:- Create a file with the name "Dockerfile" in the project folder.

Step 2:- In the Dockerfile a simple instruction and argument approach is followed.

`[instruction] [argument]`

All the instructions are on the left and are typed in all caps. Each of these instructions instructs Docker to perform a certain action while creating the image.gbn

Let's start from the beginning of the Dockerfile.

The first line contains the base image. Every Docker image must be based on another image. This means the installation of the base image is what we require in our image. So that when the container of our image runs we can access all the commands associated with the base image.

`FROM <Base_Image>`

> ***"It is important that every Dockerfile must start with FROM instruction".***

Step 3:- In this step RUN instruction is used. We add all the commands which install and update dependencies that are essential for our project to execute.

`RUN <Command>`

Step 4:- Now we use the COPY instruction to copy files from the local system into the docker image. This is done because the project that we want to execute is in our local system and we want to make our project available to the docker image.

`COPY <Source> <Destination>`

Step 5:- This is the final step in the creation of our Dockerfile. This step involves CMD instruction which stands for command and defines the command that will be run within the container when it starts.

`CMD [Command]`

The final file should look something like this:-

![Dockerfile](https://cdn.hashnode.com/res/hashnode/image/upload/v1674911099593/2cd29aa0-b0ac-4b68-9341-63b2e3143e1b.png?auto=compress,format&format=webp align="center")

Step 6:- Now that we've finally completed our Dockerfile. It's time to build it.

To build it we'll use `docker build -t my_app:1.0 <Location_of_the_Dockerfile>`

Step 7:- The last step is to run the Docker image using `docker run my_app:1.0`

# **Environment Variables**

However, if you decide to change the background color in the future you will have to change the application code.:-APP\_COLOR

\-e option to set an environment variable within the container to deploy multiple containers

example:- docker run -e APP\_COLOR=blue "image name"-color

# **Docker Compose**

we first learned how to run a docker container using the docker run command. If we needed to set up a complex application running multiple services a better way to do it is to use Docker compose with Docker compose. We could create a configuration file in YAML format called "Docker-compose.yml".This is easier to implement run and maintain. All changes are always stored in the docker-compose configuration file

. docker run -d --name=image name:-by this we put multiple applications in a single docker engine.

how we linked applications together:-docker run -d --name=worker --link redis:redis worker

. this is an example of how to link the worker application with redis.

if we are trying to pull an image we can replace the image online with a build line and specify the location of a directory .which contain the application code and a docker file with instructions to build the Docker image.

build: ./vote

how to create a database container and image:-docker run --name "db container name" -d "image name"

# **Docker Engine**

Docker Engine is an open-source containerization technology for building and containerizing your applications. Docker Engine acts as a client-server application with:

* A server with a long-running daemon process [`dockerd`](https://docs.docker.com/engine/reference/commandline/dockerd).
    
* APIs which specify interfaces that programs can use to talk to and instruct the Docker daemon.
    
* A command line interface (CLI) client [`docker`](https://docs.docker.com/engine/reference/commandline/cli/).
    

Docker demon is a background process that manages Docker objects such as the images containers volumes and networks.

Simply use the -H option on the docker command and specify the remote Docker engine address and the port:- **docker -H=remote-docker-engine:2375**

\# Run a `mysql` container named `mysql-db` using the `mysql` image. Set database password to `db_pass123`

Note: Remember to run it in the detached mode.

\=docker run -d --name mysql-db -e MYSQL\_ROOT\_PASSWORD=db\_pass123 mysql

### **Docker Networking**

When you install Docker it creates three networks automatically bridge none And host bridge is the default network a container gets attached to. Bridge network is a private internal network created by docker on host.In None network the containers are not a test to any network and doesn't have any access to the external network or other containers they run in an isolated network.

# **Container Orchestration**

container orchestration is a solution that consists of a set of tools and scripts that can help host containers in a production Typically a container orchestration solution consist of multiple Docker hosts that can host containers that way even if one fails.The application is still accessible through the others a container orchestration solution easily allows you to deploy hundreds or thousands of instances of your application with a single command.
