Docker Commands
docker --version
docker --help
docker images
docker pull ubuntu
docker run -it -d ubuntu
docker ps
docker ps -a
docker exec -it containerID bash
exit
docker stop containerID
docker login
docker commit containerID Zulaikha/ubuntu
docker tag Imagename Zulaikha/ubuntu
docker push Zulaikha/ubuntu
docker rm containerID
docker rmi ImageID
docker container logs containerID
docker container rm containerID
docker container kill containerID
docker container run containerID
docker container start containerID
docker export --output="latest.tar" containerID
docker import /home/username/Downloads/demo.tgz
Dockerfile
Create a dockerfile for Apache webserver
Make a directory to build our Docker file for which you can use vim editor.
mkdir /test
cd /test
sudo vim dockerfile
FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]
Description of the above commands
Ubuntu is our base image in which we are launching our server.
In the second line, is to set a non-interactive environment.
In the third line, the apt-get update command is used to update all the packages on Ubuntu.
In the fourth line, we are installing apache2 on our image.
In the fifth line, we are installing all the necessary utility Apache packages.
In the sixth line, the apt-get clean command cleans all the unnecessary files from the system.
In the seventh line, the EXPOSE command is used to expose the port 80 of Apache in the container.
The last command is used to run apache2 in the background.
Next step is to build the docker file by using the docker build command.
sudo docker build -t myapachewebserver .
Command:
-t: this option is to tag the image
The web server file has built, the next step is to create a container from the image for that we use the docker run command.
docker run -d -p 8080:80 myapachewebserver
OR
docker run -it -p 8080:80 myapachewebserver
Commands:
-d: This option is used to run the container in detached mode i.e the container can run in the background.
-p: This option is used to map our port number with 5000 port numbers on our localhost.
-it: This argument is used to allocate a bash shell and take standard input.
If you go to your web browser and write http://host_ip:8080 your Apache server is up and running on that port.
Create a dockerfile for Apache webserver and Copy the Index.html
Method 1
Make a directory
mkdir /test
cd /test
Create a sample web page with name index.html
vim /test/index.html
<!DOCTYPE html>
<html>
<body>
<h1>Hello World</h1>
<p>My Personal website</p>
</body>
</html>
Now create a file dockerfile
vim /test/dockerfile
FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
COPY index.html /var/www/html/
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]
Next step is to build the docker file by using the docker build command.
sudo docker build -t myapachewebserver .
Command:
-t: this option is to tag the image
The web server file has built, the next step is to create a container from the image for that we use the docker run command.
docker run -d -p 8080:80 myapachewebserver
OR
docker run -it -p 8080:80 myapachewebserver
Commands:
-d: This option is used to run the container in detached mode i.e the container can run in the background.
-p: This option is used to map our port number with 5000 port numbers on our localhost.
-it: This argument is used to allocate a bash shell and take standard input.
If you go to your web browser and write http://host_ip:8080 your
Apache server is up and running on that port.
Method 2
Another way to create apache web server image is using manual commands.
Step 1: Get the latest ubuntu Docker image by using docker pull command. Docker pull command is used to download or pull latest image from Docker Hub repositories.
docker pull ubuntu:latest
Step 2: To check and list all docker images
docker images
Step 3: To run docker image we use following command
-it : This argument is used to allocate a bash shell and take standard input.
-- name : This argument is used to tag a name to the running container.
docker run -it --name webserver ubuntu:latest
Step 4: Now install Apache webserver and it’s all dependencies
[root@Docker_Id]#apt-get update -y
[root@Docker_Id]#apt-get install apache2 -y
[root@Docker_Id]#apt-get install nano -y
Step 5: Now create a create a webpage at location /var/www/html/index.html
[root@Docker_Id]# nano /var/www/html/index.html
<!DOCTYPE html>
<html>
<body>
<h1>Hello World</h1>
<p>My Personal website</p>
</body>
</html>
Step 6: Exit from the running container using exit command. Exit command will stop the container. Exit command of docker same as power off or shut down of our computer.
[root@Docker_Id]# exit
Step 7: Now we have container in which apache webserver is installed and our webpage is configured. We can make a new customized docker image from the stopped docker image using docker commit command. Docker commit command will build our own image.
#docker commit <container_id or name of container will launching> <Name of new image>:<version name>
docker commit webserver webserver:v1
docker images
Step 8: Launching a webserver container using our customized image.
-p: This argument is used to port forwarding. Which means anybody from outside who comes for 8080 its request is forwarded to port 80. Port 80 is default port number where apache webserver runs.
/usr/sbin/apache2 –D FOREGROUND: This argument is command which will run when container is launched this command will start the web server
docker run -p 8080:80 webserver:v1 /usr/sbin/apache2 -D FOREGROUND
Step 9: To See the Result on web browser
http://:8080/
Create a dockerfile for Nginx webserver and Copy the Index.html
we need to change the docker file as below.Rest of the steps are same as above.
FROM ubuntu
RUN apt-get update
RUN apt-get install nginx -y
COPY index.html /var/www/html/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
Top comments (0)