Running a container:
—> sudo apt update
—> sudo apt install [Link] -y
—> service docker status
—> sudo docker run -p 9090:8080 -d pengbai/docker-
supermario
Explanation:
----------------
#sudo : admin privilege
# docker : cli
# run : create and run the container
# p : publish the container port 8080 to hostport 9090
#pengbai/docker-supermario : container image being fetched
from docker hub
# d : detached mode / run the container in background
Deploy webserver nginx :
—> sudo docker run -p 92:80 -d nginx
Deploy webserver httpd :
—> sudo docker run -p 93:80 -d httpd
Deploy wildfly application server :
—> sudo docker run -p 94:8080 -d [Link]/wildfly/wildfly
—> docker ps
—> docker ps –a
Start,stop,kill, remove operation
—> docker stop <container-id>
—> docker start <container-id>
—> docker restart <container-id
—> docker kill <container-id>
—> docker rm <container-id>
—> docker images
—> docker rmi <image-id>
Export Import
—> docker export -o [Link] <container-id>
—> ls -lrt
—> docker import [Link] new image
—> docker images
Save Load
—> docker save
—> docker save -o [Link] httpd
—> ls -lrt
—> docker load
—> docker load --help
—> docker load -i [Link]
—> docker images
Tag and push
—> docker tag mysql mysql-old
—> docker images
—> docker tag mysql vinithamx/mysql-new
#Create a docker hub account name / free
—> docker login
#format of the image should be
Imageregistryurl/Dockerhubaccountname/image-name
—> docker push [Link]/vinithamx/mysql-new
Customise a container image
Step 1 - create a nginx webserver container [ docker run ]
Step 2 - Enter inside the nginx container to customise.
[ docker exec -it ]
Step 3 - Update and install vim software [ apt update , apt
install vim ]
Step 4 - Modify [Link] [/usr/nginx/share/html]
What is Dockerfile :
- the file contains the set of instructions to create a
customised image
- use below parameters to create the Dockerfile
- Use docker build command to build the customised image
Dockerfile
FROM. ## download the nginx image from registry
MAINTAINER. ## the person who maintains the container
image
COPY. ## copy the file from local path to container path
ADD. ## Copy the file from remote path to container path
RUN. ## install softwares , uninstall softwares, create files,
folders
EXPOSE
ENV
ENTRYPOINT
CMD. ## the command to start the container
## Example 1
—> vi Dockerfile
FROM nginx
MAINTAINER Vinitha vinithamx@[Link]
RUN apt update
RUN apt install vim -y
COPY [Link] /usr/nginx/share/html/
—> docker build . -t customnginx-image
—> touch [Link]
—> docker images
## Example 2
—> mkdir mysql
—> cd mysql
—> vi Dockerfile
# Use an official MySQL image
FROM mysql:8.0
# Set environment variables
ENV MYSQL_ROOT_PASSWORD=rootpassword
ENV MYSQL_DATABASE=mydubai
ENV MYSQL_USER=user
ENV MYSQL_PASSWORD=password
# Expose port 3306
EXPOSE 3306
—> sudo docker build -t mysql-db .
—> sudo docker run -p 3306:3306 -d mysql-db
## Example 3
—> mkdir tomcat
—> cd tomcat
—> touch web [Link]
—> vi Dockerfile
# Use an official Tomcat image
FROM tomcat:9.0
# Copy the web application to the container
COPY ./[Link] /usr/local/tomcat/webapps/
# Expose port 8080
EXPOSE 8080
—> sudo docker build -t java-tomcat-app .
—> sudo docker run -p 8080:8080 java-tomcat-app
## Example 4
—> mkdir python
—> cd python
—> vi Dockerfile
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at
/app
COPY . /app
# Install the necessary Python packages
RUN pip install --no-cache-dir -r [Link]
# Make port 5000 available to the world outside this
container
EXPOSE 5000
# Define environment variable
ENV NAME World
# Run [Link] when the container launches
CMD ["python", "[Link]"]
—> vi [Link]
from flask import Flask
app = Flask(__name__)
@[Link]("/")
def hello():
return "Hello, World! Welcome to the Python Flask Web
App."
if __name__ == "__main__":
[Link](host='[Link]', port=5000)
—> vi [Link]
Flask==2.0.1
—> sudo docker build -t python-flask-app .
—> sudo docker run -p 5000:5000 python-flask-app
##################################
—> docker
—> docker run. ## create and run a container
—> docker ps. ## Show the running containers
—> docker ps -a. ## a : all / show all containers with
stopped/started started
—> docker stop. # stop the container
—> docker start. # start the container
—> docker restart. # restart the container
—> docker kill. # kill the container
—> docker images ## list the images
—> docker rm. ## remove the container
—> docker rmi. ## remove the container images
—> docker export. ## take backup of a container
—> docker import. ## import the backup tar file and create
the container image
—> docker save. ## take backup of a container image
—> docker load. ## import the backup to images
—> docker tag. ## copy the image name to different name
—> docker exec. ## enter inside the container
—> docker commit. ## extract the image from the running
container
—> docker build. ## to build a container image