Building your own docker image

Note

If you have installed docker on local computer then make sure docker service is running before executing any command.

The following two examples explain the process of creating your own docker images.

Example 1

Step 1: Create a new directory and change into that directory

cd
mkdir image_1
cd image_1

Step 2: Clone the repo and change into animated_text directory

git clone https://github.com/mianasbat/animated_text.git
cd animated_text

Step 3: create a Dockerfile (case sensitive)

touch Dockerfile

Step 4: Open Dockerfile, write the following text in it and save the changes.

FROM ubuntu
RUN apt-get update
RUN apt-get install -y python3.6
RUN apt-get install -y python3-pip
RUN apt-get install -y wget
WORKDIR /usr/app
COPY . .
RUN pip3 install -r requirements.txt
WORKDIR /usr/app/src
CMD ["python3", "app.py"]

Step 5: Now run the command to create the image. The -t is used to tag the image. Dont forget the . at the end of the command.

docker build -t image_1:v1 .

Step 6: Now verify that the image is created using command

docker images

Step 7: To test the image, run your image using

# For Mac and Linux
docker run -it image_1:v1

# For windows in gitbash
winpty docker run -it image_1:v1

Step 8: Press q to quit the animation.

Example 2

In this example, we will create the same image but starting from a different base image

Step 1: Create a new directory and change to that directory

cd
mkdir image_2
cd image_2

Step 2: Clone the repo and change into animated_text directory

git clone https://github.com/mianasbat/animated_text.git
cd animated_text

Step 3: create a Dockerfile (case sensitive)

touch Dockerfile

Step 4: Open Dockerfile, write the following text in it and save the changes.

FROM python:3.6.9-slim
WORKDIR /usr/app
COPY . .
RUN pip install -r requirements.txt
WORKDIR /usr/app/src
CMD ["python", "app.py"]

Step 5: Now run the command to create the image. The -t is used to tag it. Dont forget the . at the end of the command.

docker build -t image_2:v1 .

Step 6: Now verify that the image is created running the command

docker images

Step 7: To test the image, run your image using

# For Mac and Linux
docker run -it image_2:v1

# For windows in gitbash
winpty docker run -it image_2:v1

Step 8: Press q to quit the animation.

Now run the command docker images and check the sizes of both your images. Small size images are more portable than large size. So in this case we will prefer second example.