Docker Introduction
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Docker Hub
https://hub.docker.com/
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
We will see how to use Docker in practice with a complete workflow with a demo project.
1. we will see how to develop locally with containers
2. we will run multiple containers and services with Docker Compose.
3. We will build our own image with docker file. And we will push that built image to a private docker repository on AWS.
4. Finally we will deploy our containerized application.
5. How to persist data in docker
6. Learning the different volume types after configure our persistence or demo projects
Overview
What container is and what problems it solves:
Containers live in a container repository. many companies have their own private repositories where they store/host all their containers . But there is also a public repository for docker containers where you can browse and find any application container that you want.
search for docker hub which is the public repository for docker .
https://hub.docker.com/
In normal development without containers , there would be many applications that developers may have to install during the installation process. If there were 10 different application that you are using you may have to install 10 different applications.
With containers you may not need to install any of these software . You do not have to install any kind of application on your OS. because a container is its own isolated OS system layer with linux based image. you have everything packaged into one Isolated environment
The download of a container -- for in this case a container with PostgreSQL can be downloaded and run in a single command.
if you java script depends on 10 different service. all you need to do run 10 docker commands to install and run those services in your environment.
Traditional Development Process :
"In software development, an artifact is like the final version of the software that the team has created."
Traditional team will produce artifacts together with a set of instruction as to how to install and configure those artifacts on the server so you will have a jar file or similar for your application in addition you may have some database service or some other service also with a set of instruction of how to set it up on the server .
So the development team will give the artifacts to the operations team and the operations team will handle setting up the environment to deploy those applications.
No environment configuration is needed on the server all you need is a docker run time installed on the server. you will be able to run containers there.
-- Technically a container is made up of images , so we have layers of stacked images on top of each other.
-- And the base of a container you will have a linux based image which is either alphine with its specific version or could be some other linux distribution . Important for those base image to be small which is why most of them are really Aphine. Because that will make sure the container remain small in size . On top of the base image you wil have application image .
-- On top of the base image you have application image , usually you will have these intermediate images lead up to the actual application image that is going to run in the container. And of course on top of it you will have this configuration data.
Lets head to docker hub and look for postgresSQL ,
We can use a simple docker command to download and run any application such as PostgresSQL docker pull postgres
docker pull postgres:9.6 -- with the version of your choice.And if you do not specify it would give me the latest but we will use a version
First it say unable to find the image locally and the it goes to docker hub and starts downloading
As we said docker image consists of layers , this is what we see that it is download layer by layer.
The advantage of spliting those application layer is lets say the image changes or I have to download a newer version of the image postgres , what happens is that the layers are the same between those two applications two versions of postgres will not be downloaded again but only those layers that are different shall be downloaded.
it also starts the application after downloading it .
$ docker ps
with docker ps command you can see the container
Installing Docker :
1. setup a docker repository and download and install the docker repo.
2. Download and Install it from there.
The second option is install the packages manually. This is not a recommended way.
once you install dockers. the basic command that you use to test if it is installed is
$ docker run helloworld
if this command is successful that means docker was successfully installed on your computer.
Docker Commands
$ docker pull redis
$ docker images // check the images i have in my local docker folder.
we have so far only pulled the image from the docker repo. The container is not yet running lets run it
$ docker run redis
$ docker ps // to get the status of all the running containers
$ docker run redis -- command will run in an attached mode
if i were to stop it with the "Ctl + C" the redis application stops.
if I do > docker ps now you will see no containers running
There is an option in there in the docker containers to run it in "detached" mode with "-d" flag
$ docker run -d redis
I will only get the ID of container as output .
Detached Mode (-d flag):
When you start a container in detached mode using the -d flag, the container runs in the background, and you regain control of the command prompt immediately. This mode is often used for long-running services or applications that don't require interaction with the terminal.
- The container runs as a background process.
- The command prompt is immediately available for further commands.
- The container's output is not displayed in the terminal where you started the container.
- You can use
docker logsto view the container's output later. - The container continues running even after you close the terminal or disconnect from the shell.
Re-starting a Container
if there was an error in the container or you need to restart it for some reason you would need the container id.
$ docker ps
// get the id of the container
$ docker stop <container_id>
if you want to start it again use the same id
$ docker start < container_id_same_one_as_above>
If all the container are down you will not be able to see the container _id , in order to see the container id.
$ docker ps -a // this will show all the container that are running and Not running with other details.
you can restart is using the above container_id
Lets assume you need two different versions of the redis version
-- The last time you installed a redis which is 5.0
this time
$ docker run redis:4.10 // this command pulls and run a redis container at one command. It performs two commands basically.
How to use any container that you just started :
$ docker ps
In the output you also see the ports section.
which specifics which port the container is listening to the incoming request. Both containers open the same port which is 6379 what was specified in the image
How does that actually work and how does it not have conflict when running on the same port ?
Container is just a virtual environment running on your host and you can have multiple containers running on your host simultaneously it can be your laptop . pc whatever you are working on.
And you laptop has certain ports available that you can open for certain applications. How how it works is that you need to create a bind with port that your laptop machine has with port on the docker container .
Now you will have conflicts if you open two 5000 ports on your hostmachine because you will get a message your port is already bound/or used.
You have second and third containers , both listening on port 3000 . by the container and this is absolutely okay as long as you bind them to two different port on the host machine.
once your binding is already done you can connect to the running container using the port of your host
And the host will know how to forward the request to the container using the port on the host.
we will do the binding of the ports during the run command .
$ docker run -p6000:6379 redis //-p (host port:<container_port>)
next time when you run
docker ps -- you will see the binding here.
lets start in the detach mode.
$ docker ps -p6000:6379 redis -d
Debugging containers
$ docker ps
$ docker logs <container_id>
There is another by using the name of the container
$ docker logs <name_container>
when you create a container you get some random name . you can specify your name to the container while you are running docker run
$ docker run -d -p6000:6379 --name redis-older redis:4.0
To get the terminal of the container to debug the issue
$ docker ps
$ docker exec -it <container_id> /bin/bash -- so i get the bash
or
$ docker exec -it <container_name> /bin/bash -- so i get the bash
// it -- stands for Interative Terminal
this takes you inside if the docker container as a root user
in order to exit the container terminal
$ exit and you are out.
with docker run -- you are working with images
with docker start -- you are working with containers
Developing with containers
Docker Network
docker creates its Isolated Network where the dockers containers are running in . When i deploy two containers in it "Mongo" and "Mongoexpress" -- They can talk to each other using the container name
because they are in the same network. And the application that runs outside of docker such as the NodeJS are going to connect to them from outside from the host localhost:portnumber
Later when we package our application into its own docker image what we are going to have is MongoDB containers and we are going to have a NodeJS application that we wrote
including nodejs and the index, html . The docker network is going to connect to the Java script application using host name and the port number.
to check docker generated networks
$ docker network ls
we are going to create its own network for mongodb and mongodb_express we are going to call it mongo network.
$ docker network create mongo-network
Now if we want our mongodb container and the mongo express container use this network we have to provide the network option while we are running the containers.
You can use the docker hub page for mondo db where they have given the environment variable that we can use to start up the docker container.
docker run -p 27017:27017 -d -e --name mongodb -net mongo-network MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret mongo
rewriting to be in order
check the log to see if that was successful
docker logs the id at the end of the line.
Now lets start Mongoexpress
These are the databases that are created by default which are on Mongo which are created on startup. But we can create our own database and using the UI we can create our own database
As you have see we could have specified environmental variable on initdb during mongodb startup that could have created a new database . that does not matter we will create a new database here.
So we will call user-account database : now we can use it and connect to this database from nodejs
now both the mongodb database and the mongo express containers are running, now we can connect . Now we can connect to this database using NodeJS with the database.
docker commands :
$ docker logs <container_id> | tail // check the last of the logs
$ docker logs <container_id> -f // stream the logs -- the currently running logs.
In the last demo we ran two docker containers.
You do not want to run these tedious commands every time you want to automate it or make it easier.
There is a tool that makes it easier for us to run these commands that is docker compose.
say you want to run 10 docker containers that you want to run or interact with each other you can basically write a docker compose file.
docker compose file .
now I have a docker compose file how do i start using the docker containers using that.
Start docker containers using this docker compose file . If you have installed docker , docker compose comes packages inside it . So you should have both docker and docker composed installed as a package .
$ docker-compose -f mongo.yaml up // up says what I want to do with this file
It will start all the containers in the file.
docker ps -- check if there are no containers running
The containers logs are mixed .
Comments
Post a Comment