Docker Basic Commands - Part 02
Image credit: App Dyanamics (found from google search)

Docker Basic Commands - Part 02

Steps to install Docker:

Below is the link for docker installation, that is provided by Docker, we have clear documentation available for installing docker. This link will direct you to official docker documents.

https://docs.docker.com/engine/install/ubuntu/

Once docker is installed check the version of docker?

No alt text provided for this image

Let’s familiarize our self with few basic docker commands:-

  • docker ps

?Gives you the list of active containers on your machine

No alt text provided for this image

In the output we can see it displays few details about the container

CONTAINER ID: Each and every container will be assigned with a unique ID

IMAGE: Every Image has an attached tag

COMMAND: Each and every image will be assigned with a unique ID

CREATED: shows the detail when it was created

STATUS: Shows the detail whether the container is active or not

PORTS: Exposed port

NAMES:? Random name is assigned by docker for container created

  • docker ps? -a??

Gives you the full list of containers including the one's which are stopped or crashed

No alt text provided for this image

  • docker images

Gives you the list of images present in the system

No alt text provided for this image

  • docker run ARGUMENT IMAGE-NAME

?It will create a container using the image name

No alt text provided for this image

Here arguments -itd means

I – Interactive

T – Connected to terminal

D – Detached Mode

We can run? the container as detached mode (-itd) or root mode (-td)as per the requirement.

  • docker stop CONTAINER-ID/NAME?

?To stop the container

No alt text provided for this image

  • docker rm CONTAINER-ID/NAME

To remove a container

No alt text provided for this image

  • docker rmi? IMAGE-ID

To remove a docker image

No alt text provided for this image

  • docker exec -it container name /bin/bash/

Get access to shell of container?

With this command we can run our required code within the container.

No alt text provided for this image

  • Volumes in Container: Containers are dynamic in nature, they move a lot, today a container might be on server A tomorrow it may be on server B so?they will be shuffled, relocated as per the requirement?

Volume acts as data warehouse, or data storage attached externally to container.

Lets say for example in data directory (data) we have data1 and data2 as file that are part of the data associated for our application to work with.?

No alt text provided for this image

but we do not want these data to be really stored within container, instead of that we want it be mounted, so the actual read and write will happen within data1 and data2 it will look like it’s a part of container. So these mounted data are called as Volumes.

No alt text provided for this image
No alt text provided for this image

Now that we have basic idea about creating, deleting and starting a container, further will see how to create your own image.

On our server machine we need to install Apache2 by running below mentioned commands:

sudo apt-get update

sudo apt-get install apache2

Creating a Dockerfiles:

Create a directory as sample-code within that create a? Dockerfile and index.html file

No alt text provided for this image
No alt text provided for this image

Every dockerfile starts with FROM command which tells? from where is the base image coming from , here? we are telling to use httpd as our base image, and then we want to add a file? index.html, which will act as our source and our destination will be ?/usr/local/apache2/htdocs/index.html, when we run this file docker will create a temporary container and it will create an image out of it once the image is created we can use this image to create a container out of it.?

docker build? . -t first-image: this command will build an image where first-mage is the name of image

docker run -itd? --name first-container -p 8090:80 first-image: this command will build the container where first-container is name of container mapped to port 80

we can see the output : https://server_IP:port?

Conclusion:

We came across various terms and parameters that are used in docker to containerize an application, Docker containers are better that virtual machines which ensure that our application runs without an error, it makes the complicated process into simple way of containerizing? our application.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了