Post

How to create a new docker image

How to create a new docker image

Steps to create, test and push a docker image

  1. Download and install Docker Desktop. If on Linux, download Docker Engine - Community.
  2. Create a Docker file with the name Dockerfile or download one.
  • Simple docker file example

    1
    2
    3
    4
    
      FROM ubuntu:18.04
      RUN apt-get update
      RUN apt install -y python3
      RUN python3 --version
    
  • Official Example

  • Advanced Example

    1
    
      >notepad Dockerfile
    
  1. Login to docker hub

    1
    
     >docker login
    
  2. Make changes to docker file and build it

    1
    2
    3
    4
    5
    6
    
     >docker build --rm -t krishan_apex:latest .
        
     Sending build context to Docker daemon  3.584kB
     **** installs a bunch of packages ***
     **** installs some more packages ***
     Successfully built 4a4cc017fe68
    
  3. See the list of docker images

    1
    2
    3
    
     > docker images
     REPOSITORY                           TAG                                        IMAGE ID            CREATED             SIZE
     krishan_apex                         latest                                     4a4cc017fe68        2 minutes ago       6.33GB
    
  4. Create a repository in docker hub

  5. Tag your image with <username>/<reponame>:<tag>

    1
    
     > docker tag krishan_apex krishansubudhi/pytorch_apex:latest
    
  6. Run the container on your machine using the image you just created.

    1
    
     > docker run -it -d -p 5000:5000  krishansubudhi/pytorch_apex:latest
    
  7. Check all running docker containers

    1
    2
    3
    4
    
     > docker ps
        
     CONTAINER ID        IMAGE                                COMMAND             CREATED             STATUS              PORTS                                                  NAMES
     55c7cfc80e35        krishansubudhi/pytorch_apex:latest   "/bin/bash"         15 seconds ago      Up 13 seconds       5001/tcp, 8883/tcp, 0.0.0.0:5000->5000/tcp, 8888/tcp   angry_elbakyan
    
  8. Log in to terminal of runing container and run simple linux commands.

    1
    
     docker attach 55c7cfc80e35
    
  9. Push the image to dockerhub repo to be accessed by everyone. This takes a long time.

    1
    
    docker push krishansubudhi/pytorch_apex:latest
    

Reference :

IBM tutorial Official documentation What is Docker. RUN vs CMD vs ENTRYPOINT

This post is licensed under CC BY 4.0 by the author.