Docker

 




--------------------------------------------------------------------------------------------------------------------------

Docker commands 


run - start a container 


#docker run nginx


list all running containers 


#docker ps 


list all containers 


docker ps -a 


stop container 


#docker stop <name/id>


remove container 


# docker rm <name/id>


list available images


# docker images


remove an image


first stop all running containers of the images 


#docker rmi nginx 


how to download an image 


#docker pull nginx


or download and run


# docker run nginx 


execute commands on a docker container 


# docker exec <container_name> <command>


 example

# docker exec distracted_disk cat /etc/hosts 




run - attach / detach

by default docker container runs on attach mode 


run in attach mode 

(the console is confined to the output)


#docker run kodekloud/web-app




run in detach mode 

(the console is not confined to the output)


#docker run -d kodekloud/web-app


make a detach running command attach 


docker attach <detach id>




docker terminal mapping 


when we run docker container it runs as a isolated process and by default all the input and  output is blocked we must allow our docker to interactive and be able to publish the docker terminal on our standard output for that we will use tag 


# docker run -it <kodekloud/simple-prompt-docker>




port mapping 


  • by default all docker runs on isolated processes that don't interact with outside world 



  • how to access your application inside docker host 

  • use docker container ip (every docker container have its own ip but it is internal and can only be accessed inside container)

  • use the docker host ip (map port of container to host )


how data is persisted in docker container 


  • note all container`s are isolated and have isolated data storage and all the data is removed when the container is removed 



to securely store data inside container we will map a storage location outside of container to a location inside container 




inspect container


# docker inspect <container_name/id>


check logs of a container 


# docker logs <name/id>


Advanced docker commands 


run docker commands using tags 


# docker run ubuntu:17.10 cat /etc/*release*


to convert a detached mode running container to attach mode 


# take the container id 


# docker attach <id>


how to get the internal ip of a docker container 

# docker inspect <id>

then check the network section there is ip address



or to access using port mapping


# docker run -p 8080:8080 jenkins 


save container data


# make a local directory 


# mkdir my-jenkins-data 


then map the data inside container to the created directory on the host 


# docker run -p 8080:8080 -v /root/my-jenkins-data:/var/jenkins_home -u root jenkins


this configuration will store the jenkins data in /root/my-jenkins-data folder 


how to create your own image ??


first write down the steps you would perform while deploying the app 

write step by step 






docker file


layered architecture 


dockerfile builds the steps in a layered architecture for this reason if one layer have some problem the only that layer needs to built and other layer stay as it is 



 


creating a docker file 

FROM ubuntu 


RUN apt-get update 

RUN apt-get inatall -y python python-pip

RUN pip install flask


COPY app.py /opt/app.py


ENTRYPOINT <command to run the application>



then build the image 


# docker build . -t <docker_account_name/image-name>


run you image 


# docker run <image-name>


push to a docker repository 


# tag your docker instance to the repo 


login to docker cli using 


docker login 


username

password 


# docker push <docker_account_name/image-name>






































2 comments: