Docker during development

3

I began to study the use of Docker and a question arose that I still can not understand:

Imagine that I have a python script that I want to run inside my container, so I go there and create the following DockerFile

FROM python:3
ADD hello_world.py /
CMD [ "python", "./hello_world.py" ]

So far everything 100%, I created the image, uploaded the container and managed to perform the tests that I needed in my development environment.

Imagine that now I want to make only a small change in my code hello_world.py to perform another test.

Do I need to perform the whole procedure again? Create the image, upload it and etc?

In my mind this seems unproductive, after all it would be much easier to just run the script on my machine. And use the container only to do the final tests by creating an approval environment.

Does Docker really work this way? Was not it made for the programmer to test while developing? Or am I using it incorrectly?

    
asked by anonymous 05.07.2017 / 19:43

2 answers

3

Yes you can use the container for development, so you can understand better I'll make some clarifications.

  • The CMD command will be executed only once when its container is startedado .
  • To make a change to your hello_world.py file and so that reflected in the container, you will need to link the volume where it finds your file with the volume of the container.
  • In order for you to use the container as a development environment, I suggest the following:

  • Change the command ADD hello_world.py / to ADD hello_world.py /scripts thus saving all your scripts to a folder.
  • Remove CMD from your Dockerfile , so to run your script you would connect in the bash of your container and run the script.
  • Second change is at the time of executing the command docker run , in it you would add -v .:/scripts , thus linking the folder where your Dockerfile is and scripts, with the scripts folder created inside the container. So any changes you make to your machine will be reflected in the files in the container
  • To execute the scripts you can access your container (the same one you already own) with docker exec -it #hashDoContainer bash (to find the hash of the container, just use the docker ps command and identify yours). This will allow you to access your container and run any script you want
  • 05.07.2017 / 20:04
    2

    Add the -v key to the command that starts the Docker container (% with%). This option can not be included in Dockerfile to maintain the portability of environments.

    Use with syntax:

    -v /diretorio/no/hospedeiro:/diretorio/no/container
    
        
    05.07.2017 / 19:57