Saturday, 22 July 2023

How to create docker image

 1. keep your code ready

2. write docker file:

Dockerfile

# downloaded base image
FROM python:3.8-alpine
# copied code from current location to app folder inside docker
COPY . /app
# redirecting to app folder inside docker
WORKDIR /app
# install all libraries inside your docker
RUN pip install -r requirement.txt
# running the final Python code
CMD python app.py


3. download the docker desktop on your computer
4. in the command prompt go to the location of dockerfile and run
    1. to create a docker image
    docker build -t <docker image name> .
    #example => docker build -t first-docker-image .

    2. to see existing docker images
    docker images

    3. to run the created docker image
    docker run -p <port>:<port> <image name>
    #example => docker run -p 5000:5000 first-docker-image

    here once you run, you get two ip address, one is your localhost address and another one is docker image ip address that will not work on our local computer. so take computer localhost to run

    4. to see all running docker images
    docker ps

    5. to stop the running docker image
    docker stop <container name>

    6. to delete the existing docker image
    docker image rm -f <docker image name>
    #example => docker image rm -f first-docker-image

    7. to change docker image name
    docker tag <current name> <new name>
    docker tag first-docker-image first-docker-image-1

    8. to push your docker image to repository
        login to docker
        1. docker login
        2.  docker push <image name>:<version>
        #example => docker push first-docker-image-1:latest
        now your image will be visible on the repository

    
    9. pull any image from docker
        docker pull <image name>:<tag> 
        #example => docker pull first-docker-image-1:latest