Docker during the development

Asked

Viewed 539 times

3

I started 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, climbed the container and managed to perform the tests I needed in my development environment.

Imagine that now I just want to make a small change to 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 it seems unproductive, after all it would be much easier for me to just run the script on my machine. And use the container only to make the final tests creating an approval environment.

Does Docker really work this way? Isn’t it meant for the programmer to test while developing? Or am I using it incorrectly?

  • Use Docker in development and production, and we use the option -v to link between the machine folder and the container during development, php work, then apache takes care of identifying the file changes and no need to upload the container again, in its example as the executed command is a script I believe I have to drop and move the container up again to Update the script, but with the -v saves time in creating the new image

2 answers

4


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

  1. The command CMD will run only once, when your container is starlet.
  2. To make a change to your file hello_world.py and for what the same if reflected in the container, you will need to link the volume where you find your file with container volume.

So that you can use the container as a development environment I suggest you the following:

  1. Change the command ADD hello_world.py / for ADD hello_world.py /scripts thus storing all your scripts in a folder.
  2. Remove the CMD of your Dockerfile, so to run your script you would connect to the bash of your container and run the script.
  3. Second change is the time to execute the command docker run, in it you would add the -v .:/scripts, thus linking the folder where your Dockerfile and scripts are, 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
  4. To run the scripts you can access your container (the same one you already have) with docker exec -it #hashDoContainer bash (to find the container hash, just use the command docker ps and identify yours). So you can access your container and run any script you want

2

Add the key -v command that starts the Docker container (docker run). This option cannot be included in Dockerfile to maintain the portability of environments.

Use with the syntax:

-v /diretorio/no/hospedeiro:/diretorio/no/container
  • In case I link between the folder where my source code is stored on my computer with the code that is in the right image? And then I don’t need to keep tipping over the container and climbing up again to test?

  • Exactly that. It works like a directory shared between a virtual machine and the host machine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.