Beyond Code: Crafting DevEx Journeys
In the dynamic realm of software development, where every keystroke contributes to the creation of innovative solutions, the experience of developers plays a
This post is about Docker, how to build an own image and launch a container by running this image.
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.
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:
ADD . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
ENV NAME World
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.
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.
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