Sunday, 7 October 2018

Basic Usage of Docker

In This Section, I will show how to download a docker image, build a container and how to access the container.

1- To create a new container, choosing a base image with the OS- ubuntu or centos or another. Search for a base image with the docker search command.

[root@docker-server ~]# docker search ubuntu
This command will show you all ubuntu images.

2- Now Download the base image to our server

[root@docker-server ~]# docker pull ubuntu

This command downloads an image to your server from docker registry/DockerHub.

3- Check the Downloaded images.

[root@docker-server ~]# docker images


4- Remove Docker Images

[root@docker-server ~]# docker rmi    <REPOSITRY Name /IMAGE ID >

5- Launch New Container with Image.

[root@docker-server ~]# docker run -i -t ubuntu:16.04 /bin/bash


The above command is divided as follows:

-i            is used to start an interactive session.
-t           allocates a tty and attaches stdin and stdout.

ubuntu:16.04 is the image that we used to create the container.
bash (or /bin/bash) is the command that we are running inside the Ubuntu container.


Note- The container will stop when you leave it with the command exit. If you like to have a container that is running in the background, you just need to add the -d option in the command

or

To exit from docker container type CTRL + P + Q. This will leave container running in background an provide you host system console.

[root@docker-server ~]# docker run -i -t --name=Server-Linux02 -d  ubuntu:16.04 /bin/bash


6- Now you can see the container running in the background by using command

[root@docker-server ~]# docker ps


7- Access the shell of container that runs in the background mode

[root@docker-server ~]# docker exec -i -t 5c54131e9883 /bin/bash


Other e.q.

Update system-

[root@docker-server ~]# docker exec e18de3b27825 apt-get update

Install Apache Package-
[root@docker-server ~]# docker exec e18de3b27825 apt-get install apache2 -y


8- To list all containers (including stopped container) use following command.

[root@docker-server ~]# docker ps -a


9- Start/Stop Container

#  docker stop <CONTAINER ID>
#  docker start <CONTAINER ID>


10- Remove The Container
If you like to remove the container, stop it first and then remove it with the command.

[root@docker-server ~]# docker rm <CONTAINER ID>


11- Run Apache inside Docker container and access apache Server

[root@docker-server ~]# docker run -i -t -d --name Apche-Server01 -p 81:80 ubuntu:16.04

( -p option exposes the host port to container port. )

[root@docker-server ~]# docker ps
[root@docker-server ~]# docker exec 8f5e22f73e10 apt-get update
[root@docker-server ~]# docker exec 8f5e22f73e10 apt-get install apache2 -y
[root@docker-server ~]# docker exec 8f5e22f73e10 service apache2 start
[root@docker-server ~]# docker exec 8f5e22f73e10 service apache2 status

In order to visit the page served by the Apache2 container, open a browser from a remote location in your LAN and type the IP address of your machine using the HTTP protocol.


12- View Logs for a Docker Container [root@docker-server ~]# docker logs <Containe ID>

13- Rename Docker Container

[root@docker-server ~]# docker rename <Old_Name>      <New_Name>

No comments:

Post a Comment