Womenintech Part 7: Docker for beginners - Creating an own Docker Image


Bicycle

Introduction

This post is about Docker, how to build an own image and launch a container by running this image.

Cheatsheet

Docker

What is Docker? In general, Docker is an engine. With this engine you can develop, deploy and run applications isolated in a container. Containers virtualize the OS instead of hardware. Docker is very simple and fast. It is possible to "dockerize" applications in a few minutes. Another benefit is that applications are easy to build and to collaborate on. Developers are able to run any application as a lightweight, portable container literally anywhere. It is possible for example to have 20 Docker containers running on the base OS without needing a hypervisor compared to virtual machines where you would have to boot 20 operating systems with a lot of resources.

A container is based on an image. This image consists of application code, runtime, libraries, environment variables and configuration files. After executing the image a runtime instance is set up, the container. To list all running containers the command 'docker ps' or 'docker container ls' is used.

The Dockerfile

There are two different ways to create a Dockerfile. First, it is possible to start with a parent image, that means that our own image is based on this parent image. The second option is to create a base image, which is convenient if you want to control the total content of your image. A Dockerfile with a parent image could look like this:

FROM python:2.7-slim

WORKDIR /app

ADD . /app

RUN pip install --trusted-host pypi.python.org -r requirements.txt

EXPOSE 80

ENV NAME World
  • The keyword 'FROM' in the first line declares that we want to use a parent image. In this case, the image is based on a Python Docker image.
  • With 'WORKDIR' we set the working directory to /app.
  • The 'COPY' statement copies the contents from the current directory into the container at /app
  • 'RUN' installs the packages described in the txt.file.
  • With 'EXPOSE' the specified port is made available to the outside of the container.
  • 'ENV' makes it possible to declare environment variables and 'CMD' runs the declared application when the container launches.

Creating the image

To create the Docker image out of the Dockerfile the following command is use:

docker build –t tag:version .

With –t we can tag the image with a name to distinguish it from other images, for example 'docker build -t py-app:1.0.0 .'. With the dot at the end we set the build context to the current directory. By executing the command 'docker image ls' or 'docker image' we can list all images.

Now we are able to run a container with the previous created image. We can use the command 'docker run -it py-app:1.0.0 /bin/bash' to run the Docker container in interactive mode.

Docker Hub

Now, we want to upload our image to Docker Hub. Therefore, we first need to login with the command 'docker login' and provide a Docker ID. Then we use the command 'docker push username/tag:version' to pull the image to the Hub. In our case the command would look as follows:

docker push infralovers/py-app:1.0.0

On Docker Hub we can see the new image and Docker Hub provides automatically a pull command for the image which looks as follows: 'docker pull infralovers/py-app'. To run the container the command 'docker run py-app' is used.

Go Back explore our courses

We are here for you

You are interested in our courses or you simply have a question that needs answering? You can contact us at anytime! We will do our best to answer all your questions.

Contact us