I want to start off by saying I am an absolute beginner with docker and have never used it prior to the project I am currently working on. If I am posting this in the wrong place, I would be happy to remove it and ask for help elsewhere. The OS version, App version, and steps aren’t that important because it seems to be more of a conceptual issue.
I have watched quite a few tutorial videos and have started to read through a lot of the Dockerfile and docker-compose documentation. That being said, I think I have a fundamental misunderstanding about how to actually develop code using docker.
In summary, I want to create a Django application (using the python official image), use a MySQL database, and serve it using apache (using the httpd image). I think I understand the general process:
- Create a Dockerfile that installs Django and other dependencies with a requirements.txt, create the directory, copy in the Django project files, etc
- Create a docker-compose file that builds the Django image and runs it along with the SQL DB and web server on the same network
The main issue that I would like to address is the following:
When creating the Django image that I will then be attempting to build, you need to copy in the source code. Nearly everything that I have seen has a line like the following:
COPY . /some_dir
With the intention being to copy the Django website code that you already have into the docker Image you are building. My issue with this is how are you supposed to already have this code if you need Django on your local machine to init the project, to begin with? I want to use docker so that I can keep my local machine free of dependency conflicts, so I want Django to only exist within the container; this is an issue though because I still need to have Django on my machine to run a “Django startproject” command or similar, as that is what makes the code that I would then have to COPY into the image. It feels a bit like a chicken-egg kind of scenario.
Additionally, my end goal and the final purpose of the container is to host a website; when you start the container on an EC2 instance I want to be able to visit the IP address and view the website, but what if you are just developing the code locally? How would you go about spinning up the Django container to, for example, create a new app within a project, without turning on the Apache container as well? Should I create two docker-compose files, one for serving the website and the other for development purposes?
I have heard about volumes, but my understanding was they stored information generated by the container, like database entries, so that they persist between containers starting and stopping. I can see a world where this might solve my problem, but I just wanted to ask what the general workflow is for developing code with docker; I want to only rely on the dependencies that exist within the container, and not need to install anything additional directly on my machine.
If you’ve gotten to the end of this, I’m sure there are like 100 things wrong with the post I just made, and I might not have illustrated the issue I’m having in the clearest manner, but I would really appreciate any help. I’m trying to learn docker because I think it could be really useful and I find the concept interesting, but I’m having a hard time wrapping my head around the general workflow.