Dockers — Build your first Image — Tutorial 2

Create an Image

Gal Hever
3 min readJan 10, 2022

Let’s create our first image!

If you didn’t go over the previous tutorial I really recommend you to go back and read a bit more about dockers and than to return here — Docker — Basic Actions and Installation — Tutorial 1.

Create an Image File

Open a new folder and name it however you want. I called mine “cowsay_image”. In this folder add a file that is named “app.py” with the next code:

import cowsay

cowsay.cow(“Hello”)

Under “cowsay_image” folder add another file “requirements.txt” with the package “cowsay”.

Create a Docker File

Under this cowsay_image folder add another file that is called “Dockerfile” with no extension in the end with the next code:

From python:3.6.5

WORKDIR /code

COPY . .

RUN pip install — trusted-host= -r requirements.txt

EXPOSE 8888

CMD [“python”,”app.py”]

Commands Interpretation

  • FROM <base image>:

Specifies the version and type of our base image for the container. Our container will include all of the dependencies and configurations already present in the base image we’ll choose.

  • WORKDIR /<main_dir>:

Sets the working directory for any subsequent commands to /<main_dir>. This means that any following commands, such as “COPY”, “RUN”, or “CMD”, will be performed within this directory.

  • COPY <current_dir> <docker_dir>:

Copies all the files from the source directory (where the Dockerfile is located) to the /<main_dir> directory in the Docker image.

  • RUN <install>:

This command installs the dependencies for the image.

  • CMD [“<statrt_command>”]:

This command will be executed when a container is started from this image.

Build an Image

Now that you have created all the files let’s build the image. Write in the Terminal the next command with the same folder’s path:

docker build . -t cowsayhello

If we want to publish this image in DockerHub that will be explained next we need to add the docker id before the image name as below:

docker build . -t <Docker ID>/cowsayhello

Let’s check that the build worked successfully by:

docker image ls

Let’s run the container:

docker run cowsayhello

Push an Image to DockerHub

If you want that your image will be public and other people will be able to use it you can publish it on DockerHub. DockerHub is a platform that are used as an images repository. For example you can see below some image that I uploaded that you can pull from my repository:

Now, Let’s push the cowsay image that we built to our repository! First of all sign up to DockerHub and get a dockerid here.

Now that you have an account you can push the image to your repo by:

docker push <Docker ID>/<Image Name>:<tagname>

Change to your ids all the places with <> and change “tagname” to the version of the image.

For example:

docker push gal3dockerid/cowsayhello:latest

If it worked successfully you will see the image in your repo:

Now, try to pull the image to your computer:

docker pull gal3dockerid/cowsayhello:latest

End Notes

In thos tutorial we built our first image and uplaoded it to DockerHub. If you want to extend a bit more about the contents of the Dockerfile you can move on to my next blog post: Dockers — How to build a Dockerfile — Tutorial 3.

--

--