General docker workflow misunderstanding

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:

  1. Create a Dockerfile that installs Django and other dependencies with a requirements.txt, create the directory, copy in the Django project files, etc
  2. 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.

Ther i some guide in the documentation

but I try to reply your questions as much as I can, but I don’t promise detailed instructions in one post.

You decide what you want to copy. Copying the source code means you don’t need generated files on your machine. I don’t remember Django since I used it years ago at the university, but if you need to use a tool tha generates the base Django project, you can do that too in a container and copy out the result to the host.
Docker also supports to use docker build and save the result on your host using the --output option.

You can put everything into a Dockerfile and store the files on your host only that you need to edit. For example you have the framework in the container and store 10 files on the host that you copy where you want to during the build.

Sometimes you can’t avoid to install some tools on your machine if you want to be able to develop. For example when the IDE needs a specific python version on the host to provide some help with the code.

Visual Studio Code allows you to develop in a container, so you can use a container as it were a virtual machine in which you install everything you need including file editors that we usually don’t have in containers. And of course Visual Studio Code will install its extensions in the container.

Docker Desktop is an option too. It has a feature called “Dev Enviornment” which is based on autodetecing the programming language and pulling a docker image and also depends on the above mentioned feature of Visual Studio Code.

I used development environments with and without Docker Desktop. Both solutions have their benefits.

There are volumes and bind mounts but both can be created with the same command/configuration. Volumes will not help you during development. Local volumes are folders on your machine (or in a virtual machine in case of Docker Desktop) managed by Docker which sets proper permissions for the files inside the container.

One thing that many people “fight with” is setting proper permissions during development, since the folders of volumes will not be accessible by your non-root users by default. Even if you can access that folder, you need to make your files have an owner and or group membership that allows the processes inside the container and you to read or edit the files depending on what you want to do with the files. This is a longer topic and it was discussed on the forum multiple times so I would not go into details here for now.

I hope I could give you some ideas. Docker can help you a lot but you lso need to be open to learn because you will need to.

Here s alink that wou used to recommend for beginners, but it will not answer your current questions quickly. It will just help you (in time) to understand containers better.

https://container.training/intro-selfpaced.yml.html