GithubHelp home page GithubHelp logo

paulinamoskwa / hello-docker-dev Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 84 KB

Step-by-step beginners guide to use Docker in development mode ๐Ÿ‹

Dockerfile 63.31% Python 36.69%
beginner-friendly docker docker-build docker-compose docker-exec docker-run dockerfile tutorial

hello-docker-dev's Introduction

Docker-Dev 101: A Step-by-Step Guide

Windows: 11 Docker Desktop: Genuary, 2023

Tutorial difficulty level: โœจ made by a blonde, for blondes โœจ

Docker-Dev

This tutorial assumes that the previous one has already been seen.
From the previous tutorial we learned how to use the following commands:

docker build -t hello_docker .
docker run -d --name=hello_docker_container hello_docker sleep infinity
docker exec hello_docker_container python3 main.py

Note. The content of the files compared to the previous repo is very similar, it just changed slightly the main.py and the DockerFile. In the DockerFile we now create a working directory (for later convenience).
The new DockerFile is the following:

FROM python:3.9
ADD main.py .

WORKDIR /hello_docker_dev

ADD requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

However, now, if we modify something in the main.py, such as even just adding a print('...') we have to do everything all over again: create the image, run the container and re-run the script. Too much.

To get the ability to edit the main.py and run it without having to re-do the whole Docker-process from scratch, it is enough to slightly change the settings of the docker run command.

Instead of the simple command:

docker run -d --name=hello_docker_container hello_docker sleep infinity

We add: -v </host/directory>:</container/directory>.

The -v flag stands for "volume" and it creates a volume that allows to share data between the host machine and the container. When we run a Docker container with the -v flag, we specify a host directory and a container directory, separated by a colon, to create the volume.

In our specific exemple, this would be:

docker run -d -v absolute\path\to\Hello-Docker-Dev:/hello_docker_dev --name=hello_docker_container hello_docker sleep infinity

Now, running the command:

docker exec hello_docker_container python3 main.py

we run the main.py as it is saved in the repository, obtaining the followin output:

A tensor from a list
tensor([1, 2, 3])

We modify the main.py by adding a simple print:

import torch

print('Hello! This is a change made after the image and the container were created.')
print('A tensor from a list')
tensor = torch.tensor([1, 2, 3])
print(tensor)

And, by running again the command:

docker exec hello_docker_container python3 main.py

we now get as output:

Hello! This is a change made after the image and the container were created.
A tensor from a list
tensor([1, 2, 3])

Docker Compose

Everything cool, but now the commands are getting quite long to remember and to launch each time.

Let us introduce โœจ Docker Compose โœจ, a tool for defining and running multi-container Docker applications. With Compose, we use a single YAML file to configure everything. Then, with a single command, we create and start all the services from the configuration.

We rewrite the same build and run components, but in single YAML file.

Note. Some things change: for example, the sleep ininfity is replaced with command: tail -F anything.

The overall YAML looks like the following:

services:
  hello_docker_dev_image:
    build: .
    container_name: hello_docker_container
    command: tail -F anything
    volumes:
      - absolute\path\to\Hello-Docker-Dev:/hello_docker_dev

The build line specifies where the DockerFile is located, the rest specifies the name we want to give the container, additional commands and possibly the -v option.

Once this YAML is created, we can start the whole process with the following command:

docker-compose up -d

Notice that the flag -d remains.

At this point we can do everything as we did before, so run the main.py (docker exec hello_docker_container python3 main.py), edit it, and re-run it, etc.

Once we are done (so, once we also stopped the container with the command docker stop hello_docker_container), in order to shut down everything correctly we use the command:

docker-compose down

Docker Compose Extra

Docker Compose is a very powerful tool. We can involve different images in different ways.

For example, suppose we want to launch our own application, and we want to lean on Redis.
It is possible to simply change the YAML in the following way:

services:
  hello_docker_dev_image:
    build: .
    container_name: hello_docker_container
    command: tail -F anything
    volumes:
      - absolute\path\to\Hello-Docker-Dev:/hello_docker_dev
    depends_on:
      - redis_image
  
  redis_image:
    image: redis:alpine

In this case we are taking Redis from the Hub (the alpine version) and we are making depend our content on that.

Useful Resource/s

A good resource for this topic.

hello-docker-dev's People

Contributors

paulinamoskwa avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.